@@ -23,8 +23,18 @@ import {
2323 DataPoint ,
2424 Histogram ,
2525} from '@opentelemetry/sdk-metrics' ;
26- import { hrTimeToMilliseconds } from '@opentelemetry/core' ;
26+ import {
27+ InstrumentationScope ,
28+ hrTimeToMilliseconds ,
29+ } from '@opentelemetry/core' ;
2730import { Resource } from '@opentelemetry/resources' ;
31+ import {
32+ ATTR_OTEL_SCOPE_NAME ,
33+ ATTR_OTEL_SCOPE_VERSION ,
34+ } from '@opentelemetry/semantic-conventions' ;
35+
36+ // This is currently listed as experimental.
37+ const ATTR_OTEL_SCOPE_SCHEMA_URL = 'otel.scope.schema_url' ;
2838
2939type PrometheusDataTypeLiteral =
3040 | 'counter'
@@ -173,19 +183,22 @@ export class PrometheusSerializer {
173183 private _appendTimestamp : boolean ;
174184 private _additionalAttributes : Attributes | undefined ;
175185 private _withResourceConstantLabels : RegExp | undefined ;
186+ private _withoutScopeInfo : boolean | undefined ;
176187 private _withoutTargetInfo : boolean | undefined ;
177188
178189 constructor (
179190 prefix ?: string ,
180191 appendTimestamp = false ,
181192 withResourceConstantLabels ?: RegExp ,
182- withoutTargetInfo ?: boolean
193+ withoutTargetInfo ?: boolean ,
194+ withoutScopeInfo ?: boolean
183195 ) {
184196 if ( prefix ) {
185197 this . _prefix = prefix + '_' ;
186198 }
187199 this . _appendTimestamp = appendTimestamp ;
188200 this . _withResourceConstantLabels = withResourceConstantLabels ;
201+ this . _withoutScopeInfo = ! ! withoutScopeInfo ;
189202 this . _withoutTargetInfo = ! ! withoutTargetInfo ;
190203 }
191204
@@ -227,12 +240,15 @@ export class PrometheusSerializer {
227240 private _serializeScopeMetrics ( scopeMetrics : ScopeMetrics ) {
228241 let str = '' ;
229242 for ( const metric of scopeMetrics . metrics ) {
230- str += this . _serializeMetricData ( metric ) + '\n' ;
243+ str += this . _serializeMetricData ( metric , scopeMetrics . scope ) + '\n' ;
231244 }
232245 return str ;
233246 }
234247
235- private _serializeMetricData ( metricData : MetricData ) {
248+ private _serializeMetricData (
249+ metricData : MetricData ,
250+ scope : InstrumentationScope
251+ ) {
236252 let name = sanitizePrometheusMetricName (
237253 escapeString ( metricData . descriptor . name )
238254 ) ;
@@ -250,19 +266,53 @@ export class PrometheusSerializer {
250266 ? `\n# UNIT ${ name } ${ escapeString ( metricData . descriptor . unit ) } `
251267 : '' ;
252268 const type = `# TYPE ${ name } ${ toPrometheusType ( metricData ) } ` ;
269+ let additionalAttributes : Attributes | undefined ;
270+
271+ if ( this . _withoutScopeInfo ) {
272+ additionalAttributes = this . _additionalAttributes ;
273+ } else {
274+ const scopeInfo : Attributes = { [ ATTR_OTEL_SCOPE_NAME ] : scope . name } ;
275+
276+ if ( scope . schemaUrl ) {
277+ scopeInfo [ ATTR_OTEL_SCOPE_SCHEMA_URL ] = scope . schemaUrl ;
278+ }
279+
280+ if ( scope . version ) {
281+ scopeInfo [ ATTR_OTEL_SCOPE_VERSION ] = scope . version ;
282+ }
283+
284+ additionalAttributes = Object . assign (
285+ scopeInfo ,
286+ this . _additionalAttributes
287+ ) ;
288+ }
253289
254290 let results = '' ;
255291 switch ( dataPointType ) {
256292 case DataPointType . SUM :
257293 case DataPointType . GAUGE : {
258294 results = metricData . dataPoints
259- . map ( it => this . _serializeSingularDataPoint ( name , metricData , it ) )
295+ . map ( it =>
296+ this . _serializeSingularDataPoint (
297+ name ,
298+ metricData ,
299+ it ,
300+ additionalAttributes
301+ )
302+ )
260303 . join ( '' ) ;
261304 break ;
262305 }
263306 case DataPointType . HISTOGRAM : {
264307 results = metricData . dataPoints
265- . map ( it => this . _serializeHistogramDataPoint ( name , metricData , it ) )
308+ . map ( it =>
309+ this . _serializeHistogramDataPoint (
310+ name ,
311+ metricData ,
312+ it ,
313+ additionalAttributes
314+ )
315+ )
266316 . join ( '' ) ;
267317 break ;
268318 }
@@ -279,7 +329,8 @@ export class PrometheusSerializer {
279329 private _serializeSingularDataPoint (
280330 name : string ,
281331 data : MetricData ,
282- dataPoint : DataPoint < number >
332+ dataPoint : DataPoint < number > ,
333+ additionalAttributes : Attributes | undefined
283334 ) : string {
284335 let results = '' ;
285336
@@ -291,15 +342,16 @@ export class PrometheusSerializer {
291342 attributes ,
292343 value ,
293344 this . _appendTimestamp ? timestamp : undefined ,
294- this . _additionalAttributes
345+ additionalAttributes
295346 ) ;
296347 return results ;
297348 }
298349
299350 private _serializeHistogramDataPoint (
300351 name : string ,
301352 data : MetricData ,
302- dataPoint : DataPoint < Histogram >
353+ dataPoint : DataPoint < Histogram > ,
354+ additionalAttributes : Attributes | undefined
303355 ) : string {
304356 let results = '' ;
305357
@@ -316,7 +368,7 @@ export class PrometheusSerializer {
316368 attributes ,
317369 value ,
318370 this . _appendTimestamp ? timestamp : undefined ,
319- this . _additionalAttributes
371+ additionalAttributes
320372 ) ;
321373 }
322374
@@ -343,7 +395,7 @@ export class PrometheusSerializer {
343395 attributes ,
344396 cumulativeSum ,
345397 this . _appendTimestamp ? timestamp : undefined ,
346- Object . assign ( { } , this . _additionalAttributes ?? { } , {
398+ Object . assign ( { } , additionalAttributes , {
347399 le :
348400 upperBound === undefined || upperBound === Infinity
349401 ? '+Inf'
0 commit comments