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

Collector exporter #552

Merged
merged 28 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
abaadf8
chore: linting
obecny Nov 20, 2019
7af153f
feat(collector-exporter): new exporter for opentelemetry collector
obecny Nov 20, 2019
48818fb
chore: updating readme
obecny Nov 20, 2019
1cbf4c6
chore: undo auto lint fix - which is wrong
obecny Nov 20, 2019
952f99d
chore: updates after comments
obecny Nov 21, 2019
1fe92b8
chore: renaming util to transform
obecny Nov 21, 2019
eb1bb4f
chore: renaming types, last comments from review
obecny Nov 21, 2019
c09bc5c
chore: adding missing links
obecny Nov 21, 2019
4920633
chore: fixes after comments
obecny Nov 22, 2019
7c2923b
chore: fixes after comments
obecny Nov 22, 2019
a42d992
chore: fixes after comments
obecny Nov 22, 2019
e3203f0
chore: updating jsdoc
obecny Nov 25, 2019
dfbbc94
chore: enabling attributes
obecny Nov 25, 2019
08d8f8c
chore: adding script to generate package version file
obecny Nov 25, 2019
0fed57a
chore: naming
obecny Nov 25, 2019
1868afd
chore: adding todo
obecny Nov 25, 2019
bfea4ca
chore: updating types for link
obecny Nov 26, 2019
bab3be3
chore: fixing typo
obecny Nov 26, 2019
a000724
chore: removing unnecessary typing
obecny Nov 26, 2019
03d8c8b
chore: const for enum
obecny Nov 26, 2019
2b67bed
chore: adding missing interface for message event
obecny Nov 26, 2019
89ad47c
chore: adding timestamp example
obecny Nov 26, 2019
c1ee7f6
chore: changes after review
obecny Nov 26, 2019
535d536
chore: adding case when the exporter is shutdown but export is called
obecny Nov 27, 2019
7849823
chore: adding missing header for request to prevent instrumentation
obecny Nov 28, 2019
6859550
Merge branch 'master' into collector-exporter
mayurkale22 Dec 2, 2019
0c2657d
Merge branch 'master' into collector-exporter
mayurkale22 Dec 3, 2019
b603098
Merge branch 'master' into collector-exporter
mayurkale22 Dec 4, 2019
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
19 changes: 19 additions & 0 deletions examples/basic-tracer-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ $ npm install
Setup [Zipkin Tracing](https://zipkin.io/pages/quickstart.html)
or
Setup [Jaeger Tracing](https://www.jaegertracing.io/docs/latest/getting-started/#all-in-one)
or
Setup [Collector Exporter](https://github.com/open-telemetry/opentelemetry-exporter-collector)


## Run the Application

Expand Down Expand Up @@ -57,6 +60,22 @@ Click on the trace to view its details.

<p align="center"><img src="./images/jaeger-ui-detail.png?raw=true"/></p>

### Collector Exporter
Make sure you have [docker](https://docs.docker.com/) installed
- Run the docker container
```sh
$ # from this directory
$ # open telemetry
$ npm run collector:docker:ot
$ # or alternatively open census
obecny marked this conversation as resolved.
Show resolved Hide resolved
$ npm run collector:docker:oc
$ # at any time you can stop & clean them
$ npm run collector:docker:stop
```

#### Collector Exporter - Zipkin UI
Please follow the section [Zipkin UI](#zipkin-ui) only
obecny marked this conversation as resolved.
Show resolved Hide resolved

### Export to multiple exporters

- Run the sample
Expand Down
7 changes: 7 additions & 0 deletions examples/basic-tracer-node/docker/oc/collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
receivers:
opencensus:
address: "127.0.0.1:55678"

exporters:
zipkin:
endpoint: "http://zipkin-all-in-one:9411/api/v2/spans"
19 changes: 19 additions & 0 deletions examples/basic-tracer-node/docker/oc/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "2"
services:

# Collector
collector:
image: omnition/opencensus-collector
command: ["--config=/conf/collector-config.yaml", "--http-pprof-port=1777", "--receive-zipkin", "--receive-oc-trace", "--log-level=DEBUG"]
volumes:
- ./collector-config.yaml:/conf/collector-config.yaml
ports:
- "55678:55678"
depends_on:
- zipkin-all-in-one

# Zipkin
zipkin-all-in-one:
image: openzipkin/zipkin:latest
ports:
- "9411:9411"
18 changes: 18 additions & 0 deletions examples/basic-tracer-node/docker/ot/collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
receivers:
opencensus:
endpoint: 0.0.0.0:55678

exporters:
zipkin:
url: "http://zipkin-all-in-one:9411/api/v2/spans"

processors:
batch:
queued_retry:

service:
pipelines:
traces:
receivers: [opencensus]
exporters: [zipkin]
processors: [batch, queued_retry]
19 changes: 19 additions & 0 deletions examples/basic-tracer-node/docker/ot/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "2"
services:

# Collector
collector:
image: otelcol:latest
command: ["--config=/conf/collector-config.yaml", "--log-level=DEBUG"]
volumes:
- ./collector-config.yaml:/conf/collector-config.yaml
ports:
- "55678:55678"
depends_on:
- zipkin-all-in-one

# Zipkin
zipkin-all-in-one:
image: openzipkin/zipkin:latest
ports:
- "9411:9411"
7 changes: 5 additions & 2 deletions examples/basic-tracer-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@ const opentelemetry = require('@opentelemetry/core');
const { BasicTracer, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');
const { CollectorExporter } = require('@opentelemetry/exporter-collector');

const options = {
serviceName: 'basic-service'
}
};

// Initialize an exporter depending on how we were started
let exporter;

const EXPORTER = process.env.EXPORTER || '';
if (EXPORTER.toLowerCase().startsWith('z')) {
exporter = new ZipkinExporter(options);
} else {
} else if (EXPORTER.toLowerCase().startsWith('j')) {
exporter = new JaegerExporter(options);
} else {
exporter = new CollectorExporter(options);
}

const tracer = new BasicTracer();
Expand Down
7 changes: 6 additions & 1 deletion examples/basic-tracer-node/multi_exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const opentelemetry = require('@opentelemetry/core');
const { BasicTracer, BatchSpanProcessor, SimpleSpanProcessor } = require('@opentelemetry/tracing');
const { JaegerExporter } = require('@opentelemetry/exporter-jaeger');
const { ZipkinExporter } = require('@opentelemetry/exporter-zipkin');
const { CollectorExporter } = require('@opentelemetry/exporter-collector');

const tracer = new BasicTracer();

Expand All @@ -10,7 +11,8 @@ const jaegerExporter = new JaegerExporter({
serviceName: 'basic-service',
// The default flush interval is 5 seconds.
flushInterval: 2000
})
});
const collectorExporter = new CollectorExporter({serviceName: 'basic-service'});

// It is recommended to use this BatchSpanProcessor for better performance
// and optimization, especially in production.
Expand All @@ -22,6 +24,8 @@ tracer.addSpanProcessor(new BatchSpanProcessor(zipkinExporter, {
// it's internal client already handles the spans with batching logic.
tracer.addSpanProcessor(new SimpleSpanProcessor(jaegerExporter));

tracer.addSpanProcessor(new SimpleSpanProcessor(collectorExporter));

// Initialize the OpenTelemetry APIs to use the BasicTracer bindings
opentelemetry.initGlobalTracer(tracer);

Expand All @@ -36,6 +40,7 @@ span.end();
// flush and close the connection.
zipkinExporter.shutdown();
jaegerExporter.shutdown();
collectorExporter.shutdown();

function doWork(parent) {
// Start another span. In this example, the main method already started a
Expand Down
5 changes: 5 additions & 0 deletions examples/basic-tracer-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"scripts": {
"zipkin:basic": "cross-env EXPORTER=zipkin node ./index.js",
"jaeger:basic": "cross-env EXPORTER=jaeger node ./index.js",
"collector:basic": "cross-env EXPORTER=collector node ./index.js",
"collector:docker:oc": "cd ./dockers/ot && docker-compose down && cd ../oc && docker-compose down && docker-compose up",
"collector:docker:ot": "cd ./dockers/oc && docker-compose down && cd ../ot && docker-compose down && docker-compose up",
"collector:docker:stop": "cd ./dockers/oc && docker-compose down && cd ../ot && docker-compose down",
"multi_exporter": "node ./multi_exporter.js"
},
"repository": {
Expand All @@ -28,6 +32,7 @@
},
"dependencies": {
"@opentelemetry/core": "^0.2.0",
"@opentelemetry/exporter-collector": "^0.2.0",
"@opentelemetry/exporter-jaeger": "^0.2.0",
"@opentelemetry/exporter-zipkin": "^0.2.0",
"@opentelemetry/tracing": "^0.2.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/tracer-web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</head>

<body>
Example of using Web Tracer with document load plugin and console exporter
Example of using Web Tracer with document load plugin with console exporter and collector exporter
<script type="text/javascript" src="/bundle.js"></script>
<br/>
<button id="button1">Test WebTracer with ZoneScopeManager - async</button>
Expand Down
2 changes: 2 additions & 0 deletions examples/tracer-web/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ConsoleSpanExporter, SimpleSpanProcessor } from '@opentelemetry/tracing
import { WebTracer } from '@opentelemetry/web';
import { DocumentLoad } from '@opentelemetry/plugin-document-load';
import { ZoneScopeManager } from '@opentelemetry/scope-zone';
import { CollectorExporter } from '@opentelemetry/exporter-collector'

const webTracer = new WebTracer({
plugins: [
Expand All @@ -17,6 +18,7 @@ const webTracerWithZone = new WebTracer({
]
});
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
webTracerWithZone.addSpanProcessor(new SimpleSpanProcessor(new CollectorExporter()));

console.log('Current span is window', webTracerWithZone.getCurrentSpan() === window);

Expand Down
1 change: 1 addition & 0 deletions examples/tracer-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"webpack-merge": "^4.2.2"
},
"dependencies": {
"@opentelemetry/exporter-collector": "^0.2.0",
"@opentelemetry/plugin-document-load": "^0.2.0",
"@opentelemetry/scope-zone": "^0.2.0",
"@opentelemetry/tracing": "^0.2.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/opentelemetry-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"precompile": "tsc --version",
"compile": "tsc -p .",
"fix": "gts fix",
"prepare": "npm run compile"
"prepare": "npm run compile",
"watch": "tsc -w"
},
"keywords": [
"opentelemetry",
Expand Down
27 changes: 27 additions & 0 deletions packages/opentelemetry-core/src/common/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ export function hrTimeDuration(
return [seconds, nanos];
}

// Returns end time based on startTime and duration
export function hrTimeEndTime(
startTime: types.HrTime,
duration: types.HrTime
): types.HrTime {
let seconds = startTime[0] + duration[0];
let nanos = startTime[1] + duration[1];

// overflow
if (nanos < 0) {
seconds -= 1;
// negate
nanos += SECOND_TO_NANOSECONDS;
}

return [seconds, nanos];
}

// Convert hrTime to timestamp.
obecny marked this conversation as resolved.
Show resolved Hide resolved
export function hrTimeToTimeStamp(hrTime: types.HrTime): string {
obecny marked this conversation as resolved.
Show resolved Hide resolved
obecny marked this conversation as resolved.
Show resolved Hide resolved
const precision = NANOSECOND_DIGITS;
const tmp = `${'0'.repeat(precision)}${hrTime[1]}Z`;
const nanoString = tmp.substr(tmp.length - precision - 1);
const date = new Date(hrTime[0] * 1000).toISOString();
return date.replace('000Z', nanoString);
}

// Convert hrTime to nanoseconds.
export function hrTimeToNanoseconds(hrTime: types.HrTime): number {
return hrTime[0] * SECOND_TO_NANOSECONDS + hrTime[1];
Expand Down
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 @@ -17,3 +17,4 @@
export * from './id';
export * from './performance';
export * from './timer-util';
export * from './span-id-to-base64';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*!
* Copyright 2019, 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 base64
* @param hexStr - id of span
*/
export function spanIdToBase64(hexStr: string = ''): string {
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 btoa(hexAsciiCharsStr);
}
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 @@ -17,3 +17,4 @@
export * from './id';
export * from './performance';
export * from './timer-util';
export * from './span-id-to-base64';
31 changes: 31 additions & 0 deletions packages/opentelemetry-core/src/platform/node/span-id-to-base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*!
* Copyright 2019, 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 base64
* @param hexStr - id of span
*/
export function spanIdToBase64(hexStr: string = ''): string {
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').toString('base64');
}
29 changes: 29 additions & 0 deletions packages/opentelemetry-core/test/common/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
hrTimeToNanoseconds,
hrTimeToMilliseconds,
hrTimeToMicroseconds,
hrTimeToTimeStamp,
isTimeInput,
hrTimeEndTime,
} from '../../src/common/time';

describe('time', () => {
Expand Down Expand Up @@ -156,6 +158,33 @@ describe('time', () => {
});
});

describe('#hrTimeEndTime', () => {
it('should return endTime', () => {
const startTime: types.HrTime = [22, 400000000];
const durationTime: types.HrTime = [32, 800000000];

const output = hrTimeEndTime(startTime, durationTime);
assert.deepStrictEqual(output, [54, 1200000000]);
});

it('should handle nanosecond overflow', () => {
const startTime: types.HrTime = [22, 400000000];
const durationTime: types.HrTime = [32, 200000000];

const output = hrTimeEndTime(startTime, durationTime);
assert.deepStrictEqual(output, [54, 600000000]);
});
});

describe('#hrTimeToTimeStamp', () => {
it('should return timestamp', () => {
const time: types.HrTime = [1573513121, 123456];

const output = hrTimeToTimeStamp(time);
assert.deepStrictEqual(output, '2019-11-11T22:58:41.000123456Z');
});
});

describe('#hrTimeToNanoseconds', () => {
it('should return nanoseconds', () => {
const output = hrTimeToNanoseconds([1, 200000000]);
Expand Down
Loading