|
| 1 | +ifdef::env-github[] |
| 2 | +NOTE: For the best reading experience, |
| 3 | +please view this documentation at https://www.elastic.co/guide/en/apm/agent/nodejs/current/opentelemetry-bridge.html[elastic.co] |
| 4 | +endif::[] |
| 5 | + |
| 6 | +[[opentelemetry-bridge]] |
| 7 | +== OpenTelemetry bridge |
| 8 | + |
| 9 | +NOTE: Added as experimental in v3.34.0. |
| 10 | +To enable it, set <<opentelemetry-bridge-enabled, `opentelemetryBridgeEnabled`>> to `true`. |
| 11 | + |
| 12 | +The Elastic APM OpenTelemetry bridge allows one to use the vendor-neutral |
| 13 | +https://opentelemetry.io/docs/instrumentation/js/api/[OpenTelemetry Tracing API] |
| 14 | +(https://www.npmjs.com/package/@opentelemetry/api[`@opentelemetry/api`]) to |
| 15 | +manually instrument your code, and have the Elastic Node.js APM agent handle |
| 16 | +those API calls. This allows one to use the Elastic APM agent for tracing, |
| 17 | +without any vendor lock-in from adding manual tracing using the APM agent's own |
| 18 | +<<api,public API>>. |
| 19 | + |
| 20 | + |
| 21 | +[float] |
| 22 | +[[otel-getting-started]] |
| 23 | +=== Getting started |
| 24 | + |
| 25 | +The goal of the OpenTelemetry bridge is to allow using the OpenTelemetry API |
| 26 | +with the APM agent. ① First, you will need to add those dependencies to your |
| 27 | +project. The minimum required OpenTelemetry API version is 1.0.0. For example: |
| 28 | + |
| 29 | +[source,bash] |
| 30 | +---- |
| 31 | +npm install --save elastic-apm-node @opentelemetry/api |
| 32 | +---- |
| 33 | + |
| 34 | +② Second, you will need to configure and start the APM agent. This can be done |
| 35 | +completely with environment variables (so that there is no need to touch |
| 36 | +your application code): |
| 37 | + |
| 38 | +[source,bash] |
| 39 | +---- |
| 40 | +export ELASTIC_APM_SERVER_URL='<url of your APM server>' |
| 41 | +export ELASTIC_APM_SECRET_TOKEN='<secret token for your APM server>' # or ELASTIC_APM_API_KEY=... |
| 42 | +export ELASTIC_APM_OPENTELEMETRY_BRIDGE_ENABLED=true |
| 43 | +export NODE_OPTIONS='-r elastic-apm-node/start.js' # Tell node to preload and start the APM agent |
| 44 | +node my-app.js |
| 45 | +---- |
| 46 | + |
| 47 | +Or, alternatively, you can configure and start the APM agent at the top of your |
| 48 | +application code as follows. (Note: For automatic instrumentations to function |
| 49 | +properly, this must be executed before other `require` statements and |
| 50 | +application code.) |
| 51 | + |
| 52 | +[source,js] |
| 53 | +---- |
| 54 | +require('elastic-apm-node').start({ |
| 55 | + serverUrl: '<url of your APM server>', |
| 56 | + secretToken: '<secret token for your APM server>', // or, apiKey: '<your API key>' |
| 57 | + opentelemetryBridgeEnabled: true |
| 58 | +}); |
| 59 | +
|
| 60 | +// Application code ... |
| 61 | +---- |
| 62 | + |
| 63 | +NOTE: These examples show the minimal configuration. See <<configuration,the full APM agent configuration reference>> for other configuration options. |
| 64 | + |
| 65 | +③ Finally, you can use the OpenTelemetry API for any manual tracing in your code. |
| 66 | +For example, the following script uses |
| 67 | +https://open-telemetry.github.io/opentelemetry-js-api/interfaces/tracer.html#startactivespan[Tracer#startActiveSpan()] |
| 68 | +to trace an outgoing HTTPS request: |
| 69 | + |
| 70 | +[source,js] |
| 71 | +---- |
| 72 | +const https = require('https') |
| 73 | +const otel = require('@opentelemetry/api') |
| 74 | +const tracer = otel.trace.getTracer('trace-https-request') |
| 75 | +
|
| 76 | +tracer.startActiveSpan('makeRequest', span => { |
| 77 | + https.get('https://httpstat.us/200', (response) => { |
| 78 | + console.log('STATUS:', response.statusCode) |
| 79 | + const body = [] |
| 80 | + response.on('data', (chunk) => body.push(chunk)) |
| 81 | + response.on('end', () => { |
| 82 | + console.log('BODY:', body.toString()) |
| 83 | + span.end() |
| 84 | + }) |
| 85 | + }) |
| 86 | +}) |
| 87 | +---- |
| 88 | + |
| 89 | +The APM agent source code repository includes |
| 90 | +https://github.com/elastic/apm-agent-nodejs/tree/main/examples/opentelemetry-bridge[some examples using the OpenTelemetry bridge]. |
| 91 | + |
| 92 | + |
| 93 | +[float] |
| 94 | +[[otel-architecture]] |
| 95 | +=== Bridge architecture |
| 96 | + |
| 97 | +The OpenTelemetry bridge works similarly to the |
| 98 | +https://github.com/open-telemetry/opentelemetry-js[OpenTelemetry JS SDK]. It |
| 99 | +registers Tracer and ContextManager providers with the OpenTelemetry API. |
| 100 | +Subsequent `@opentelemetry/api` calls in user code will call into those |
| 101 | +providers. The APM agent translates from OpenTelemetry to Elastic APM semantics |
| 102 | +and sends tracing data to your APM server for full support in |
| 103 | +https://www.elastic.co/apm[Elastic Observability's APM app]. |
| 104 | + |
| 105 | +Here are a couple examples of semantic translations: The first entry span of a |
| 106 | +service (e.g. an incoming HTTP request) will be converted to an |
| 107 | +{apm-guide-ref}/data-model-transactions.html[Elasic APM `Transaction`], |
| 108 | +subsequent spans are mapped to |
| 109 | +{apm-guide-ref}/data-model-spans.html[Elastic APM `Span`]. OpenTelemetry Span |
| 110 | +attributes are translated into the appropriate fields in Elastic APM's data |
| 111 | +model. |
| 112 | + |
| 113 | +The only difference, from the user's point of view, is in the setup of tracing. |
| 114 | +Instead of setting up the OpenTelemetry JS SDK, one sets up the APM agent |
| 115 | +as <<otel-getting-started,described above>>. |
| 116 | + |
| 117 | + |
| 118 | +[float] |
| 119 | +[[otel-caveats]] |
| 120 | +=== Caveats |
| 121 | +Not all features of the OpenTelemetry API are supported. |
| 122 | + |
| 123 | +[float] |
| 124 | +[[otel-metrics]] |
| 125 | +===== Metrics |
| 126 | +This bridge only supports the tracing API. |
| 127 | +The Metrics API is currently not supported. |
| 128 | + |
| 129 | +[float] |
| 130 | +[[otel-span-links]] |
| 131 | +===== Span Links |
| 132 | +Adding links when |
| 133 | +https://open-telemetry.github.io/opentelemetry-js-api/interfaces/tracer.html[starting a span] |
| 134 | +are not currently supported. Any given links will be silently dropped. |
| 135 | + |
| 136 | +[float] |
| 137 | +[[otel-span-events]] |
| 138 | +===== Span Events |
| 139 | +Span events (https://open-telemetry.github.io/opentelemetry-js-api/interfaces/span.html#addevent[`Span#addEvent()`]) |
| 140 | +is not currently supported. Events will be silently dropped. |
| 141 | + |
| 142 | +[float] |
| 143 | +[[otel-baggage]] |
| 144 | +===== Baggage |
| 145 | +https://open-telemetry.github.io/opentelemetry-js-api/classes/propagationapi.html[Propagating baggage] |
| 146 | +within or outside the process is not supported. Baggage items are silently |
| 147 | +dropped. |
0 commit comments