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 all 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 = git@github.com:open-telemetry/opentelemetry-proto.git
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.6.1",
"@opentelemetry/base": "^0.6.1",
"@opentelemetry/core": "^0.6.1",
"@opentelemetry/resources": "^0.6.1",
"@opentelemetry/tracing": "^0.6.1"
"@opentelemetry/tracing": "^0.6.1",
"google-protobuf": "^3.11.4",
"grpc": "^1.24.2"
}
}
39 changes: 19 additions & 20 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 { opentelemetryProto } 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,33 +79,34 @@ 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) {
resultCallback(ExportResult.FAILED_NOT_RETRYABLE);
} else {
resultCallback(ExportResult.FAILED_RETRYABLE);
.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);
}
}
});
);
}

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 +125,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
@@ -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,26 +14,26 @@
* limitations under the License.
*/

import * as core from '@opentelemetry/core';
import { Logger } from '@opentelemetry/api';
import { ReadableSpan } from '@opentelemetry/tracing';
import { CollectorExporter } from '../../CollectorExporter';
import { toCollectorExportTraceServiceRequest } 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 +43,17 @@ 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,
const exportTraceServiceRequest = toCollectorExportTraceServiceRequest(
spans,
};
collectorExporter
);

const body = JSON.stringify(exportTraceServiceRequest);

Expand Down Expand Up @@ -104,7 +87,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 +96,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 @@ -129,13 +112,15 @@ function sendSpansWithBeacon(
function sendSpansWithXhr(
body: string,
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('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(body);

xhr.onreadystatechange = () => {
Expand All @@ -144,8 +129,12 @@ function sendSpansWithXhr(
logger.debug('xhr success', body);
onSuccess();
} else {
logger.error('xhr error', xhr.status, body);
onError(xhr.status);
logger.error('body', body);
logger.error('xhr error', xhr);
onError({
code: xhr.status,
message: xhr.responseText,
});
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
### Important!
**Submodule is always pointing to certain revision number. So updating the master of the submodule repo will not have impact on your code.
Knowing this if you want to change the submodule to point to a different version (when for example proto has changed) here is how to do it:**

### Updating submodule to point to certain revision number

1. Make sure you are in the same folder as this instruction

2. Update your submodules by running this command

```shell script
git submodule sync --recursive
git submodule update --init --recursive
```

3. Find the SHA which you want to update to and copy it (the long one)
the latest sha when this guide was written is `e6c3c4a74d57f870a0d781bada02cb2b2c497d14`

4. Enter a submodule directory from this directory

```shell script
cd protos
```

5. Updates files in the submodule tree to given commit:

```shell script
git checkout -q <sha>
```

6. Return to the main directory:

```shell script
cd ../
```

7. Please run `git status` you should see something like `Head detached at`. This is correct, go to next step

8. Now thing which is very important. You have to commit this to apply these changes

```shell script
git commit -am "chore: updating submodule for opentelemetry-proto"
```

9. If you look now at git log you will notice that the folder `protos` has been changed and it will show what was the previous sha and what is current one
Submodule protos added at e6c3c4
Loading