From 2099f3f7452b133efb63d3c74847f65e310b35c4 Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Mon, 13 Jun 2022 15:00:57 +0900 Subject: [PATCH 1/4] feat(opentelemetry-exporter-prometheus): export PrometheusSerializer --- .../packages/opentelemetry-exporter-prometheus/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/packages/opentelemetry-exporter-prometheus/src/index.ts b/experimental/packages/opentelemetry-exporter-prometheus/src/index.ts index bcf661b337c..20041cda131 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/src/index.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/src/index.ts @@ -15,4 +15,5 @@ */ export * from './PrometheusExporter'; +export * from './PrometheusSerializer'; export * from './export/types'; From d429dfb48a54d974fe3fa79fdc1a549ad4cf393a Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Mon, 13 Jun 2022 15:09:12 +0900 Subject: [PATCH 2/4] Update test to use package-level export --- .../test/PrometheusSerializer.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index 5f8656cbe5d..51733e6386e 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -27,7 +27,7 @@ import { Histogram, } from '@opentelemetry/sdk-metrics-base'; import * as sinon from 'sinon'; -import { PrometheusSerializer } from '../src/PrometheusSerializer'; +import { PrometheusSerializer } from '../src'; import { mockedHrTimeMs, mockHrTime } from './util'; const attributes = { From fb7b15da65cbf2b58d36594df26ce0b299a81259 Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Mon, 13 Jun 2022 22:37:02 +0900 Subject: [PATCH 3/4] Add changelog entry --- experimental/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/experimental/CHANGELOG.md b/experimental/CHANGELOG.md index 696643c81f9..273e918b6b6 100644 --- a/experimental/CHANGELOG.md +++ b/experimental/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to experimental packages in this project will be documented ### :rocket: (Enhancement) * feat(http-instrumentation): record exceptions in http instrumentation #3008 @luismiramirez +* feat(opentelemetry-exporter-prometheus): export PrometheusSerializer #3034 @matschaffer ### :bug: (Bug Fix) From d4581e0d7c31beef880e66921f609d278eabb78d Mon Sep 17 00:00:00 2001 From: Mat Schaffer Date: Fri, 17 Jun 2022 14:31:19 +0900 Subject: [PATCH 4/4] Make only PrometheusSerializer.serialize public Using underscore prefix to communicate intention for JS consumers. Also leverage bracket accessing to directly test private methods. --- .../src/PrometheusSerializer.ts | 16 ++++++++-------- .../test/PrometheusSerializer.test.ts | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts index 4608562df39..1b84efe08b1 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/src/PrometheusSerializer.ts @@ -179,20 +179,20 @@ export class PrometheusSerializer { serialize(resourceMetrics: ResourceMetrics): string { let str = ''; for (const scopeMetrics of resourceMetrics.scopeMetrics) { - str += this.serializeScopeMetrics(scopeMetrics); + str += this._serializeScopeMetrics(scopeMetrics); } return str; } - serializeScopeMetrics(scopeMetrics: ScopeMetrics) { + private _serializeScopeMetrics(scopeMetrics: ScopeMetrics) { let str = ''; for (const metric of scopeMetrics.metrics) { - str += this.serializeMetricData(metric) + '\n'; + str += this._serializeMetricData(metric) + '\n'; } return str; } - serializeMetricData(metricData: MetricData) { + private _serializeMetricData(metricData: MetricData) { let name = sanitizePrometheusMetricName( escapeString(metricData.descriptor.name) ); @@ -218,13 +218,13 @@ export class PrometheusSerializer { switch (dataPointType) { case DataPointType.SINGULAR: { results = metricData.dataPoints - .map(it => this.serializeSingularDataPoint(name, metricData.descriptor.type, it)) + .map(it => this._serializeSingularDataPoint(name, metricData.descriptor.type, it)) .join(''); break; } case DataPointType.HISTOGRAM: { results = metricData.dataPoints - .map(it => this.serializeHistogramDataPoint(name, metricData.descriptor.type, it)) + .map(it => this._serializeHistogramDataPoint(name, metricData.descriptor.type, it)) .join(''); break; } @@ -236,7 +236,7 @@ export class PrometheusSerializer { return `${help}\n${type}\n${results}`.trim(); } - serializeSingularDataPoint(name: string, type: InstrumentType, dataPoint: DataPoint): string { + private _serializeSingularDataPoint(name: string, type: InstrumentType, dataPoint: DataPoint): string { let results = ''; name = enforcePrometheusNamingConvention(name, type); @@ -252,7 +252,7 @@ export class PrometheusSerializer { return results; } - serializeHistogramDataPoint(name: string, type: InstrumentType, dataPoint: DataPoint): string { + private _serializeHistogramDataPoint(name: string, type: InstrumentType, dataPoint: DataPoint): string { let results = ''; name = enforcePrometheusNamingConvention(name, type); diff --git a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts index 51733e6386e..f0dc004e25a 100644 --- a/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts +++ b/experimental/packages/opentelemetry-exporter-prometheus/test/PrometheusSerializer.test.ts @@ -86,7 +86,7 @@ describe('PrometheusSerializer', () => { const pointData = metric.dataPoints as DataPoint[]; assert.strictEqual(pointData.length, 1); - const result = serializer.serializeSingularDataPoint(metric.descriptor.name, metric.descriptor.type, pointData[0]); + const result = serializer['_serializeSingularDataPoint'](metric.descriptor.name, metric.descriptor.type, pointData[0]); return result; } @@ -129,7 +129,7 @@ describe('PrometheusSerializer', () => { const pointData = metric.dataPoints as DataPoint[]; assert.strictEqual(pointData.length, 1); - const result = serializer.serializeHistogramDataPoint(metric.descriptor.name, metric.descriptor.type, pointData[0]); + const result = serializer['_serializeHistogramDataPoint'](metric.descriptor.name, metric.descriptor.type, pointData[0]); return result; } @@ -184,7 +184,7 @@ describe('PrometheusSerializer', () => { assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); const scopeMetrics = resourceMetrics.scopeMetrics[0]; - const result = serializer.serializeScopeMetrics(scopeMetrics); + const result = serializer['_serializeScopeMetrics'](scopeMetrics); return result; } @@ -236,7 +236,7 @@ describe('PrometheusSerializer', () => { assert.strictEqual(resourceMetrics.scopeMetrics[0].metrics.length, 1); const scopeMetrics = resourceMetrics.scopeMetrics[0]; - const result = serializer.serializeScopeMetrics(scopeMetrics); + const result = serializer['_serializeScopeMetrics'](scopeMetrics); return result; } @@ -284,7 +284,7 @@ describe('PrometheusSerializer', () => { const pointData = metric.dataPoints as DataPoint[]; assert.strictEqual(pointData.length, 1); - const result = serializer.serializeSingularDataPoint(metric.descriptor.name, metric.descriptor.type, pointData[0]); + const result = serializer['_serializeSingularDataPoint'](metric.descriptor.name, metric.descriptor.type, pointData[0]); return result; } @@ -323,7 +323,7 @@ describe('PrometheusSerializer', () => { const pointData = metric.dataPoints as DataPoint[]; assert.strictEqual(pointData.length, 1); - const result = serializer.serializeSingularDataPoint(metric.descriptor.name, metric.descriptor.type, pointData[0]); + const result = serializer['_serializeSingularDataPoint'](metric.descriptor.name, metric.descriptor.type, pointData[0]); return result; }