Skip to content
Merged
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 @@ -16,6 +16,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
* feat(opentelemetry-configuration): Parse of Configuration File [#5875](https://github.com/open-telemetry/opentelemetry-js/pull/5875) @maryliag
* feat(opentelemetry-configuration): parse of array objects on configuration file [#5947](https://github.com/open-telemetry/opentelemetry-js/pull/5947) @maryliag
* feat(opentelemetry-configuration): parse of environment variables on configuration file [#5947](https://github.com/open-telemetry/opentelemetry-js/pull/5947) @maryliag
* feat(exporter-prometheus): support withoutTargetInfo option [#5962](https://github.com/open-telemetry/opentelemetry-js/pull/5962) @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,
withoutTargetInfo: false,
};

private readonly _host?: string;
Expand Down Expand Up @@ -86,12 +87,16 @@ export class PrometheusExporter extends MetricReader {
const _withResourceConstantLabels =
config.withResourceConstantLabels ||
PrometheusExporter.DEFAULT_OPTIONS.withResourceConstantLabels;
const _withoutTargetInfo =
config.withoutTargetInfo ||
PrometheusExporter.DEFAULT_OPTIONS.withoutTargetInfo;
// 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,
_withResourceConstantLabels
_withResourceConstantLabels,
_withoutTargetInfo
);

this._baseUrl = `http://${this._host}:${this._port}/`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,20 @@ export class PrometheusSerializer {
private _appendTimestamp: boolean;
private _additionalAttributes: Attributes | undefined;
private _withResourceConstantLabels: RegExp | undefined;
private _withoutTargetInfo: boolean | undefined;

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

serialize(resourceMetrics: ResourceMetrics): string {
Expand Down Expand Up @@ -353,6 +356,10 @@ export class PrometheusSerializer {
}

protected _serializeResource(resource: Resource): string {
if (this._withoutTargetInfo === true) {
return '';
}

const name = 'target_info';
const help = `# HELP ${name} Target metadata`;
const type = `# TYPE ${name} gauge`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,10 @@ export interface ExporterConfig {
* @default undefined (no resource attributes are applied)
*/
withResourceConstantLabels?: RegExp;

/**
* If true, the target_info metric is not included in scraped metrics.
* @default false (target_info metric is included)
*/
withoutTargetInfo?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,14 @@ describe('PrometheusExporter', () => {
'',
]);
});

it('should omit target_info if withoutTargetInfo is true', async () => {
exporter = new PrometheusExporter({ withoutTargetInfo: true });
setup(exporter);
const body = await request('http://localhost:9464/metrics');

assert.deepStrictEqual(body.includes('target_info'), false);
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -830,5 +830,25 @@ describe('PrometheusSerializer', () => {
'target_info{env="prod",hostname="myhost",datacenter="sdc",region="europe",owner="frontend"} 1\n'
);
});

it('omits target_info if withoutTargetInfo is true', () => {
const serializer = new PrometheusSerializer(
undefined,
true,
undefined,
true
);
const result = serializer['_serializeResource'](
resourceFromAttributes({
env: 'prod',
hostname: 'myhost',
datacenter: 'sdc',
region: 'europe',
owner: 'frontend',
})
);

assert.strictEqual(result.includes('target_info'), false);
});
});
});