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

grpc for node and support for new proto format for node and browser #901

Merged
merged 20 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 9 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 .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "packages/opentelemetry-exporter-collector/src/platform/node/protos"]
obecny marked this conversation as resolved.
Show resolved Hide resolved
path = packages/opentelemetry-exporter-collector/src/platform/node/protos
url = [email protected]:open-telemetry/opentelemetry-proto.git
30 changes: 30 additions & 0 deletions packages/opentelemetry-core/src/platform/browser/hex-to-bytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*!
* 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.
*/

/**
* converts id string into Uint8Array
* @param hexStr - id of span
*/
export function hexToBytes(hexStr: string): any {
obecny marked this conversation as resolved.
Show resolved Hide resolved
const hexStrLen = hexStr.length;
const arr = [];
for (let i = 0; i < hexStrLen; i += 2) {
const hexPair = hexStr.substring(i, i + 2);
const hexVal = parseInt(hexPair, 16);
arr.push(hexVal);
}
return new Uint8Array(arr);
}
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/platform/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './id';
export * from './performance';
export * from './timer-util';
export * from './hex-to-base64';
export * from './hex-to-bytes';
31 changes: 31 additions & 0 deletions packages/opentelemetry-core/src/platform/node/hex-to-bytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* 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.
*/

/**
* converts id string into bytes
* @param hexStr - id of span
*/
export function hexToBytes(hexStr: string): Uint8Array {
obecny marked this conversation as resolved.
Show resolved Hide resolved
const hexStrLen = hexStr.length;
let hexAsciiCharsStr = '';
for (let i = 0; i < hexStrLen; i += 2) {
const hexPair = hexStr.substring(i, i + 2);
const hexVal = parseInt(hexPair, 16);
hexAsciiCharsStr += String.fromCharCode(hexVal);
}

return Buffer.from(hexAsciiCharsStr, 'ascii');
mayurkale22 marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions packages/opentelemetry-core/src/platform/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './id';
export * from './performance';
export * from './timer-util';
export * from './hex-to-base64';
export * from './hex-to-bytes';
obecny marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/opentelemetry-exporter-collector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const { BasicTracerProvider, SimpleSpanProcessor } = require('@opentelemetry/tra
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
};

Expand Down
11 changes: 9 additions & 2 deletions packages/opentelemetry-exporter-collector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
"codecov:browser": "nyc report --reporter=json && codecov -f coverage/*.json -p ../../",
"precompile": "tsc --version",
"compile": "npm run version:update && tsc -p .",
"postcompile": "npm run submodule && npm run protos:copy",
"prepare": "npm run compile",
"protos:copy": "cpx src/platform/node/protos/opentelemetry/**/*.* build/src/platform/node/protos/opentelemetry",
"submodule": "git submodule sync --recursive && git submodule update --init --recursive",
"tdd": "npm run test -- --watch-extensions ts --watch",
"tdd:browser": "karma start",
"test": "nyc ts-mocha -p tsconfig.json 'test/**/*.test.ts' --exclude 'test/browser/**/*.ts'",
"test:browser": "nyc karma start --single-run",
"version:update": "node ../../scripts/version-update.js",
"watch": "tsc -w"
"watch": "npm run protos:copy && tsc -w"
},
"keywords": [
"opentelemetry",
Expand Down Expand Up @@ -56,6 +59,7 @@
"@types/webpack-env": "1.13.9",
"babel-loader": "^8.0.6",
"codecov": "^3.1.0",
"cpx": "^1.5.0",
"gts": "^1.0.0",
"istanbul-instrumenter-loader": "^3.0.1",
"karma": "^4.4.1",
Expand All @@ -79,10 +83,13 @@
"webpack-merge": "^4.2.2"
},
"dependencies": {
"@grpc/proto-loader": "^0.5.3",
"@opentelemetry/api": "^0.5.1",
"@opentelemetry/base": "^0.5.1",
"@opentelemetry/core": "^0.5.1",
"@opentelemetry/resources": "^0.5.1",
"@opentelemetry/tracing": "^0.5.1"
"@opentelemetry/tracing": "^0.5.1",
"google-protobuf": "^3.11.4",
"grpc": "^1.24.2"
}
}
27 changes: 11 additions & 16 deletions packages/opentelemetry-exporter-collector/src/CollectorExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ import { ExportResult } from '@opentelemetry/base';
import { NoopLogger } from '@opentelemetry/core';
import { ReadableSpan, SpanExporter } from '@opentelemetry/tracing';
import { Attributes, Logger } from '@opentelemetry/api';
import * as collectorTypes from './types';
import { toCollectorSpan, toCollectorResource } from './transform';
import { onInit, onShutdown, sendSpans } from './platform/index';
import { Resource } from '@opentelemetry/resources';
import { CollectorExporterError } from './types';

/**
* Collector Exporter Config
Expand Down Expand Up @@ -65,7 +63,7 @@ export class CollectorExporter implements SpanExporter {
this.shutdown = this.shutdown.bind(this);

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

/**
Expand All @@ -81,12 +79,16 @@ export class CollectorExporter implements SpanExporter {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
return;
}

this._exportSpans(spans)
.then(() => {
resultCallback(ExportResult.SUCCESS);
})
.catch((status: number = 0) => {
if (status < 500) {
.catch((error: CollectorExporterError) => {
if (error.message) {
this.logger.error(error.message);
}
if (error.code && error.code < 500) {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
} else {
resultCallback(ExportResult.FAILED_RETRYABLE);
Expand All @@ -97,17 +99,10 @@ export class CollectorExporter implements SpanExporter {
private _exportSpans(spans: ReadableSpan[]): Promise<unknown> {
return new Promise((resolve, reject) => {
try {
const spansToBeSent: collectorTypes.Span[] = spans.map(span =>
toCollectorSpan(span)
);
this.logger.debug('spans to be sent', spansToBeSent);
const resource = toCollectorResource(
spansToBeSent.length > 0 ? spans[0].resource : Resource.empty()
);

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(spansToBeSent, resolve, reject, this, resource);
sendSpans(spans, resolve, reject, this);
} catch (e) {
reject(e);
}
Expand All @@ -126,6 +121,6 @@ export class CollectorExporter implements SpanExporter {
this.logger.debug('shutdown started');

// platform dependent
onShutdown(this.shutdown);
onShutdown(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,31 @@
* limitations under the License.
*/

import * as core from '@opentelemetry/core';
import { Logger } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { ReadableSpan } from '@opentelemetry/tracing';
import { CollectorExporter } from '../../CollectorExporter';
import {
toCollectorExportTraceServiceRequest,
toCollectorResource,
toCollectorSpan,
} from '../../transform';
import * as collectorTypes from '../../types';
import { VERSION } from '../../version';

/**
* function that is called once when {@link ExporterCollector} is initialised
* @param shutdownF shutdown method of {@link ExporterCollector}
* @param collectorExporter CollectorExporter {@link ExporterCollector}
*/
export function onInit(shutdownF: EventListener) {
window.addEventListener('unload', shutdownF);
export function onInit(collectorExporter: CollectorExporter) {
window.addEventListener('unload', collectorExporter.shutdown);
}

/**
* function to be called once when {@link ExporterCollector} is shutdown
* @param shutdownF - shutdown method of {@link ExporterCollector}
* @param collectorExporter CollectorExporter {@link ExporterCollector}
*/
export function onShutdown(shutdownF: EventListener) {
window.removeEventListener('unload', shutdownF);
export function onShutdown(collectorExporter: CollectorExporter) {
window.removeEventListener('unload', collectorExporter.shutdown);
}

/**
Expand All @@ -43,34 +48,23 @@ export function onShutdown(shutdownF: EventListener) {
* @param onSuccess
* @param onError
* @param collectorExporter
* @param resource
*/
export function sendSpans(
spans: collectorTypes.Span[],
spans: ReadableSpan[],
onSuccess: () => void,
onError: (status?: number) => void,
collectorExporter: CollectorExporter,
resource: collectorTypes.Resource
onError: (error: collectorTypes.CollectorExporterError) => void,
collectorExporter: CollectorExporter
) {
const exportTraceServiceRequest: collectorTypes.ExportTraceServiceRequest = {
node: {
identifier: {
hostName: collectorExporter.hostName || window.location.host,
startTimestamp: core.hrTimeToTimeStamp(core.hrTime()),
},
libraryInfo: {
language: collectorTypes.LibraryInfoLanguage.WEB_JS,
coreLibraryVersion: core.VERSION,
exporterVersion: VERSION,
},
serviceInfo: {
name: collectorExporter.serviceName,
},
attributes: collectorExporter.attributes,
},
resource,
spans,
};
const spansToBeSent: collectorTypes.opentelemetryProto.trace.v1.Span[] = spans.map(
span => toCollectorSpan(span)
);
const resource: Resource | undefined =
spans.length > 0 ? spans[0].resource : Resource.empty();

const exportTraceServiceRequest = toCollectorExportTraceServiceRequest(
spansToBeSent,
toCollectorResource(resource)
);

const body = JSON.stringify(exportTraceServiceRequest);

Expand Down Expand Up @@ -104,7 +98,7 @@ export function sendSpans(
function sendSpansWithBeacon(
body: string,
onSuccess: () => void,
onError: (status?: number) => void,
onError: (error: collectorTypes.CollectorExporterError) => void,
logger: Logger,
collectorUrl: string
) {
Expand All @@ -113,7 +107,7 @@ function sendSpansWithBeacon(
onSuccess();
} else {
logger.error('sendBeacon - cannot send', body);
onError();
onError({});
obecny marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -127,15 +121,16 @@ function sendSpansWithBeacon(
* @param collectorUrl
*/
function sendSpansWithXhr(
body: string,
body: any,
onSuccess: () => void,
onError: (status?: number) => void,
onError: (error: collectorTypes.CollectorExporterError) => void,
logger: Logger,
collectorUrl: string
) {
const xhr = new XMLHttpRequest();
xhr.open('POST', collectorUrl);
xhr.setRequestHeader(collectorTypes.OT_REQUEST_HEADER, '1');
// xhr.setRequestHeader('Content-Length', String(body.length));
xhr.send(body);

xhr.onreadystatechange = () => {
Expand All @@ -145,7 +140,10 @@ function sendSpansWithXhr(
onSuccess();
} else {
logger.error('xhr error', xhr.status, body);
onError(xhr.status);
onError({
code: xhr.status,
message: xhr.responseText,
});
}
}
};
Expand Down
Submodule protos added at e6c3c4
Loading