Skip to content

Commit

Permalink
Fix conflicts.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonWeber committed Feb 14, 2025
2 parents ef8d84e + 0a761e9 commit e1cd627
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ For semantic convention package changes, see the [semconv CHANGELOG](packages/se

### :boom: Breaking Change

* feat(sdk-trace-base): Add `parentSpanContext` and remove `parentSpanId` from `Span` and `ReadableSpan` [#5450](https://github.com/open-telemetry/opentelemetry-js/pull/5450) @JacksonWeber
* feat(sdk-trace-base)!: Add `parentSpanContext` and remove `parentSpanId` from `Span` and `ReadableSpan` [#5450](https://github.com/open-telemetry/opentelemetry-js/pull/5450) @JacksonWeber
* (user-facing): the SDK's `Span`s `parentSpanId` was replaced by `parentSpanContext`, to migrate to the new property, please replace `span.parentSpanId` -> `span.parentSpanContext?.spanId`
* feat(sdk-metrics)!: drop deprecated `type` field on `MetricDescriptor` [#5291](https://github.com/open-telemetry/opentelemetry-js/pull/5291) @chancancode
* feat(sdk-metrics)!: drop deprecated `InstrumentDescriptor` type; use `MetricDescriptor` instead [#5277](https://github.com/open-telemetry/opentelemetry-js/pull/5266) @chancancode
* feat(sdk-metrics)!: bump minimum version of `@opentelemetry/api` peer dependency to 1.9.0 [#5254](https://github.com/open-telemetry/opentelemetry-js/pull/5254) @chancancode
Expand Down
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ All notable changes to experimental packages in this project will be documented
* feat(instrumentation-fetch): add a `requestHook` option [#5380](https://github.com/open-telemetry/opentelemetry-js/pull/5380)
* feat(instrumentation): re-export initialize function from import-in-the-middle [#5123](https://github.com/open-telemetry/opentelemetry-js/pull/5123)
* feat(sdk-node): lower diagnostic level [#5360](https://github.com/open-telemetry/opentelemetry-js/pull/5360) @cjihrig
* feat(exporter-prometheus): add additional attributes option [#5317](https://github.com/open-telemetry/opentelemetry-js/pull/5317) @marius-a-mueller
* Add `withResourceConstantLabels` option to `ExporterConfig`. It can be used to define a regex pattern to choose which resource attributes will be used as static labels on the metrics. The default is to not set any static labels.

### :bug: (Bug Fix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class PrometheusExporter extends MetricReader {
endpoint: '/metrics',
prefix: '',
appendTimestamp: false,
withResourceConstantLabels: undefined,
};

private readonly _host?: string;
Expand Down Expand Up @@ -82,11 +83,15 @@ export class PrometheusExporter extends MetricReader {
typeof config.appendTimestamp === 'boolean'
? config.appendTimestamp
: PrometheusExporter.DEFAULT_OPTIONS.appendTimestamp;
const _withResourceConstantLabels =
config.withResourceConstantLabels ||
PrometheusExporter.DEFAULT_OPTIONS.withResourceConstantLabels;
// unref to prevent prometheus exporter from holding the process open on exit
this._server = createServer(this._requestHandler).unref();
this._serializer = new PrometheusSerializer(
this._prefix,
this._appendTimestamp
this._appendTimestamp,
_withResourceConstantLabels
);

this._baseUrl = `http://${this._host}:${this._port}/`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,29 @@ const NO_REGISTERED_METRICS = '# no registered metrics';
export class PrometheusSerializer {
private _prefix: string | undefined;
private _appendTimestamp: boolean;
private _additionalAttributes: Attributes | undefined;
private _withResourceConstantLabels: RegExp | undefined;

constructor(prefix?: string, appendTimestamp = false) {
constructor(
prefix?: string,
appendTimestamp = false,
withResourceConstantLabels?: RegExp
) {
if (prefix) {
this._prefix = prefix + '_';
}
this._appendTimestamp = appendTimestamp;
this._withResourceConstantLabels = withResourceConstantLabels;
}

serialize(resourceMetrics: ResourceMetrics): string {
let str = '';

this._additionalAttributes = this._filterResourceConstantLabels(
resourceMetrics.resource.attributes,
this._withResourceConstantLabels
);

for (const scopeMetrics of resourceMetrics.scopeMetrics) {
str += this._serializeScopeMetrics(scopeMetrics);
}
Expand All @@ -193,6 +205,22 @@ export class PrometheusSerializer {
return this._serializeResource(resourceMetrics.resource) + str;
}

private _filterResourceConstantLabels(
attributes: Attributes,
pattern: RegExp | undefined
) {
if (pattern) {
const filteredAttributes: Attributes = {};
for (const [key, value] of Object.entries(attributes)) {
if (key.match(pattern)) {
filteredAttributes[key] = value;
}
}
return filteredAttributes;
}
return;
}

private _serializeScopeMetrics(scopeMetrics: ScopeMetrics) {
let str = '';
for (const metric of scopeMetrics.metrics) {
Expand Down Expand Up @@ -260,7 +288,7 @@ export class PrometheusSerializer {
attributes,
value,
this._appendTimestamp ? timestamp : undefined,
undefined
this._additionalAttributes
);
return results;
}
Expand All @@ -285,7 +313,7 @@ export class PrometheusSerializer {
attributes,
value,
this._appendTimestamp ? timestamp : undefined,
undefined
this._additionalAttributes
);
}

Expand All @@ -312,12 +340,12 @@ export class PrometheusSerializer {
attributes,
cumulativeSum,
this._appendTimestamp ? timestamp : undefined,
{
Object.assign({}, this._additionalAttributes ?? {}, {
le:
upperBound === undefined || upperBound === Infinity
? '+Inf'
: String(upperBound),
}
})
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ export interface ExporterConfig {
* @experimental
*/
metricProducers?: MetricProducer[];

/**
* Regex pattern for defining which resource attributes will be applied
* as constant labels to the metrics.
* e.g. 'telemetry_.+' for all attributes starting with 'telemetry'.
* @default undefined (no resource attributes are applied)
*/
withResourceConstantLabels?: RegExp;
}
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,40 @@ describe('PrometheusExporter', () => {
'',
]);
});

it('should export a metric with all resource attributes', async () => {
exporter = new PrometheusExporter({
withResourceConstantLabels: /.*/,
});
setup(exporter);
const body = await request('http://localhost:9464/metrics');
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedDefaultResourceLines,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
`counter_total{key1="attributeValue1",service_name="${serviceName}",telemetry_sdk_language="${sdkLanguage}",telemetry_sdk_name="${sdkName}",telemetry_sdk_version="${sdkVersion}"} 10`,
'',
]);
});

it('should export a metric with two resource attributes', async () => {
exporter = new PrometheusExporter({
withResourceConstantLabels: /telemetry.sdk.language|telemetry.sdk.name/,
});
setup(exporter);
const body = await request('http://localhost:9464/metrics');
const lines = body.split('\n');

assert.deepStrictEqual(lines, [
...serializedDefaultResourceLines,
'# HELP counter_total description missing',
'# TYPE counter_total counter',
`counter_total{key1="attributeValue1",telemetry_sdk_language="${sdkLanguage}",telemetry_sdk_name="${sdkName}"} 10`,
'',
]);
});
});
});

Expand Down
Loading

0 comments on commit e1cd627

Please sign in to comment.