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: exporter collector TLS option #1063

Merged
merged 22 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
28 changes: 28 additions & 0 deletions packages/opentelemetry-exporter-collector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ provider.register();

```

By default, plaintext connection is used. In order to use TLS in Node.js, provide `credentials` option like so:
```js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please mark this is for node only, not browser

const fs = require('fs');
const grpc = require('grpc');
const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { CollectorExporter } = require('@opentelemetry/exporter-collector');

const collectorOptions = {
serviceName: 'basic-service',
url: '<opentelemetry-collector-url>', // url is optional and can be omitted - default is http://localhost:55678/v1/trace
credentials: grpc.credentials.createSsl(
fs.readFileSync('./ca.crt'),
fs.readFileSync('./client.key'),
fs.readFileSync('./client.crt')
)
};

const provider = new BasicTracerProvider();
const exporter = new CollectorExporter(collectorOptions);
provider.addSpanProcessor(new SimpleSpanProcessor(exporter));

provider.register();
```

To see how to generate credentials, you can refer to the script used to generate certificates for tests [here](./test/certs/regenerate.sh)

Note, that this will only work if TLS is also configured on the server.

## Running opentelemetry-collector locally to see the traces
1. Go to examples/basic-tracer-node
2. run `npm run collector:docker:ot`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@
* limitations under the License.
*/

import { Attributes, Logger } from '@opentelemetry/api';
import { ExportResult, NoopLogger } from '@opentelemetry/core';
import { ReadableSpan, SpanExporter } from '@opentelemetry/tracing';
import { Attributes, Logger } from '@opentelemetry/api';
import { onInit, onShutdown, sendSpans } from './platform/index';
import { opentelemetryProto } from './types';
import { opentelemetryProto, CollectorExporterError } from './types';

/**
* Collector Exporter Config
* Collector Exporter base config
*/
export interface CollectorExporterConfig {
export interface CollectorExporterConfigBase {
hostName?: string;
logger?: Logger;
serviceName?: string;
Expand All @@ -35,20 +34,22 @@ const DEFAULT_SERVICE_NAME = 'collector-exporter';
const DEFAULT_COLLECTOR_URL = 'http://localhost:55678/v1/trace';

/**
* Collector Exporter
* Collector Exporter abstract base class
*/
export class CollectorExporter implements SpanExporter {
readonly serviceName: string;
readonly url: string;
readonly logger: Logger;
readonly hostName: string | undefined;
readonly attributes?: Attributes;
export abstract class CollectorExporterBase<
T extends CollectorExporterConfigBase
> implements SpanExporter {
public readonly serviceName: string;
public readonly url: string;
public readonly logger: Logger;
public readonly hostName: string | undefined;
public readonly attributes?: Attributes;
private _isShutdown: boolean = false;

/**
* @param config
*/
constructor(config: CollectorExporterConfig = {}) {
constructor(config: T = {} as T) {
this.serviceName = config.serviceName || DEFAULT_SERVICE_NAME;
this.url = config.url || DEFAULT_COLLECTOR_URL;
if (typeof config.hostName === 'string') {
Expand All @@ -62,7 +63,7 @@ export class CollectorExporter implements SpanExporter {
this.shutdown = this.shutdown.bind(this);

// platform dependent
onInit(this);
this.onInit(config);
}

/**
Expand Down Expand Up @@ -105,7 +106,7 @@ export class CollectorExporter implements SpanExporter {
this.logger.debug('spans to be sent', spans);
// Send spans to [opentelemetry collector]{@link https://github.com/open-telemetry/opentelemetry-collector}
// it will use the appropriate transport layer automatically depends on platform
sendSpans(spans, resolve, reject, this);
this.sendSpans(spans, resolve, reject);
} catch (e) {
reject(e);
}
Expand All @@ -124,6 +125,14 @@ export class CollectorExporter implements SpanExporter {
this.logger.debug('shutdown started');

// platform dependent
onShutdown(this);
this.onShutdown();
}

abstract onShutdown(): void;
abstract onInit(config: T): void;
abstract sendSpans(
spans: ReadableSpan[],
onSuccess: () => void,
onError: (error: CollectorExporterError) => void
): void;
}
4 changes: 2 additions & 2 deletions packages/opentelemetry-exporter-collector/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* Copyright 2019, OpenTelemetry Authors
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './CollectorExporter';
export * from './platform/node';
obecny marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*!
* Copyright 2020, 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 {
CollectorExporterBase,
CollectorExporterConfigBase,
} from '../../CollectorExporterBase';
import { ReadableSpan } from '@opentelemetry/tracing';
import { toCollectorExportTraceServiceRequest } from '../../transform';
import * as collectorTypes from '../../types';

export interface CollectorExporterConfig extends CollectorExporterConfigBase {}

/**
* Collector Exporter for Web
*/
export class CollectorExporter extends CollectorExporterBase<
CollectorExporterConfig
> {
onInit(): void {
window.addEventListener('unload', this.shutdown);
}

onShutdown(): void {
window.removeEventListener('unload', this.shutdown);
}

sendSpans(
spans: ReadableSpan[],
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
) {
const exportTraceServiceRequest = toCollectorExportTraceServiceRequest(
spans,
this
);

const body = JSON.stringify(exportTraceServiceRequest);

if (typeof navigator.sendBeacon === 'function') {
this._sendSpansWithBeacon(body, onSuccess, onError);
} else {
this._sendSpansWithXhr(body, onSuccess, onError);
}
}

/**
* send spans using browser navigator.sendBeacon
* @param body
* @param onSuccess
* @param onError
*/
private _sendSpansWithBeacon(
body: string,
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
) {
if (navigator.sendBeacon(this.url, body)) {
this.logger.debug('sendBeacon - can send', body);
onSuccess();
} else {
this.logger.error('sendBeacon - cannot send', body);
onError({});
}
}

/**
* function to send spans using browser XMLHttpRequest
* used when navigator.sendBeacon is not available
* @param body
* @param onSuccess
* @param onError
*/
private _sendSpansWithXhr(
body: string,
onSuccess: () => void,
onError: (error: collectorTypes.CollectorExporterError) => void
) {
const xhr = new XMLHttpRequest();
xhr.open('POST', this.url);
xhr.setRequestHeader(collectorTypes.OT_REQUEST_HEADER, '1');
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(body);

xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status >= 200 && xhr.status <= 299) {
this.logger.debug('xhr success', body);
onSuccess();
} else {
this.logger.error('body', body);
this.logger.error('xhr error', xhr);
onError({
code: xhr.status,
message: xhr.responseText,
});
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* limitations under the License.
*/

export * from './sendSpans';
export * from './CollectorExporter';

This file was deleted.

Loading