Skip to content

Commit 8ed5623

Browse files
committed
user docs for the OTel bridge, deprecate OpenTracing bridge
1 parent 18fa679 commit 8ed5623

9 files changed

Lines changed: 203 additions & 32 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ npm install elastic-apm-node --save
4444
- [Metrics](https://www.elastic.co/guide/en/apm/agent/nodejs/current/metrics.html)
4545
- [Performance Tuning](https://www.elastic.co/guide/en/apm/agent/nodejs/current/performance-tuning.html)
4646
- [Source Map Support](https://www.elastic.co/guide/en/apm/agent/nodejs/current/source-maps.html)
47-
- [OpenTracing Support](https://www.elastic.co/guide/en/apm/agent/nodejs/current/opentracing.html)
47+
- [OpenTracing Support](https://www.elastic.co/guide/en/apm/agent/nodejs/current/opentracing.html) XXX
4848
- [Supported Technologies](https://www.elastic.co/guide/en/apm/agent/nodejs/current/supported-technologies.html)
4949
- [Upgrading](https://www.elastic.co/guide/en/apm/agent/nodejs/current/upgrading.html)
5050
- [Troubleshooting](https://www.elastic.co/guide/en/apm/agent/nodejs/current/troubleshooting.html)

docs/api-opentelemetry.asciidoc

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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.

docs/configuration.asciidoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,3 +1235,24 @@ require('elastic-apm-node').start({
12351235
})
12361236
----
12371237

1238+
1239+
[[opentelemetry-bridge-enabled]]
1240+
==== `opentelemetryBridgeEnabled`
1241+
* *Type:* Boolean
1242+
* *Default:* `false`
1243+
* *Env:* `ELASTIC_APM_OPENTELEMETRY_BRIDGE_ENABLED`
1244+
1245+
Setting this option to true will enable the <<opentelemetry-bridge,OpenTelemetry Bridge>>. Briefly, the OpenTelemetry Bridge allows one to use the vendor-neutral
1246+
https://opentelemetry.io/docs/instrumentation/js/api/[OpenTelemetry Tracing API]
1247+
(https://www.npmjs.com/package/@opentelemetry/api[`@opentelemetry/api`]) to
1248+
manually instrument your code, and have the Elastic Node.js APM agent handle
1249+
those API calls.
1250+
1251+
Example usage:
1252+
1253+
[source,js]
1254+
----
1255+
require('elastic-apm-node').start({
1256+
opentelemetryBridgeEnabled: true
1257+
})
1258+
----

docs/index.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ include::./api.asciidoc[]
3131

3232
include::./metrics.asciidoc[]
3333

34+
include::./api-opentelemetry.asciidoc[]
35+
3436
include::./opentracing.asciidoc[]
3537

3638
include::./log-correlation.asciidoc[]

docs/opentracing.asciidoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ NOTE: For the best reading experience,
55
please view this documentation at https://www.elastic.co/guide/en/apm/agent/nodejs/current/opentracing.html[elastic.co]
66
endif::[]
77

8-
== OpenTracing API
8+
== OpenTracing bridge
9+
10+
NOTE: https://opentracing.io/[OpenTracing] is discontinued in favor of OpenTelemetry. This Elastic APM OpenTracing bridge is **deprecated**. Consider using the <<opentelemetry-bridge>> instead.
11+
912

1013
The Elastic APM OpenTracing bridge allows creating Elastic APM transactions and spans,
1114
using the https://opentracing-javascript.surge.sh/[OpenTracing API].

examples/opentelemetry-bridge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ To run a script using the **Elastic Node.js APM Agent** use:
1414
For example:
1515

1616
export ELASTIC_APM_OPENTELEMETRY_BRIDGE_ENABLED=true
17-
node -r elastic-apm-node/start.js trace-http-request.js
17+
node -r elastic-apm-node/start.js trace-https-request.js
1818

1919
While these examples are written to use the `node -r elastic-apm-node/start.js ...`
2020
mechanism to start the APM agent. That isn't required. One can still load and
@@ -33,7 +33,7 @@ require('elastic-apm-node').start({
3333

3434
For comparison, these scripts be instrumented with the OpenTelemetry JS SDK.
3535

36-
node -r ./otel-sdk.js trace-http-request.js
36+
node -r ./otel-sdk.js trace-https-request.js
3737

3838
The "otel-sdk.js" is a simplified tracing setup of the OpenTelemetry SDK. For
3939
the sake of simpler demonstration it writes tracing spans to the console rather

examples/opentelemetry-bridge/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"scripts": {
66
"trace-hello-world": "ELASTIC_APM_OPENTELEMETRY_BRIDGE_ENABLED=true node -r elastic-apm-node/start.js trace-hello-world.js",
7-
"trace-http-request": "ELASTIC_APM_OPENTELEMETRY_BRIDGE_ENABLED=true node -r elastic-apm-node/start.js trace-http-request.js"
7+
"trace-https-request": "ELASTIC_APM_OPENTELEMETRY_BRIDGE_ENABLED=true node -r elastic-apm-node/start.js trace-https-request.js"
88
},
99
"dependencies": {
1010
"@opentelemetry/api": "^1.1.0",

examples/opentelemetry-bridge/trace-http-request.js

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// A demonstration of using the Elastic APM OpenTelemetry bridge to
2+
// trace an HTTP request.
3+
//
4+
// Usage:
5+
// npm install
6+
// export ELASTIC_APM_OPENTELEMETRY_BRIDGE_ENABLED=true
7+
// node -r elastic-apm-node/start.js trace-https-request.js
8+
9+
'use strict'
10+
11+
const https = require('https')
12+
const otel = require('@opentelemetry/api')
13+
const tracer = otel.trace.getTracer('trace-https-request')
14+
15+
tracer.startActiveSpan('makeRequest', span => {
16+
https.get('https://httpstat.us/200', (response) => {
17+
console.log('STATUS:', response.statusCode)
18+
const body = []
19+
response.on('data', (chunk) => body.push(chunk))
20+
response.on('end', () => {
21+
console.log('BODY:', body.toString())
22+
span.end()
23+
})
24+
})
25+
})

0 commit comments

Comments
 (0)