Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
* feat(opentelemetry-configuration): parse more parameters from config file [#5955](https://github.com/open-telemetry/opentelemetry-js/pull/5955) @maryliag
* feat(exporter-prometheus): support withoutTargetInfo option [#5962](https://github.com/open-telemetry/opentelemetry-js/pull/5962) @cjihrig
* feat(opentelemetry-configuration): parse trace provider from config file [#5992](https://github.com/open-telemetry/opentelemetry-js/pull/5992) @maryliag
* feat(exporter-prometheus): support withoutScopeInfo option [#5993](https://github.com/open-telemetry/opentelemetry-js/pull/5993) @cjihrig

### :bug: Bug Fixes

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

Expand Down Expand Up @@ -87,6 +88,9 @@ export class PrometheusExporter extends MetricReader {
const _withResourceConstantLabels =
config.withResourceConstantLabels ||
PrometheusExporter.DEFAULT_OPTIONS.withResourceConstantLabels;
const _withoutScopeInfo =
config.withoutScopeInfo ||
PrometheusExporter.DEFAULT_OPTIONS.withoutScopeInfo;
const _withoutTargetInfo =
config.withoutTargetInfo ||
PrometheusExporter.DEFAULT_OPTIONS.withoutTargetInfo;
Expand All @@ -96,7 +100,8 @@ export class PrometheusExporter extends MetricReader {
this._prefix,
this._appendTimestamp,
_withResourceConstantLabels,
_withoutTargetInfo
_withoutTargetInfo,
_withoutScopeInfo
);

this._baseUrl = `http://${this._host}:${this._port}/`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ import {
DataPoint,
Histogram,
} from '@opentelemetry/sdk-metrics';
import { hrTimeToMilliseconds } from '@opentelemetry/core';
import {
InstrumentationScope,
hrTimeToMilliseconds,
} from '@opentelemetry/core';
import { Resource } from '@opentelemetry/resources';
import {
ATTR_OTEL_SCOPE_NAME,
ATTR_OTEL_SCOPE_VERSION,
} from '@opentelemetry/semantic-conventions';

// This is currently listed as experimental.
const ATTR_OTEL_SCOPE_SCHEMA_URL = 'otel.scope.schema_url';

type PrometheusDataTypeLiteral =
| 'counter'
Expand Down Expand Up @@ -173,19 +183,22 @@ export class PrometheusSerializer {
private _appendTimestamp: boolean;
private _additionalAttributes: Attributes | undefined;
private _withResourceConstantLabels: RegExp | undefined;
private _withoutScopeInfo: boolean | undefined;
private _withoutTargetInfo: boolean | undefined;

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

Expand Down Expand Up @@ -227,12 +240,15 @@ export class PrometheusSerializer {
private _serializeScopeMetrics(scopeMetrics: ScopeMetrics) {
let str = '';
for (const metric of scopeMetrics.metrics) {
str += this._serializeMetricData(metric) + '\n';
str += this._serializeMetricData(metric, scopeMetrics.scope) + '\n';
}
return str;
}

private _serializeMetricData(metricData: MetricData) {
private _serializeMetricData(
metricData: MetricData,
scope: InstrumentationScope
) {
let name = sanitizePrometheusMetricName(
escapeString(metricData.descriptor.name)
);
Expand All @@ -250,19 +266,47 @@ export class PrometheusSerializer {
? `\n# UNIT ${name} ${escapeString(metricData.descriptor.unit)}`
: '';
const type = `# TYPE ${name} ${toPrometheusType(metricData)}`;
let additionalAttributes: Attributes | undefined;

if (this._withoutScopeInfo) {
additionalAttributes = this._additionalAttributes;
} else {
additionalAttributes = Object.assign(
{
[ATTR_OTEL_SCOPE_NAME]: scope.name,
[ATTR_OTEL_SCOPE_SCHEMA_URL]: scope.schemaUrl ?? '',
[ATTR_OTEL_SCOPE_VERSION]: scope.version ?? '',
},
this._additionalAttributes
);
}

let results = '';
switch (dataPointType) {
case DataPointType.SUM:
case DataPointType.GAUGE: {
results = metricData.dataPoints
.map(it => this._serializeSingularDataPoint(name, metricData, it))
.map(it =>
this._serializeSingularDataPoint(
name,
metricData,
it,
additionalAttributes
)
)
.join('');
break;
}
case DataPointType.HISTOGRAM: {
results = metricData.dataPoints
.map(it => this._serializeHistogramDataPoint(name, metricData, it))
.map(it =>
this._serializeHistogramDataPoint(
name,
metricData,
it,
additionalAttributes
)
)
.join('');
break;
}
Expand All @@ -279,7 +323,8 @@ export class PrometheusSerializer {
private _serializeSingularDataPoint(
name: string,
data: MetricData,
dataPoint: DataPoint<number>
dataPoint: DataPoint<number>,
additionalAttributes: Attributes | undefined
): string {
let results = '';

Expand All @@ -291,15 +336,16 @@ export class PrometheusSerializer {
attributes,
value,
this._appendTimestamp ? timestamp : undefined,
this._additionalAttributes
additionalAttributes
);
return results;
}

private _serializeHistogramDataPoint(
name: string,
data: MetricData,
dataPoint: DataPoint<Histogram>
dataPoint: DataPoint<Histogram>,
additionalAttributes: Attributes | undefined
): string {
let results = '';

Expand All @@ -316,7 +362,7 @@ export class PrometheusSerializer {
attributes,
value,
this._appendTimestamp ? timestamp : undefined,
this._additionalAttributes
additionalAttributes
);
}

Expand All @@ -343,7 +389,7 @@ export class PrometheusSerializer {
attributes,
cumulativeSum,
this._appendTimestamp ? timestamp : undefined,
Object.assign({}, this._additionalAttributes ?? {}, {
Object.assign({}, additionalAttributes, {
le:
upperBound === undefined || upperBound === Infinity
? '+Inf'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export interface ExporterConfig {
*/
withResourceConstantLabels?: RegExp;

/**
* If true, scope labels are not included in scraped metrics.
* @default false (scope labels are included)
*/
withoutScopeInfo?: boolean;

/**
* If true, the target_info metric is not included in scraped metrics.
* @default false (target_info metric is included)
Expand Down
Loading