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

chore: adding example for collector-exporter for node with opentelemetry-collector #958

Merged
merged 18 commits into from
Apr 14, 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
6 changes: 5 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ jobs:
- checkout
- run:
name: Install minimal doc and lint modules globally
command: npm i -g tslint lerna typedoc linkinator typescript gts tslint-consistent-codestyle tslint-microsoft-contrib
command: npm i -g tslint lerna typedoc linkinator typescript tslint-consistent-codestyle tslint-microsoft-contrib
- run:
name: Install gts version 1.1.0 globally
command: npm i -g gts@1.1.0
- run:
name: Symlink global modules into all lerna packages
command: lerna exec 'npm link tslint lerna typedoc linkinator typescript gts tslint-consistent-codestyle tslint-microsoft-contrib'
Expand Down Expand Up @@ -273,3 +276,4 @@ workflows:
- node12
- node13
- node12-browsers

39 changes: 39 additions & 0 deletions examples/collector-exporter-node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Overview

This example shows how to use [@opentelemetry/exporter-collector](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-exporter-collector) to instrument a simple Node.js application.

This example will export spans data simultaneously using [Exporter Collector](https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-exporter-collector) and grpc. It will use [proto format](https://github.com/open-telemetry/opentelemetry-proto).


## Installation

```shell script
$ # from this directory
$ npm install
```

## Run the Application

1. Run docker
```shell script
$ # from this directory
$ npm run docker:start
```

2. Run app
```shell script
$ # from this directory
$ npm start
```

3. Open page at http://localhost:9411/zipkin/ - you should be able to see the spans in zipkin
![Screenshot of the running example](images/spans.png)


## Useful links
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/>
- For more information on tracing, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/master/packages/opentelemetry-tracing>

## LICENSE

Apache License 2.0
18 changes: 18 additions & 0 deletions examples/collector-exporter-node/docker/collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
receivers:
otlp:
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: [otlp]
exporters: [zipkin]
processors: [batch, queued_retry]
19 changes: 19 additions & 0 deletions examples/collector-exporter-node/docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "2"
services:

# Collector
collector:
image: omnition/opentelemetry-collector-contrib:0.2.8
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"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions examples/collector-exporter-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "example-collector-exporter-node",
"private": true,
"version": "0.6.1",
"description": "Example of using @opentelemetry/collector-exporter in Node.js",
"main": "index.js",
"scripts": {
"start": "node ./start.js",
"docker:start": "cd ./docker && docker-compose down && docker-compose up",
"docker:stop": "cd ./docker && docker-compose down"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com/open-telemetry/opentelemetry-js.git"
},
"keywords": [
"opentelemetry",
"tracing"
],
"engines": {
"node": ">=8"
},
"author": "OpenTelemetry Authors",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/open-telemetry/opentelemetry-js/issues"
},
"dependencies": {
"@opentelemetry/api": "^0.6.1",
"@opentelemetry/exporter-collector": "^0.6.1",
"@opentelemetry/tracing": "^0.6.1"
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme"
}
52 changes: 52 additions & 0 deletions examples/collector-exporter-node/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

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

const address = '127.0.0.1:55678';
const exporter = new CollectorExporter({
serviceName: 'basic-service',
url: address,
});

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

const tracer = opentelemetry.trace.getTracer('example-collector-exporter-node');

// Create a span. A span must be closed.
const parentSpan = tracer.startSpan('main');
for (let i = 0; i < 10; i += 1) {
doWork(parentSpan);
}
// Be sure to end the span.
parentSpan.end();

// give some time before it is closed
setTimeout(() => {
// flush and close the connection.
exporter.shutdown();
}, 2000);

function doWork(parent) {
// Start another span. In this example, the main method already started a
// span, so that'll be the parent span, and this will be a child span.
const span = tracer.startSpan('doWork', {
parent,
});

// simulate some random work.
for (let i = 0; i <= Math.floor(Math.random() * 40000000); i += 1) {
// empty
}
// Set attributes to the span.
span.setAttribute('key', 'value');

// Annotate our span to capture metadata about our operation
span.addEvent('invoking doWork');

// end span
span.end();
}