Skip to content

Commit d4c3c39

Browse files
no-ticket-replace-proto-serialization-with-json (#27)
1 parent 02a5a7d commit d4c3c39

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "partial_span_processor"
7-
version = "0.0.6"
7+
version = "0.0.7"
88
authors = [
99
{ name = "Mladjan Gadzic", email = "[email protected]" }
1010
]

src/partial_span_processor/__init__.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414

1515
from __future__ import annotations
1616

17-
import base64
17+
import json
1818
import threading
1919
import time
2020
from queue import Queue
2121
from typing import TYPE_CHECKING
2222

23+
from google.protobuf import json_format
2324
from opentelemetry._logs.severity import SeverityNumber
2425
from opentelemetry.exporter.otlp.proto.common.trace_encoder import encode_spans
2526
from opentelemetry.proto.trace.v1 import trace_pb2
@@ -113,15 +114,32 @@ def get_heartbeat_attributes(self) -> dict[str, str]:
113114
return {
114115
"partial.event": "heartbeat",
115116
"partial.frequency": str(self.heartbeat_interval_millis) + "ms",
117+
"partial.body.type": "json/v1",
116118
}
117119

118120
def get_log_data(self, span: Span, attributes: dict[str, str]) -> LogData:
119-
span_context = Span.get_span_context(span)
121+
instrumentation_scope = span.instrumentation_scope if hasattr(span,
122+
"instrumentation_scope") else None
123+
span_context = span.get_span_context()
120124

121125
enc_spans = encode_spans([span]).resource_spans
122126
traces_data = trace_pb2.TracesData()
123127
traces_data.resource_spans.extend(enc_spans)
124-
serialized_traces_data = traces_data.SerializeToString()
128+
serialized_traces_data = json_format.MessageToJson(traces_data)
129+
130+
# FIXME/HACK replace serialized traceId and spanId values as string comparison
131+
# possible issue is when there are multiple spans in the same trace.
132+
# currently that should not be the case.
133+
# trace_id and span_id are stored as int.
134+
# when serializing it gets serialized to bytes.
135+
# that is not inline with partial collector.
136+
traces_dict = json.loads(serialized_traces_data)
137+
for resource_span in traces_dict.get("resourceSpans", []):
138+
for scope_span in resource_span.get("scopeSpans", []):
139+
for span in scope_span.get("spans", []):
140+
span["traceId"] = hex(span_context.trace_id)[2:]
141+
span["spanId"] = hex(span_context.span_id)[2:]
142+
serialized_traces_data = json.dumps(traces_dict, separators=(',', ':'))
125143

126144
log_record = LogRecord(
127145
timestamp=time.time_ns(),
@@ -131,16 +149,17 @@ def get_log_data(self, span: Span, attributes: dict[str, str]) -> LogData:
131149
trace_flags=TraceFlags().get_default(),
132150
severity_text="INFO",
133151
severity_number=SeverityNumber.INFO,
134-
body=base64.b64encode(serialized_traces_data).decode("utf-8"),
152+
body=serialized_traces_data,
135153
resource=self.resource,
136154
attributes=attributes,
137155
)
138156
return LogData(
139-
log_record=log_record, instrumentation_scope=span.instrumentation_scope,
157+
log_record=log_record, instrumentation_scope=instrumentation_scope,
140158
)
141159

142160

143161
def get_stop_attributes() -> dict[str, str]:
144162
return {
145163
"partial.event": "stop",
164+
"partial.body.type": "json/v1",
146165
}

0 commit comments

Comments
 (0)