Skip to content

Commit

Permalink
feat: Collector Metric Exporter[2/x] Create CollectorMetricExporterBa…
Browse files Browse the repository at this point in the history
…se (open-telemetry#1258)

Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
davidwitten and dyladan committed Jul 6, 2020
1 parent a557b04 commit 46ce535
Show file tree
Hide file tree
Showing 11 changed files with 469 additions and 41 deletions.
1 change: 1 addition & 0 deletions packages/opentelemetry-exporter-collector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@opentelemetry/api": "^0.9.0",
"@opentelemetry/core": "^0.9.0",
"@opentelemetry/resources": "^0.9.0",
"@opentelemetry/metrics": "^0.9.0",
"@opentelemetry/tracing": "^0.9.0",
"google-protobuf": "^3.11.4",
"grpc": "^1.24.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { MetricExporter, MetricRecord } from '@opentelemetry/metrics';
import { Attributes, Logger } from '@opentelemetry/api';
import { CollectorExporterConfigBase } from './types';
import { NoopLogger, ExportResult } from '@opentelemetry/core';
import * as collectorTypes from './types';

const DEFAULT_SERVICE_NAME = 'collector-metric-exporter';

/**
* Collector Metric Exporter abstract base class
*/
export abstract class CollectorMetricExporterBase<
T extends CollectorExporterConfigBase
> implements MetricExporter {
public readonly serviceName: string;
public readonly url: string;
public readonly logger: Logger;
public readonly hostname: string | undefined;
public readonly attributes?: Attributes;
protected readonly _startTime = new Date().getTime() * 1000000;
private _isShutdown: boolean = false;

/**
* @param config
*/
constructor(config: T = {} as T) {
this.logger = config.logger || new NoopLogger();
this.serviceName = config.serviceName || DEFAULT_SERVICE_NAME;
this.url = this.getDefaultUrl(config.url);
this.attributes = config.attributes;
if (typeof config.hostname === 'string') {
this.hostname = config.hostname;
}
this.onInit();
}

/**
* Export metrics
* @param metrics
* @param resultCallback
*/
export(
metrics: MetricRecord[],
resultCallback: (result: ExportResult) => void
) {
if (this._isShutdown) {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
return;
}

this._exportMetrics(metrics)
.then(() => {
resultCallback(ExportResult.SUCCESS);
})
.catch((error: collectorTypes.ExportServiceError) => {
if (error.message) {
this.logger.error(error.message);
}
if (error.code && error.code < 500) {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
} else {
resultCallback(ExportResult.FAILED_RETRYABLE);
}
});
}

private _exportMetrics(metrics: MetricRecord[]): Promise<unknown> {
return new Promise((resolve, reject) => {
try {
this.logger.debug('metrics to be sent', metrics);
// Send metrics to [opentelemetry collector]{@link https://github.com/open-telemetry/opentelemetry-collector}
// it will use the appropriate transport layer automatically depends on platform
this.sendMetrics(metrics, resolve, reject);
} catch (e) {
reject(e);
}
});
}

/**
* Shutdown the exporter.
*/
shutdown(): void {
if (this._isShutdown) {
this.logger.debug('shutdown already started');
return;
}
this._isShutdown = true;
this.logger.debug('shutdown started');

// platform dependent
this.onShutdown();
}

abstract getDefaultUrl(url: string | undefined): string;
abstract onInit(): void;
abstract onShutdown(): void;
abstract sendMetrics(
metrics: MetricRecord[],
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import { Attributes, Logger } from '@opentelemetry/api';
import { ExportResult, NoopLogger } from '@opentelemetry/core';
import { ReadableSpan, SpanExporter } from '@opentelemetry/tracing';
import {
opentelemetryProto,
CollectorExporterError,
CollectorExporterConfigBase,
ExportServiceError,
} from './types';

const DEFAULT_SERVICE_NAME = 'collector-exporter';
Expand All @@ -34,7 +34,7 @@ export abstract class CollectorTraceExporterBase<
public readonly serviceName: string;
public readonly url: string;
public readonly logger: Logger;
public readonly hostName: string | undefined;
public readonly hostname: string | undefined;
public readonly attributes?: Attributes;
private _isShutdown: boolean = false;

Expand All @@ -44,8 +44,8 @@ export abstract class CollectorTraceExporterBase<
constructor(config: T = {} as T) {
this.serviceName = config.serviceName || DEFAULT_SERVICE_NAME;
this.url = this.getDefaultUrl(config.url);
if (typeof config.hostName === 'string') {
this.hostName = config.hostName;
if (typeof config.hostname === 'string') {
this.hostname = config.hostname;
}

this.attributes = config.attributes;
Expand Down Expand Up @@ -76,20 +76,16 @@ export abstract class CollectorTraceExporterBase<
.then(() => {
resultCallback(ExportResult.SUCCESS);
})
.catch(
(
error: opentelemetryProto.collector.trace.v1.ExportTraceServiceError
) => {
if (error.message) {
this.logger.error(error.message);
}
if (error.code && error.code < 500) {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
} else {
resultCallback(ExportResult.FAILED_RETRYABLE);
}
.catch((error: ExportServiceError) => {
if (error.message) {
this.logger.error(error.message);
}
);
if (error.code && error.code < 500) {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
} else {
resultCallback(ExportResult.FAILED_RETRYABLE);
}
});
}

private _exportSpans(spans: ReadableSpan[]): Promise<unknown> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ export class CollectorTraceExporter extends CollectorTraceExporterBase<
this.traceServiceClient.export(
exportTraceServiceRequest,
this.metadata,
(
err: collectorTypes.opentelemetryProto.collector.trace.v1.ExportTraceServiceError
) => {
(err: collectorTypes.ExportServiceError) => {
if (err) {
this.logger.error(
'exportTraceServiceRequest',
Expand Down
21 changes: 12 additions & 9 deletions packages/opentelemetry-exporter-collector/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ export namespace opentelemetryProto {
export interface ExportTraceServiceRequest {
resourceSpans: opentelemetryProto.trace.v1.ResourceSpans[];
}

export interface ExportTraceServiceError {
code: number;
details: string;
metadata: { [key: string]: unknown };
message: string;
stack: string;
}
}
}

Expand Down Expand Up @@ -171,11 +163,22 @@ export interface CollectorExporterError {
stack?: string;
}

/**
* Interface for handling export service errors
*/
export interface ExportServiceError {
code: number;
details: string;
metadata: { [key: string]: unknown };
message: string;
stack: string;
}

/**
* Collector Exporter base config
*/
export interface CollectorExporterConfigBase {
hostName?: string;
hostname?: string;
logger?: Logger;
serviceName?: string;
attributes?: Attributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../helper';
const sendBeacon = navigator.sendBeacon;

describe('CollectorExporter - web', () => {
describe('CollectorTraceExporter - web', () => {
let collectorTraceExporter: CollectorTraceExporter;
let collectorExporterConfig: collectorTypes.CollectorExporterConfigBrowser;
let spyOpen: any;
Expand All @@ -56,7 +56,7 @@ describe('CollectorExporter - web', () => {
describe('export', () => {
beforeEach(() => {
collectorExporterConfig = {
hostName: 'foo',
hostname: 'foo',
logger: new NoopLogger(),
serviceName: 'bar',
attributes: {},
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('CollectorExporter - web', () => {
});
});

describe('CollectorExporter - browser (getDefaultUrl)', () => {
describe('CollectorTraceExporter - browser (getDefaultUrl)', () => {
it('should default to v1/trace', done => {
const collectorExporter = new CollectorTraceExporter({});
setTimeout(() => {
Expand Down
Loading

0 comments on commit 46ce535

Please sign in to comment.