diff --git a/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/logs/v1/logs_service_marshaler.py b/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/logs/v1/logs_service_marshaler.py index 0734fcf..a1bc8cb 100644 --- a/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/logs/v1/logs_service_marshaler.py +++ b/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/logs/v1/logs_service_marshaler.py @@ -97,6 +97,7 @@ def calculate_size(self) -> int: size += len(b"\x08") + Varint.size_varint_i64(self.rejected_log_records) if self.error_message: v = self.error_message.encode("utf-8") + self._marshaler_cache[b"\x12"] = v size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -105,7 +106,7 @@ def write_to(self, out: bytearray) -> None: out += b"\x08" Varint.write_varint_i64(out, self.rejected_log_records) if self.error_message: - v = self.error_message.encode("utf-8") + v = self._marshaler_cache[b"\x12"] out += b"\x12" Varint.write_varint_u32(out, len(v)) out += v diff --git a/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/metrics/v1/metrics_service_marshaler.py b/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/metrics/v1/metrics_service_marshaler.py index 44ddf94..7de23c5 100644 --- a/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/metrics/v1/metrics_service_marshaler.py +++ b/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/metrics/v1/metrics_service_marshaler.py @@ -97,6 +97,7 @@ def calculate_size(self) -> int: size += len(b"\x08") + Varint.size_varint_i64(self.rejected_data_points) if self.error_message: v = self.error_message.encode("utf-8") + self._marshaler_cache[b"\x12"] = v size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -105,7 +106,7 @@ def write_to(self, out: bytearray) -> None: out += b"\x08" Varint.write_varint_i64(out, self.rejected_data_points) if self.error_message: - v = self.error_message.encode("utf-8") + v = self._marshaler_cache[b"\x12"] out += b"\x12" Varint.write_varint_u32(out, len(v)) out += v diff --git a/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/trace/v1/trace_service_marshaler.py b/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/trace/v1/trace_service_marshaler.py index c848a14..bbef204 100644 --- a/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/trace/v1/trace_service_marshaler.py +++ b/src/snowflake/telemetry/_internal/opentelemetry/proto/collector/trace/v1/trace_service_marshaler.py @@ -97,6 +97,7 @@ def calculate_size(self) -> int: size += len(b"\x08") + Varint.size_varint_i64(self.rejected_spans) if self.error_message: v = self.error_message.encode("utf-8") + self._marshaler_cache[b"\x12"] = v size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -105,7 +106,7 @@ def write_to(self, out: bytearray) -> None: out += b"\x08" Varint.write_varint_i64(out, self.rejected_spans) if self.error_message: - v = self.error_message.encode("utf-8") + v = self._marshaler_cache[b"\x12"] out += b"\x12" Varint.write_varint_u32(out, len(v)) out += v diff --git a/src/snowflake/telemetry/_internal/opentelemetry/proto/common/v1/common_marshaler.py b/src/snowflake/telemetry/_internal/opentelemetry/proto/common/v1/common_marshaler.py index d0f199f..78c4c70 100644 --- a/src/snowflake/telemetry/_internal/opentelemetry/proto/common/v1/common_marshaler.py +++ b/src/snowflake/telemetry/_internal/opentelemetry/proto/common/v1/common_marshaler.py @@ -56,6 +56,7 @@ def calculate_size(self) -> int: size = 0 if self.string_value is not None: v = self.string_value.encode("utf-8") + self._marshaler_cache[b"\n"] = v size += len(b"\n") + Varint.size_varint_u32(len(v)) + len(v) if self.bool_value is not None: size += len(b"\x10") + 1 @@ -85,7 +86,7 @@ def calculate_size(self) -> int: def write_to(self, out: bytearray) -> None: if self.string_value is not None: - v = self.string_value.encode("utf-8") + v = self._marshaler_cache[b"\n"] out += b"\n" Varint.write_varint_u32(out, len(v)) out += v @@ -200,6 +201,7 @@ def calculate_size(self) -> int: size = 0 if self.key: v = self.key.encode("utf-8") + self._marshaler_cache[b"\n"] = v size += len(b"\n") + Varint.size_varint_u32(len(v)) + len(v) if self._value is not None: size += ( @@ -211,7 +213,7 @@ def calculate_size(self) -> int: def write_to(self, out: bytearray) -> None: if self.key: - v = self.key.encode("utf-8") + v = self._marshaler_cache[b"\n"] out += b"\n" Varint.write_varint_u32(out, len(v)) out += v @@ -250,9 +252,11 @@ def calculate_size(self) -> int: size = 0 if self.name: v = self.name.encode("utf-8") + self._marshaler_cache[b"\n"] = v size += len(b"\n") + Varint.size_varint_u32(len(v)) + len(v) if self.version: v = self.version.encode("utf-8") + self._marshaler_cache[b"\x12"] = v size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v) if self._attributes: size += sum( @@ -267,12 +271,12 @@ def calculate_size(self) -> int: def write_to(self, out: bytearray) -> None: if self.name: - v = self.name.encode("utf-8") + v = self._marshaler_cache[b"\n"] out += b"\n" Varint.write_varint_u32(out, len(v)) out += v if self.version: - v = self.version.encode("utf-8") + v = self._marshaler_cache[b"\x12"] out += b"\x12" Varint.write_varint_u32(out, len(v)) out += v diff --git a/src/snowflake/telemetry/_internal/opentelemetry/proto/logs/v1/logs_marshaler.py b/src/snowflake/telemetry/_internal/opentelemetry/proto/logs/v1/logs_marshaler.py index 20b25c8..a190d10 100644 --- a/src/snowflake/telemetry/_internal/opentelemetry/proto/logs/v1/logs_marshaler.py +++ b/src/snowflake/telemetry/_internal/opentelemetry/proto/logs/v1/logs_marshaler.py @@ -124,6 +124,7 @@ def calculate_size(self) -> int: ) if self.schema_url: v = self.schema_url.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -138,7 +139,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, v._get_size()) v.write_to(out) if self.schema_url: - v = self.schema_url.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v @@ -187,6 +188,7 @@ def calculate_size(self) -> int: ) if self.schema_url: v = self.schema_url.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -201,7 +203,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, v._get_size()) v.write_to(out) if self.schema_url: - v = self.schema_url.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v @@ -266,6 +268,7 @@ def calculate_size(self) -> int: size += len(b"\x10") + Varint.size_varint_u32(v) if self.severity_text: v = self.severity_text.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) if self._body is not None: size += ( @@ -311,7 +314,7 @@ def write_to(self, out: bytearray) -> None: out += b"\x10" Varint.write_varint_u32(out, v) if self.severity_text: - v = self.severity_text.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v diff --git a/src/snowflake/telemetry/_internal/opentelemetry/proto/metrics/v1/metrics_marshaler.py b/src/snowflake/telemetry/_internal/opentelemetry/proto/metrics/v1/metrics_marshaler.py index d75fc54..b6e57c3 100644 --- a/src/snowflake/telemetry/_internal/opentelemetry/proto/metrics/v1/metrics_marshaler.py +++ b/src/snowflake/telemetry/_internal/opentelemetry/proto/metrics/v1/metrics_marshaler.py @@ -102,6 +102,7 @@ def calculate_size(self) -> int: ) if self.schema_url: v = self.schema_url.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -116,7 +117,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, v._get_size()) v.write_to(out) if self.schema_url: - v = self.schema_url.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v @@ -165,6 +166,7 @@ def calculate_size(self) -> int: ) if self.schema_url: v = self.schema_url.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -179,7 +181,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, v._get_size()) v.write_to(out) if self.schema_url: - v = self.schema_url.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v @@ -253,12 +255,15 @@ def calculate_size(self) -> int: size = 0 if self.name: v = self.name.encode("utf-8") + self._marshaler_cache[b"\n"] = v size += len(b"\n") + Varint.size_varint_u32(len(v)) + len(v) if self.description: v = self.description.encode("utf-8") + self._marshaler_cache[b"\x12"] = v size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v) if self.unit: v = self.unit.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) if self._gauge is not None: size += ( @@ -301,17 +306,17 @@ def calculate_size(self) -> int: def write_to(self, out: bytearray) -> None: if self.name: - v = self.name.encode("utf-8") + v = self._marshaler_cache[b"\n"] out += b"\n" Varint.write_varint_u32(out, len(v)) out += v if self.description: - v = self.description.encode("utf-8") + v = self._marshaler_cache[b"\x12"] out += b"\x12" Varint.write_varint_u32(out, len(v)) out += v if self.unit: - v = self.unit.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v diff --git a/src/snowflake/telemetry/_internal/opentelemetry/proto/trace/v1/trace_marshaler.py b/src/snowflake/telemetry/_internal/opentelemetry/proto/trace/v1/trace_marshaler.py index 58741d9..e51b0c7 100644 --- a/src/snowflake/telemetry/_internal/opentelemetry/proto/trace/v1/trace_marshaler.py +++ b/src/snowflake/telemetry/_internal/opentelemetry/proto/trace/v1/trace_marshaler.py @@ -98,6 +98,7 @@ def calculate_size(self) -> int: ) if self.schema_url: v = self.schema_url.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -112,7 +113,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, v._get_size()) v.write_to(out) if self.schema_url: - v = self.schema_url.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v @@ -161,6 +162,7 @@ def calculate_size(self) -> int: ) if self.schema_url: v = self.schema_url.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) return size @@ -175,7 +177,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, v._get_size()) v.write_to(out) if self.schema_url: - v = self.schema_url.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v @@ -276,6 +278,7 @@ def calculate_size(self) -> int: ) if self.trace_state: v = self.trace_state.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) if self.parent_span_id: size += ( @@ -285,6 +288,7 @@ def calculate_size(self) -> int: ) if self.name: v = self.name.encode("utf-8") + self._marshaler_cache[b"*"] = v size += len(b"*") + Varint.size_varint_u32(len(v)) + len(v) if self.kind: v = self.kind @@ -342,7 +346,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, len(self.span_id)) out += self.span_id if self.trace_state: - v = self.trace_state.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v @@ -351,7 +355,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, len(self.parent_span_id)) out += self.parent_span_id if self.name: - v = self.name.encode("utf-8") + v = self._marshaler_cache[b"*"] out += b"*" Varint.write_varint_u32(out, len(v)) out += v @@ -438,6 +442,7 @@ def calculate_size(self) -> int: size += len(b"\t") + 8 if self.name: v = self.name.encode("utf-8") + self._marshaler_cache[b"\x12"] = v size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v) if self._attributes: size += sum( @@ -457,7 +462,7 @@ def write_to(self, out: bytearray) -> None: out += b"\t" out += struct.pack(" int: ) if self.trace_state: v = self.trace_state.encode("utf-8") + self._marshaler_cache[b"\x1a"] = v size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v) if self._attributes: size += sum( @@ -543,7 +549,7 @@ def write_to(self, out: bytearray) -> None: Varint.write_varint_u32(out, len(self.span_id)) out += self.span_id if self.trace_state: - v = self.trace_state.encode("utf-8") + v = self._marshaler_cache[b"\x1a"] out += b"\x1a" Varint.write_varint_u32(out, len(v)) out += v @@ -577,6 +583,7 @@ def calculate_size(self) -> int: size = 0 if self.message: v = self.message.encode("utf-8") + self._marshaler_cache[b"\x12"] = v size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v) if self.code: v = self.code @@ -587,7 +594,7 @@ def calculate_size(self) -> int: def write_to(self, out: bytearray) -> None: if self.message: - v = self.message.encode("utf-8") + v = self._marshaler_cache[b"\x12"] out += b"\x12" Varint.write_varint_u32(out, len(v)) out += v diff --git a/src/snowflake/telemetry/_internal/serialize/__init__.py b/src/snowflake/telemetry/_internal/serialize/__init__.py index 42f3c82..cac2834 100644 --- a/src/snowflake/telemetry/_internal/serialize/__init__.py +++ b/src/snowflake/telemetry/_internal/serialize/__init__.py @@ -167,8 +167,10 @@ def size_sfixed64(self, TAG: bytes, FIELD_ATTR: int) -> int: def size_bytes(self, TAG: bytes, FIELD_ATTR: bytes) -> int: return len(TAG) + Varint.size_varint_u32(len(FIELD_ATTR)) + len(FIELD_ATTR) + # This function should not be used for repeated strings due to caching by tag def size_string(self, TAG: bytes, FIELD_ATTR: str) -> int: v = FIELD_ATTR.encode("utf-8") + self._marshaler_cache[TAG] = v return len(TAG) + Varint.size_varint_u32(len(v)) + len(v) def size_message(self, TAG: bytes, FIELD_ATTR: MessageMarshaler) -> int: @@ -252,8 +254,9 @@ def serialize_bytes(self, out: bytearray, TAG: bytes, FIELD_ATTR: bytes) -> None Varint.write_varint_u32(out, len(FIELD_ATTR)) out += FIELD_ATTR + # This function should not be used for repeated strings due to caching by tag def serialize_string(self, out: bytearray, TAG: bytes, FIELD_ATTR: str) -> None: - v = FIELD_ATTR.encode("utf-8") + v = self._marshaler_cache[TAG] out += TAG Varint.write_varint_u32(out, len(v)) out += v