-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,098 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Overview | ||
|
||
OpenTelemetry Connect Instrumentation allows the user to automatically collect trace data and export them to the backend of choice (Collector Exporter), to give observability to distributed systems. | ||
|
||
This is a simple example that demonstrates tracing calls made to Connect API. The example shows key aspects of tracing such as | ||
- Root Span (on Client) | ||
- Child Span (on Client) | ||
- Span Events | ||
- Span Attributes | ||
|
||
## Installation | ||
|
||
```sh | ||
$ # from this directory | ||
$ npm install | ||
``` | ||
|
||
## Run the Application | ||
|
||
### Collector - docker container | ||
|
||
- Run docker container with collector | ||
|
||
```sh | ||
# from this directory | ||
$ npm run docker:start | ||
``` | ||
|
||
### Server | ||
|
||
- Run the server | ||
|
||
```sh | ||
# from this directory | ||
$ npm run server | ||
``` | ||
|
||
- Run the client | ||
|
||
```sh | ||
# from this directory | ||
npm run client | ||
``` | ||
|
||
#### Zipkin UI | ||
Go to Zipkin with your browser [http://localhost:9411/]() | ||
|
||
<p align="center"><img src="images/trace1.png?raw=true"/></p> | ||
|
||
## Useful links | ||
- For more information on OpenTelemetry, visit: <https://opentelemetry.io/> | ||
- For more information on OpenTelemetry for Node.js, visit: <https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-node> | ||
|
||
## LICENSE | ||
|
||
Apache License 2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
// eslint-disable-next-line import/order | ||
const tracing = require('./tracing')('example-connect-client'); | ||
const tracer = tracing.tracer; | ||
const api = require('@opentelemetry/api'); | ||
const axios = require('axios').default; | ||
|
||
function makeRequest() { | ||
tracing.log('starting'); | ||
const span = tracer.startSpan('client.makeRequest()', { | ||
kind: api.SpanKind.CLIENT, | ||
}); | ||
|
||
api.context.with(api.trace.setSpan(api.ROOT_CONTEXT, span), async () => { | ||
try { | ||
const res = await axios.post('http://localhost:8080/run_test'); | ||
tracing.log('status:', res.statusText); | ||
span.setStatus({ code: api.SpanStatusCode.OK }); | ||
} catch (e) { | ||
tracing.log('failed:', e.message); | ||
span.setStatus({ code: api.SpanStatusCode.ERROR, message: e.message }); | ||
} | ||
span.end(); | ||
tracing.log('forcing spans to be exported'); | ||
await tracing.provider.shutdown(); | ||
tracing.log('all spans exported successfully.'); | ||
}); | ||
} | ||
|
||
makeRequest(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
http: | ||
cors_allowed_origins: | ||
- http://* | ||
- https://* | ||
|
||
exporters: | ||
zipkin: | ||
endpoint: "http://zipkin-all-in-one:9411/api/v2/spans" | ||
prometheus: | ||
endpoint: "0.0.0.0:9464" | ||
|
||
processors: | ||
batch: | ||
|
||
service: | ||
pipelines: | ||
traces: | ||
receivers: [otlp] | ||
exporters: [zipkin] | ||
processors: [batch] | ||
metrics: | ||
receivers: [otlp] | ||
exporters: [prometheus] | ||
processors: [batch] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
version: "3" | ||
services: | ||
# Collector | ||
collector: | ||
image: otel/opentelemetry-collector:0.30.0 | ||
# image: otel/opentelemetry-collector:latest | ||
command: ["--config=/conf/collector-config.yaml", "--log-level=DEBUG"] | ||
volumes: | ||
- ./collector-config.yaml:/conf/collector-config.yaml | ||
ports: | ||
- "9464:9464" | ||
- "4317:4317" | ||
- "55681:55681" | ||
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
{ | ||
"name": "connect-example", | ||
"private": true, | ||
"version": "0.24.0", | ||
"description": "Example of Connect integration with OpenTelemetry", | ||
"main": "index.js", | ||
"scripts": { | ||
"client": "node ./client.js", | ||
"docker:start": "cd ./docker && docker-compose down && docker-compose up", | ||
"docker:stop": "cd ./docker && docker-compose down", | ||
"server": "node ./server.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://[email protected]/open-telemetry/opentelemetry-js.git" | ||
}, | ||
"keywords": [ | ||
"opentelemetry", | ||
"express", | ||
"tracing" | ||
], | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"files": [ | ||
"build/src/**/*.js", | ||
"build/src/**/*.map", | ||
"build/src/**/*.d.ts", | ||
"doc", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"author": "OpenTelemetry Authors", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/open-telemetry/opentelemetry-js/issues" | ||
}, | ||
"dependencies": { | ||
"@opentelemetry/api": "^1.0.1", | ||
"@opentelemetry/exporter-jaeger": "^0.24.0", | ||
"@opentelemetry/exporter-zipkin": "^0.24.0", | ||
"@opentelemetry/exporter-collector": "^0.24.0", | ||
"@opentelemetry/instrumentation": "^0.24.0", | ||
"@opentelemetry/instrumentation-connect": "^0.24.0", | ||
"@opentelemetry/instrumentation-http": "^0.24.0", | ||
"@opentelemetry/node": "^0.24.0", | ||
"@opentelemetry/resources": "^0.24.0", | ||
"@opentelemetry/semantic-conventions": "^0.24.0", | ||
"@opentelemetry/tracing": "^0.24.0", | ||
"axios": "^0.21.1", | ||
"cross-env": "^7.0.3", | ||
"connect": "^3.7.0" | ||
}, | ||
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme", | ||
"devDependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
// eslint-disable-next-line | ||
const tracing = require('./tracing')('example-connect-server'); | ||
|
||
// Require in rest of modules | ||
const connect = require('connect'); | ||
const axios = require('axios'); | ||
|
||
// Setup connect | ||
const app = connect(); | ||
const PORT = 8080; | ||
|
||
app.use(function middleware1(req, res, next) { | ||
next(); | ||
}); | ||
|
||
app.use((req, res, next) => { | ||
next(); | ||
}); | ||
|
||
app.use('/run_test', async (req, res) => { | ||
const result = await axios.get('https://raw.githubusercontent.com/open-telemetry/opentelemetry-js/main/package.json'); | ||
tracing.log('sending response'); | ||
res.end(`OK ${result.data.version}`); | ||
|
||
// toggle enabling disabling for easier observing whether the spans are exported or not | ||
if (tracing.connectInstrumentation.isEnabled()) { | ||
tracing.log('disabling connect'); | ||
tracing.connectInstrumentation.disable(); | ||
} else { | ||
tracing.log('enabling connect'); | ||
tracing.connectInstrumentation.enable(); | ||
} | ||
|
||
}); | ||
|
||
app.listen(PORT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'use strict'; | ||
|
||
const opentelemetry = require('@opentelemetry/api'); | ||
|
||
const { diag, DiagConsoleLogger, DiagLogLevel } = opentelemetry; | ||
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO); | ||
|
||
const { Resource } = require('@opentelemetry/resources'); | ||
const { ResourceAttributes: SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions'); | ||
const { registerInstrumentations } = require('@opentelemetry/instrumentation'); | ||
const { NodeTracerProvider } = require('@opentelemetry/node'); | ||
const { SimpleSpanProcessor } = require('@opentelemetry/tracing'); | ||
const { CollectorTraceExporter } = require('@opentelemetry/exporter-collector'); | ||
|
||
const { ConnectInstrumentation } = require('@opentelemetry/instrumentation-connect'); | ||
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http'); | ||
|
||
function log() { | ||
const args = Array.from(arguments) || []; | ||
args.unshift(new Date()); | ||
console.log.apply(this, args); | ||
} | ||
|
||
module.exports = (serviceName) => { | ||
const provider = new NodeTracerProvider({ | ||
resource: new Resource({ | ||
[SemanticResourceAttributes.SERVICE_NAME]: serviceName, | ||
}), | ||
}); | ||
const connectInstrumentation = new ConnectInstrumentation(); | ||
registerInstrumentations({ | ||
tracerProvider: provider, | ||
instrumentations: [ | ||
// Connect instrumentation expects HTTP layer to be instrumented | ||
HttpInstrumentation, | ||
connectInstrumentation, | ||
], | ||
}); | ||
|
||
const exporter = new CollectorTraceExporter(); | ||
|
||
provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
|
||
// Initialize the OpenTelemetry APIs to use the NodeTracerProvider bindings | ||
provider.register({}); | ||
return { | ||
log, | ||
connectInstrumentation, | ||
provider, | ||
tracer: opentelemetry.trace.getTracer('connect-example'), | ||
} | ||
}; |
2 changes: 2 additions & 0 deletions
2
plugins/node/opentelemetry-instrumentation-connect/.eslintignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
build | ||
coverage |
7 changes: 7 additions & 0 deletions
7
plugins/node/opentelemetry-instrumentation-connect/.eslintrc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
"env": { | ||
"mocha": true, | ||
"node": true | ||
}, | ||
...require('../../../eslint.config.js') | ||
} |
4 changes: 4 additions & 0 deletions
4
plugins/node/opentelemetry-instrumentation-connect/.npmignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/bin | ||
/coverage | ||
/doc | ||
/test |
Oops, something went wrong.