Skip to content

Commit 46ce535

Browse files
davidwittendyladan
andauthored
feat: Collector Metric Exporter[2/x] Create CollectorMetricExporterBase (#1258)
Co-authored-by: Daniel Dyla <[email protected]>
1 parent a557b04 commit 46ce535

11 files changed

+469
-41
lines changed

packages/opentelemetry-exporter-collector/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"@opentelemetry/api": "^0.9.0",
8888
"@opentelemetry/core": "^0.9.0",
8989
"@opentelemetry/resources": "^0.9.0",
90+
"@opentelemetry/metrics": "^0.9.0",
9091
"@opentelemetry/tracing": "^0.9.0",
9192
"google-protobuf": "^3.11.4",
9293
"grpc": "^1.24.2"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { MetricExporter, MetricRecord } from '@opentelemetry/metrics';
18+
import { Attributes, Logger } from '@opentelemetry/api';
19+
import { CollectorExporterConfigBase } from './types';
20+
import { NoopLogger, ExportResult } from '@opentelemetry/core';
21+
import * as collectorTypes from './types';
22+
23+
const DEFAULT_SERVICE_NAME = 'collector-metric-exporter';
24+
25+
/**
26+
* Collector Metric Exporter abstract base class
27+
*/
28+
export abstract class CollectorMetricExporterBase<
29+
T extends CollectorExporterConfigBase
30+
> implements MetricExporter {
31+
public readonly serviceName: string;
32+
public readonly url: string;
33+
public readonly logger: Logger;
34+
public readonly hostname: string | undefined;
35+
public readonly attributes?: Attributes;
36+
protected readonly _startTime = new Date().getTime() * 1000000;
37+
private _isShutdown: boolean = false;
38+
39+
/**
40+
* @param config
41+
*/
42+
constructor(config: T = {} as T) {
43+
this.logger = config.logger || new NoopLogger();
44+
this.serviceName = config.serviceName || DEFAULT_SERVICE_NAME;
45+
this.url = this.getDefaultUrl(config.url);
46+
this.attributes = config.attributes;
47+
if (typeof config.hostname === 'string') {
48+
this.hostname = config.hostname;
49+
}
50+
this.onInit();
51+
}
52+
53+
/**
54+
* Export metrics
55+
* @param metrics
56+
* @param resultCallback
57+
*/
58+
export(
59+
metrics: MetricRecord[],
60+
resultCallback: (result: ExportResult) => void
61+
) {
62+
if (this._isShutdown) {
63+
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
64+
return;
65+
}
66+
67+
this._exportMetrics(metrics)
68+
.then(() => {
69+
resultCallback(ExportResult.SUCCESS);
70+
})
71+
.catch((error: collectorTypes.ExportServiceError) => {
72+
if (error.message) {
73+
this.logger.error(error.message);
74+
}
75+
if (error.code && error.code < 500) {
76+
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
77+
} else {
78+
resultCallback(ExportResult.FAILED_RETRYABLE);
79+
}
80+
});
81+
}
82+
83+
private _exportMetrics(metrics: MetricRecord[]): Promise<unknown> {
84+
return new Promise((resolve, reject) => {
85+
try {
86+
this.logger.debug('metrics to be sent', metrics);
87+
// Send metrics to [opentelemetry collector]{@link https://github.com/open-telemetry/opentelemetry-collector}
88+
// it will use the appropriate transport layer automatically depends on platform
89+
this.sendMetrics(metrics, resolve, reject);
90+
} catch (e) {
91+
reject(e);
92+
}
93+
});
94+
}
95+
96+
/**
97+
* Shutdown the exporter.
98+
*/
99+
shutdown(): void {
100+
if (this._isShutdown) {
101+
this.logger.debug('shutdown already started');
102+
return;
103+
}
104+
this._isShutdown = true;
105+
this.logger.debug('shutdown started');
106+
107+
// platform dependent
108+
this.onShutdown();
109+
}
110+
111+
abstract getDefaultUrl(url: string | undefined): string;
112+
abstract onInit(): void;
113+
abstract onShutdown(): void;
114+
abstract sendMetrics(
115+
metrics: MetricRecord[],
116+
onSuccess: () => void,
117+
onError: (error: collectorTypes.CollectorExporterError) => void
118+
): void;
119+
}

packages/opentelemetry-exporter-collector/src/CollectorTraceExporterBase.ts

+13-17
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import { Attributes, Logger } from '@opentelemetry/api';
1818
import { ExportResult, NoopLogger } from '@opentelemetry/core';
1919
import { ReadableSpan, SpanExporter } from '@opentelemetry/tracing';
2020
import {
21-
opentelemetryProto,
2221
CollectorExporterError,
2322
CollectorExporterConfigBase,
23+
ExportServiceError,
2424
} from './types';
2525

2626
const DEFAULT_SERVICE_NAME = 'collector-exporter';
@@ -34,7 +34,7 @@ export abstract class CollectorTraceExporterBase<
3434
public readonly serviceName: string;
3535
public readonly url: string;
3636
public readonly logger: Logger;
37-
public readonly hostName: string | undefined;
37+
public readonly hostname: string | undefined;
3838
public readonly attributes?: Attributes;
3939
private _isShutdown: boolean = false;
4040

@@ -44,8 +44,8 @@ export abstract class CollectorTraceExporterBase<
4444
constructor(config: T = {} as T) {
4545
this.serviceName = config.serviceName || DEFAULT_SERVICE_NAME;
4646
this.url = this.getDefaultUrl(config.url);
47-
if (typeof config.hostName === 'string') {
48-
this.hostName = config.hostName;
47+
if (typeof config.hostname === 'string') {
48+
this.hostname = config.hostname;
4949
}
5050

5151
this.attributes = config.attributes;
@@ -76,20 +76,16 @@ export abstract class CollectorTraceExporterBase<
7676
.then(() => {
7777
resultCallback(ExportResult.SUCCESS);
7878
})
79-
.catch(
80-
(
81-
error: opentelemetryProto.collector.trace.v1.ExportTraceServiceError
82-
) => {
83-
if (error.message) {
84-
this.logger.error(error.message);
85-
}
86-
if (error.code && error.code < 500) {
87-
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
88-
} else {
89-
resultCallback(ExportResult.FAILED_RETRYABLE);
90-
}
79+
.catch((error: ExportServiceError) => {
80+
if (error.message) {
81+
this.logger.error(error.message);
9182
}
92-
);
83+
if (error.code && error.code < 500) {
84+
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
85+
} else {
86+
resultCallback(ExportResult.FAILED_RETRYABLE);
87+
}
88+
});
9389
}
9490

9591
private _exportSpans(spans: ReadableSpan[]): Promise<unknown> {

packages/opentelemetry-exporter-collector/src/platform/node/CollectorTraceExporter.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ export class CollectorTraceExporter extends CollectorTraceExporterBase<
112112
this.traceServiceClient.export(
113113
exportTraceServiceRequest,
114114
this.metadata,
115-
(
116-
err: collectorTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceError
117-
) => {
115+
(err: collectorTypes.ExportServiceError) => {
118116
if (err) {
119117
this.logger.error(
120118
'exportTraceServiceRequest',

packages/opentelemetry-exporter-collector/src/types.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@ export namespace opentelemetryProto {
3232
export interface ExportTraceServiceRequest {
3333
resourceSpans: opentelemetryProto.trace.v1.ResourceSpans[];
3434
}
35-
36-
export interface ExportTraceServiceError {
37-
code: number;
38-
details: string;
39-
metadata: { [key: string]: unknown };
40-
message: string;
41-
stack: string;
42-
}
4335
}
4436
}
4537

@@ -171,11 +163,22 @@ export interface CollectorExporterError {
171163
stack?: string;
172164
}
173165

166+
/**
167+
* Interface for handling export service errors
168+
*/
169+
export interface ExportServiceError {
170+
code: number;
171+
details: string;
172+
metadata: { [key: string]: unknown };
173+
message: string;
174+
stack: string;
175+
}
176+
174177
/**
175178
* Collector Exporter base config
176179
*/
177180
export interface CollectorExporterConfigBase {
178-
hostName?: string;
181+
hostname?: string;
179182
logger?: Logger;
180183
serviceName?: string;
181184
attributes?: Attributes;

packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from '../helper';
3131
const sendBeacon = navigator.sendBeacon;
3232

33-
describe('CollectorExporter - web', () => {
33+
describe('CollectorTraceExporter - web', () => {
3434
let collectorTraceExporter: CollectorTraceExporter;
3535
let collectorExporterConfig: collectorTypes.CollectorExporterConfigBrowser;
3636
let spyOpen: any;
@@ -56,7 +56,7 @@ describe('CollectorExporter - web', () => {
5656
describe('export', () => {
5757
beforeEach(() => {
5858
collectorExporterConfig = {
59-
hostName: 'foo',
59+
hostname: 'foo',
6060
logger: new NoopLogger(),
6161
serviceName: 'bar',
6262
attributes: {},
@@ -326,7 +326,7 @@ describe('CollectorExporter - web', () => {
326326
});
327327
});
328328

329-
describe('CollectorExporter - browser (getDefaultUrl)', () => {
329+
describe('CollectorTraceExporter - browser (getDefaultUrl)', () => {
330330
it('should default to v1/trace', done => {
331331
const collectorExporter = new CollectorTraceExporter({});
332332
setTimeout(() => {

0 commit comments

Comments
 (0)