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: Collector Metric Exporter[2/x] Create CollectorMetricExporterBase #1258

Merged
merged 18 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 11 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
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';
davidwitten marked this conversation as resolved.
Show resolved Hide resolved
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;
davidwitten marked this conversation as resolved.
Show resolved Hide resolved
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;
davidwitten marked this conversation as resolved.
Show resolved Hide resolved
}
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 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
19 changes: 11 additions & 8 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,6 +163,17 @@ 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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* 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 { ExportResult, NoopLogger } from '@opentelemetry/core';
import * as assert from 'assert';
import * as sinon from 'sinon';
import { CollectorMetricExporterBase } from '../../src/CollectorMetricExporterBase';
import { CollectorExporterConfigBase } from '../../src/types';
import { MetricRecord } from '@opentelemetry/metrics';
import { mockCounter, mockObserver } from '../helper';

type CollectorExporterConfig = CollectorExporterConfigBase;
class CollectorMetricExporter extends CollectorMetricExporterBase<
CollectorExporterConfig
> {
onInit() {}
onShutdown() {}
sendMetrics() {}
getDefaultUrl(url: string) {
return url || '';
}
}

describe('CollectorMetricExporter - common', () => {
let collectorExporter: CollectorMetricExporter;
let collectorExporterConfig: CollectorExporterConfig;
let metrics: MetricRecord[];
describe('constructor', () => {
let onInitSpy: any;

beforeEach(() => {
onInitSpy = sinon.stub(CollectorMetricExporter.prototype, 'onInit');
collectorExporterConfig = {
hostName: 'foo',
davidwitten marked this conversation as resolved.
Show resolved Hide resolved
logger: new NoopLogger(),
serviceName: 'bar',
attributes: {},
url: 'http://foo.bar.com',
};
collectorExporter = new CollectorMetricExporter(collectorExporterConfig);
metrics = [];
metrics.push(Object.assign({}, mockCounter));
metrics.push(Object.assign({}, mockObserver));
});

afterEach(() => {
onInitSpy.restore();
});

it('should create an instance', () => {
assert.ok(typeof collectorExporter !== 'undefined');
});

it('should call onInit', () => {
assert.strictEqual(onInitSpy.callCount, 1);
});

describe('when config contains certain params', () => {
it('should set hostName', () => {
assert.strictEqual(collectorExporter.hostName, 'foo');
davidwitten marked this conversation as resolved.
Show resolved Hide resolved
});

it('should set serviceName', () => {
assert.strictEqual(collectorExporter.serviceName, 'bar');
});

it('should set url', () => {
assert.strictEqual(collectorExporter.url, 'http://foo.bar.com');
});

it('should set logger', () => {
assert.ok(collectorExporter.logger === collectorExporterConfig.logger);
});
});

describe('when config is missing certain params', () => {
beforeEach(() => {
collectorExporter = new CollectorMetricExporter();
});

it('should set default serviceName', () => {
assert.strictEqual(
collectorExporter.serviceName,
'collector-metric-exporter'
);
});

it('should set default logger', () => {
assert.ok(collectorExporter.logger instanceof NoopLogger);
});
});
});

describe('export', () => {
let spySend: any;
beforeEach(() => {
spySend = sinon.stub(CollectorMetricExporter.prototype, 'sendMetrics');
collectorExporter = new CollectorMetricExporter(collectorExporterConfig);
});
afterEach(() => {
spySend.restore();
});

it('should export metrics as collectorTypes.Metrics', done => {
collectorExporter.export(metrics, () => {});
setTimeout(() => {
const metric1 = spySend.args[0][0][0] as MetricRecord;
assert.deepStrictEqual(metrics[0], metric1);
const metric2 = spySend.args[0][0][1] as MetricRecord;
assert.deepStrictEqual(metrics[1], metric2);
done();
});
assert.strictEqual(spySend.callCount, 1);
});

describe('when exporter is shutdown', () => {
it('should not export anything but return callback with code "FailedNotRetryable"', () => {
collectorExporter.shutdown();
spySend.resetHistory();

const callbackSpy = sinon.spy();
collectorExporter.export(metrics, callbackSpy);
const returnCode = callbackSpy.args[0][0];

assert.strictEqual(
returnCode,
ExportResult.FAILED_NOT_RETRYABLE,
'return value is wrong'
);
assert.strictEqual(spySend.callCount, 0, 'should not call send');
});
});
});

describe('shutdown', () => {
let onShutdownSpy: any;
beforeEach(() => {
onShutdownSpy = sinon.stub(
CollectorMetricExporter.prototype,
'onShutdown'
);
collectorExporterConfig = {
hostName: 'foo',
davidwitten marked this conversation as resolved.
Show resolved Hide resolved
logger: new NoopLogger(),
serviceName: 'bar',
attributes: {},
url: 'http://foo.bar.com',
};
collectorExporter = new CollectorMetricExporter(collectorExporterConfig);
});
afterEach(() => {
onShutdownSpy.restore();
});

it('should call onShutdown', done => {
collectorExporter.shutdown();
setTimeout(() => {
assert.equal(onShutdownSpy.callCount, 1);
done();
});
});
});
});
Loading