Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(otlp-transformer)!: accept ResourceMetrics instead of ResoruceMetrics[] in metrics serializers #5159

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
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