51
51
)
52
52
from opentelemetry .sdk .resources import Resource
53
53
from opentelemetry .sdk .util .instrumentation import InstrumentationScope
54
+ from opentelemetry .exporter .otlp .json .common ._internal .encoder_utils import encode_id
55
+ from opentelemetry .exporter .otlp .json .common .encoding import IdEncoding
54
56
55
57
_logger = logging .getLogger (__name__ )
56
58
@@ -164,20 +166,25 @@ def _get_aggregation(
164
166
return instrument_class_aggregation
165
167
166
168
167
- def encode_metrics (metrics_data : MetricsData ) -> Dict [str , Any ]:
169
+ def encode_metrics (
170
+ metrics_data : MetricsData ,
171
+ id_encoding : Optional [IdEncoding ] = None ) -> Dict [str , Any ]:
168
172
"""Encodes metrics in the OTLP JSON format.
169
173
170
174
Returns:
171
175
A dict representing the metrics in OTLP JSON format as specified in the
172
176
OpenTelemetry Protocol and ProtoJSON format.
173
177
"""
178
+ id_encoding = id_encoding or IdEncoding .BASE64
179
+
174
180
resource_metrics_list = []
175
181
176
182
for resource_metrics in metrics_data .resource_metrics :
177
183
resource_metrics_dict = {
178
184
"resource" : _encode_resource (resource_metrics .resource ),
179
185
"scopeMetrics" : _encode_scope_metrics (
180
- resource_metrics .scope_metrics
186
+ resource_metrics .scope_metrics ,
187
+ id_encoding ,
181
188
),
182
189
"schemaUrl" : resource_metrics .schema_url or "" ,
183
190
}
@@ -199,6 +206,7 @@ def _encode_resource(resource: Resource) -> Dict[str, Any]:
199
206
200
207
def _encode_scope_metrics (
201
208
scope_metrics_list : Sequence [ScopeMetrics ],
209
+ id_encoding : IdEncoding ,
202
210
) -> List [Dict [str , Any ]]:
203
211
"""Encodes a list of scope metrics into OTLP JSON format."""
204
212
if not scope_metrics_list :
@@ -209,7 +217,7 @@ def _encode_scope_metrics(
209
217
result .append (
210
218
{
211
219
"scope" : _encode_instrumentation_scope (scope_metrics .scope ),
212
- "metrics" : _encode_metrics_list (scope_metrics .metrics ),
220
+ "metrics" : _encode_metrics_list (scope_metrics .metrics , id_encoding ),
213
221
"schemaUrl" : scope_metrics .schema_url or "" ,
214
222
}
215
223
)
@@ -232,7 +240,7 @@ def _encode_instrumentation_scope(
232
240
}
233
241
234
242
235
- def _encode_metrics_list (metrics : Sequence [Metric ]) -> List [Dict [str , Any ]]:
243
+ def _encode_metrics_list (metrics : Sequence [Metric ], id_encoding : IdEncoding ) -> List [Dict [str , Any ]]:
236
244
"""Encodes a list of metrics into OTLP JSON format."""
237
245
if not metrics :
238
246
return []
@@ -247,14 +255,14 @@ def _encode_metrics_list(metrics: Sequence[Metric]) -> List[Dict[str, Any]]:
247
255
248
256
# Add data based on metric type
249
257
if isinstance (metric .data , Sum ):
250
- metric_dict ["sum" ] = _encode_sum (metric .data )
258
+ metric_dict ["sum" ] = _encode_sum (metric .data , id_encoding )
251
259
elif isinstance (metric .data , Gauge ):
252
- metric_dict ["gauge" ] = _encode_gauge (metric .data )
260
+ metric_dict ["gauge" ] = _encode_gauge (metric .data , id_encoding )
253
261
elif isinstance (metric .data , HistogramType ):
254
- metric_dict ["histogram" ] = _encode_histogram (metric .data )
262
+ metric_dict ["histogram" ] = _encode_histogram (metric .data , id_encoding )
255
263
elif isinstance (metric .data , ExponentialHistogram ):
256
264
metric_dict ["exponentialHistogram" ] = (
257
- _encode_exponential_histogram (metric .data )
265
+ _encode_exponential_histogram (metric .data , id_encoding )
258
266
)
259
267
# Add other metric types as needed
260
268
@@ -263,10 +271,10 @@ def _encode_metrics_list(metrics: Sequence[Metric]) -> List[Dict[str, Any]]:
263
271
return result
264
272
265
273
266
- def _encode_sum (sum_data : Sum ) -> Dict [str , Any ]:
274
+ def _encode_sum (sum_data : Sum , id_encoding : IdEncoding ) -> Dict [str , Any ]:
267
275
"""Encodes a Sum metric into OTLP JSON format."""
268
276
result = {
269
- "dataPoints" : _encode_number_data_points (sum_data .data_points ),
277
+ "dataPoints" : _encode_number_data_points (sum_data .data_points , id_encoding ),
270
278
"aggregationTemporality" : _get_aggregation_temporality (
271
279
sum_data .aggregation_temporality
272
280
),
@@ -276,14 +284,14 @@ def _encode_sum(sum_data: Sum) -> Dict[str, Any]:
276
284
return result
277
285
278
286
279
- def _encode_gauge (gauge_data : Gauge ) -> Dict [str , Any ]:
287
+ def _encode_gauge (gauge_data : Gauge , id_encoding : IdEncoding ) -> Dict [str , Any ]:
280
288
"""Encodes a Gauge metric into OTLP JSON format."""
281
289
return {
282
- "dataPoints" : _encode_number_data_points (gauge_data .data_points ),
290
+ "dataPoints" : _encode_number_data_points (gauge_data .data_points , id_encoding ),
283
291
}
284
292
285
293
286
- def _encode_histogram (histogram_data : HistogramType ) -> Dict [str , Any ]:
294
+ def _encode_histogram (histogram_data : HistogramType , id_encoding : IdEncoding ) -> Dict [str , Any ]:
287
295
"""Encodes a Histogram metric into OTLP JSON format."""
288
296
data_points = []
289
297
@@ -307,7 +315,7 @@ def _encode_histogram(histogram_data: HistogramType) -> Dict[str, Any]:
307
315
308
316
# Optional exemplars field
309
317
if hasattr (point , "exemplars" ) and point .exemplars :
310
- point_dict ["exemplars" ] = _encode_exemplars (point .exemplars )
318
+ point_dict ["exemplars" ] = _encode_exemplars (point .exemplars , id_encoding )
311
319
312
320
data_points .append (point_dict )
313
321
@@ -321,6 +329,7 @@ def _encode_histogram(histogram_data: HistogramType) -> Dict[str, Any]:
321
329
322
330
def _encode_exponential_histogram (
323
331
histogram_data : ExponentialHistogram ,
332
+ id_encoding : IdEncoding ,
324
333
) -> Dict [str , Any ]:
325
334
"""Encodes an ExponentialHistogram metric into OTLP JSON format."""
326
335
data_points = []
@@ -367,7 +376,7 @@ def _encode_exponential_histogram(
367
376
368
377
# Add exemplars if available
369
378
if hasattr (point , "exemplars" ) and point .exemplars :
370
- point_dict ["exemplars" ] = _encode_exemplars (point .exemplars )
379
+ point_dict ["exemplars" ] = _encode_exemplars (point .exemplars , id_encoding )
371
380
372
381
data_points .append (point_dict )
373
382
@@ -381,6 +390,7 @@ def _encode_exponential_histogram(
381
390
382
391
def _encode_number_data_points (
383
392
data_points : Sequence [Any ],
393
+ id_encoding : IdEncoding
384
394
) -> List [Dict [str , Any ]]:
385
395
"""Encodes number data points into OTLP JSON format."""
386
396
result = []
@@ -402,14 +412,14 @@ def _encode_number_data_points(
402
412
403
413
# Optional exemplars field
404
414
if hasattr (point , "exemplars" ) and point .exemplars :
405
- point_dict ["exemplars" ] = _encode_exemplars (point .exemplars )
415
+ point_dict ["exemplars" ] = _encode_exemplars (point .exemplars , id_encoding )
406
416
407
417
result .append (point_dict )
408
418
409
419
return result
410
420
411
421
412
- def _encode_exemplars (exemplars : Sequence [Any ]) -> List [Dict [str , Any ]]:
422
+ def _encode_exemplars (exemplars : Sequence [Any ], id_encoding : IdEncoding ) -> List [Dict [str , Any ]]:
413
423
"""Encodes metric exemplars into OTLP JSON format."""
414
424
result = []
415
425
@@ -423,16 +433,12 @@ def _encode_exemplars(exemplars: Sequence[Any]) -> List[Dict[str, Any]]:
423
433
424
434
# Add trace info if available
425
435
if hasattr (exemplar , "trace_id" ) and exemplar .trace_id :
426
- trace_id_bytes = exemplar .trace_id .to_bytes (16 , "big" )
427
- exemplar_dict ["traceId" ] = base64 .b64encode (trace_id_bytes ).decode (
428
- "ascii"
429
- )
436
+ trace_id = encode_id (id_encoding , exemplar .trace_id , 16 )
437
+ exemplar_dict ["traceId" ] = trace_id
430
438
431
439
if hasattr (exemplar , "span_id" ) and exemplar .span_id :
432
- span_id_bytes = exemplar .span_id .to_bytes (8 , "big" )
433
- exemplar_dict ["spanId" ] = base64 .b64encode (span_id_bytes ).decode (
434
- "ascii"
435
- )
440
+ span_id = encode_id (id_encoding , exemplar .span_id , 8 )
441
+ exemplar_dict ["spanId" ] = span_id
436
442
437
443
# Add value based on type
438
444
if hasattr (exemplar , "value" ) and isinstance (exemplar .value , int ):
0 commit comments