Skip to content

Commit

Permalink
feat(otlp-transformer)!: accept ResourceMetrics instead of ResoruceMe…
Browse files Browse the repository at this point in the history
…trics[] in metrics serializers (#5159)
  • Loading branch information
pichlermarc authored Nov 20, 2024
1 parent b7c03d2 commit 91b9abd
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 143 deletions.
3 changes: 3 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ All notable changes to experimental packages in this project will be documented
* This type is scheduled for removal in a future version of this package, please treat all exporters as `SpanExporter`, `PushMetricExporter` or `LogRecordExporter`, based on their respective type.
* feat(otlp-grpc-exporter-base)!: collapse base classes into one [#5031](https://github.com/open-telemetry/opentelemetry-js/pull/5031)
* `OTLPGRPCExporterNodeBase` has been removed in favor of a platform-agnostic implementation (`OTLPExporterBase` from `@opentelemetry/otlp-exporter-base`)
* feat(otlp-transformer)!: accept `ResourceMetrics` in serializers instead of `ResourceMetrics[]`
* (user-facing): `ProtobufMetricsSerializer` now only accepts `ResourceMetrics` instead of `ResourceMetrics[]` to align with `PushMetricExporter` requirements
* (user-facing): `JsonMetricsSerializer` now only accepts `ResourceMetrics` instead of `ResourceMetrics[]` to align with `PushMetricExporter` requirements

### :rocket: (Enhancement)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { ExportResult, getEnv } from '@opentelemetry/core';
import { getEnv } from '@opentelemetry/core';
import {
AggregationTemporality,
AggregationTemporalitySelector,
Expand All @@ -28,7 +28,10 @@ import {
AggregationTemporalityPreference,
OTLPMetricExporterOptions,
} from './OTLPMetricExporterOptions';
import { IOtlpExportDelegate } from '@opentelemetry/otlp-exporter-base';
import {
IOtlpExportDelegate,
OTLPExporterBase,
} from '@opentelemetry/otlp-exporter-base';
import { diag } from '@opentelemetry/api';

export const CumulativeTemporalitySelector: AggregationTemporalitySelector =
Expand Down Expand Up @@ -117,37 +120,24 @@ function chooseAggregationSelector(
}
}

export class OTLPMetricExporterBase implements PushMetricExporter {
public _delegate: IOtlpExportDelegate<ResourceMetrics[]>;
private _aggregationTemporalitySelector: AggregationTemporalitySelector;
private _aggregationSelector: AggregationSelector;
export class OTLPMetricExporterBase
extends OTLPExporterBase<ResourceMetrics>
implements PushMetricExporter
{
private readonly _aggregationTemporalitySelector: AggregationTemporalitySelector;
private readonly _aggregationSelector: AggregationSelector;

constructor(
delegate: IOtlpExportDelegate<ResourceMetrics[]>,
delegate: IOtlpExportDelegate<ResourceMetrics>,
config?: OTLPMetricExporterOptions
) {
this._delegate = delegate;
super(delegate);
this._aggregationSelector = chooseAggregationSelector(config);
this._aggregationTemporalitySelector = chooseTemporalitySelector(
config?.temporalityPreference
);
}

export(
metrics: ResourceMetrics,
resultCallback: (result: ExportResult) => void
): void {
this._delegate.export([metrics], resultCallback);
}

async shutdown(): Promise<void> {
await this._delegate.shutdown();
}

forceFlush(): Promise<void> {
return this._delegate.forceFlush();
}

selectAggregation(instrumentType: InstrumentType): Aggregation {
return this._aggregationSelector(instrumentType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@
const testsContext = require.context('../browser', true, /test$/);
testsContext.keys().forEach(testsContext);

const testsContextCommon = require.context('../common', true, /test$/);
testsContextCommon.keys().forEach(testsContextCommon);

const srcContext = require.context('.', true, /src$/);
srcContext.keys().forEach(srcContext);

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export const JsonTraceSerializer: ISerializer<
};

export const JsonMetricsSerializer: ISerializer<
ResourceMetrics[],
ResourceMetrics,
IExportMetricsServiceResponse
> = {
serializeRequest: (arg: ResourceMetrics[]) => {
const request = createExportMetricsServiceRequest(arg, {
serializeRequest: (arg: ResourceMetrics) => {
const request = createExportMetricsServiceRequest([arg], {
useLongBits: false,
});
const encoder = new TextEncoder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ export const ProtobufLogsSerializer: ISerializer<
};

export const ProtobufMetricsSerializer: ISerializer<
ResourceMetrics[],
ResourceMetrics,
IExportMetricsServiceResponse
> = {
serializeRequest: (arg: ResourceMetrics[]) => {
const request = createExportMetricsServiceRequest(arg);
serializeRequest: (arg: ResourceMetrics) => {
const request = createExportMetricsServiceRequest([arg]);
return metricsRequestType.encode(request).finish();
},
deserializeResponse: (arg: Uint8Array) => {
Expand Down
12 changes: 6 additions & 6 deletions experimental/packages/otlp-transformer/test/metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -788,11 +788,11 @@ describe('Metrics', () => {

describe('ProtobufMetricsSerializer', function () {
it('serializes an export request', () => {
const serialized = ProtobufMetricsSerializer.serializeRequest([
const serialized = ProtobufMetricsSerializer.serializeRequest(
createResourceMetrics([
createCounterData(10, AggregationTemporality.DELTA),
]),
]);
])
);
assert.ok(serialized, 'serialized response is undefined');
const decoded =
root.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest.decode(
Expand Down Expand Up @@ -873,11 +873,11 @@ describe('Metrics', () => {

describe('JsonMetricsSerializer', function () {
it('serializes an export request', () => {
const serialized = JsonMetricsSerializer.serializeRequest([
const serialized = JsonMetricsSerializer.serializeRequest(
createResourceMetrics([
createCounterData(10, AggregationTemporality.DELTA),
]),
]);
])
);

const decoder = new TextDecoder();
const expected = {
Expand Down

0 comments on commit 91b9abd

Please sign in to comment.