Skip to content

Commit 320d045

Browse files
authored
feat(exporter-prometheus): support withoutScopeInfo option (#5993)
1 parent 57c5510 commit 320d045

File tree

6 files changed

+210
-103
lines changed

6 files changed

+210
-103
lines changed

experimental/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
1010

1111
### :rocket: Features
1212

13+
* feat(exporter-prometheus): support withoutScopeInfo option [#5993](https://github.com/open-telemetry/opentelemetry-js/pull/5993) @cjihrig
14+
1315
### :bug: Bug Fixes
1416

1517
### :books: Documentation

experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusExporter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export class PrometheusExporter extends MetricReader {
3535
prefix: '',
3636
appendTimestamp: false,
3737
withResourceConstantLabels: undefined,
38+
withoutScopeInfo: false,
3839
withoutTargetInfo: false,
3940
};
4041

@@ -87,6 +88,9 @@ export class PrometheusExporter extends MetricReader {
8788
const _withResourceConstantLabels =
8889
config.withResourceConstantLabels ||
8990
PrometheusExporter.DEFAULT_OPTIONS.withResourceConstantLabels;
91+
const _withoutScopeInfo =
92+
config.withoutScopeInfo ||
93+
PrometheusExporter.DEFAULT_OPTIONS.withoutScopeInfo;
9094
const _withoutTargetInfo =
9195
config.withoutTargetInfo ||
9296
PrometheusExporter.DEFAULT_OPTIONS.withoutTargetInfo;
@@ -96,7 +100,8 @@ export class PrometheusExporter extends MetricReader {
96100
this._prefix,
97101
this._appendTimestamp,
98102
_withResourceConstantLabels,
99-
_withoutTargetInfo
103+
_withoutTargetInfo,
104+
_withoutScopeInfo
100105
);
101106

102107
this._baseUrl = `http://${this._host}:${this._port}/`;

experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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';
2730
import { 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

2939
type 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'

experimental/packages/opentelemetry-exporter-prometheus/src/export/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ export interface ExporterConfig {
7575
*/
7676
withResourceConstantLabels?: RegExp;
7777

78+
/**
79+
* If true, scope labels are not included in scraped metrics.
80+
* @default false (scope labels are included)
81+
*/
82+
withoutScopeInfo?: boolean;
83+
7884
/**
7985
* If true, the target_info metric is not included in scraped metrics.
8086
* @default false (target_info metric is included)

0 commit comments

Comments
 (0)