Instrument Erlang/Elixir applications with OpenTelemetry
This guide shows how to instrument your Erlang/Elixir application with OpenTelemetry and export the traces to Dynatrace. To learn more about how Dynatrace works with OpenTelemetry, see Send data to Dynatrace with OpenTelemetry.
Prerequisites
- Dynatrace version 1.222+
- W3C Trace Context is enabled
- From the Dynatrace menu, go to Settings > Server-side service monitoring > Deep monitoring > Distributed tracing.
- Turn on Send W3C Trace Context HTTP headers.
Overview
To monitor your Erlang/Elixir application with OpenTelemetry
- Instrument your application
- Send the data to Dynatrace
- Restart your application and verify the data in Dynatrace
1. Instrument your application
To instrument manually, add the dependencies and the code snippet below to any Erlang or Elixir method you want to monitor.
Add the following dependencies to your rebar.config
file:
{deps, [{opentelemetry_api, "~> 1.0.0"},
{opentelemetry, "~> 1.0.0"},
{opentelemetry_exporter, "~> 1.0.0"}]}.
Add the following dependencies to your src/<PROJ_NAME>.app.src
file:
{applications, [kernel,
stdlib,
opentelemetry_api,
opentelemetry,
opentelemetry_exporter]}
Set names for the span and add attributes as you see fit.
-export([hello/0]).
-include_lib("opentelemetry_api/include/otel_tracer.hrl").
-include_lib("opentelemetry/include/otel_resource.hrl").
hello() ->
?with_span(<<"my-span">>, #{attributes => [{<<"my-key-1">>, <<"my-value-1">>}]}, fun(_ChildSpanCtx) -> %%TODO Add attributes
?set_attributes([{<<"child-key-1">>, <<"child-value-1">>}]) %%TODO Add attributes
end).
Add the following dependencies to your mix.exs
file:
defp deps do
[
{:opentelemetry_api, "~> 1.0"},
{:opentelemetry, "~> 1.0"},
{:opentelemetry_exporter, "~> 1.0"}
]
end
Also add a releases
section to your mix.exs
file:
def application do
[
extra_applications: [:logger],
mod: {<PROJ_NAME>.Application, []},
releases: [
otel_getting_started: [
version: "0.0.1",
applications: [otel_getting_started: :permanent]
]
]
]
end
Set names for the span and add attributes as you see fit.
require OpenTelemetry.Tracer, as: Tracer
def hello do
Tracer.with_span "my-span", %{attributes: [{<<"my-key-1">>, <<"my-value-1">>}]} do #TODO add attributes
Tracer.set_attributes([{"another-key-1", "another-value-1"}]) #TODO add attributes
end
end
2. Send data to Dynatrace
To send data to Dynatrace, adjust the config files as below:
To send data to Dynatrace with OneAgent, add the following configuration to your config/sys.config
file:
[
{otel_getting_started, []},
{opentelemetry,
[{processors,
[{otel_batch_processor,
#{exporter => {opentelemetry_exporter, #{endpoints =>
["http://localhost:14499/otlp"]}
}}
}]
},
{resource,
[{service,
#{name => "erlang-quickstart", version => "1.0.1"} %%TODO Replace with the name and version of your application
}]
}]
}
].
To send data to Dynatrace without OneAgent, add the following configuration to your config/sys.config
file:
[
{otel_getting_started, []},
{opentelemetry,
[{processors,
[{otel_batch_processor,
#{exporter => {opentelemetry_exporter, #{endpoints =>
["https://<URL>:443/api/v2/otlp"], %%TODO Replace <URL> to your SaaS/Managed-URL as mentioned in the next step
headers => [{"Authorization", "Api-Token <TOKEN>"}]} %%TODO Replace <TOKEN> with your API Token as mentioned in the next step
}}
}]
},
{resource,
[{service,
#{name => "erlang-quickstart", version => "1.0.1"} %%TODO Replace with the name and version of your application
}]
}]
}
].
Lastly, you need to define the correct endpoint and token, to make sure your data arrives where it should be.
- To set the endpoint:
- Use your Environment ID to set the endpoint to which your app will send traces as follows:
- Dynatrace SaaS
https://{your-environment-id}.live.dynatrace.com:443/api/v2/otlp
- Dynatrace Managed
https://{your-domain}:443/e/{your-environment-id}/api/v2/otlp
- Replace
<URL>
in the code snippet above with your endpoint.
- To create an authentication token
- In the Dynatrace menu, go to Access tokens and select Generate new token.
- Provide a Token name.
- In the Search scopes box, search for
Ingest OpenTelemetry traces
and select the checkbox. - Select Generate token.
- Select Copy to copy the token to your clipboard.
- Save the token in a safe place; you can't display it again.
- Replace
<TOKEN>
in the code snippet above with your token.
To send data to Dynatrace with OneAgent, edit your config/config.exs
file (or create the file if it does not already exist) and add the following configuration:
import Config
config :opentelemetry, :resource,
service: %{name: "elixir-quickstart", version: "1.0.1"} #TODO Replace with the name and version of your application
config :opentelemetry, :processors,
otel_batch_processor: %{
exporter: {:opentelemetry_exporter, %{endpoints: ["http://localhost:14499/otlp"]}}
}
To send data to Dynatrace without OneAgent, edit your config/config.exs
file (or create the file if it does not already exist) and add the following configuration:
import Config
config :opentelemetry, :resource,
service: %{name: "elixir-quickstart", version: "1.0.1"} #TODO Replace with the name and version of your application
config :opentelemetry, :processors,
otel_batch_processor: %{
exporter: {:opentelemetry_exporter, %{endpoints: ["<URL>:443/api/v2/otlp"], #TODO Replace <URL> to your SaaS/Managed-URL as mentioned in the next step
headers: [{"Authorization", "Api-Token <TOKEN>"}]}} #TODO Replace <TOKEN> with your API Token as mentioned in the next step
}
Lastly, you need to define the correct endpoint and token, to make sure your data arrives where it should be.
- To set the endpoint:
- Use your Environment ID to set the endpoint to which your app will send traces as follows:
- Dynatrace SaaS
https://{your-environment-id}.live.dynatrace.com:443/api/v2/otlp
- Dynatrace Managed
https://{your-domain}:443/e/{your-environment-id}/api/v2/otlp
- Replace
<URL>
in the code snippet above with your endpoint.
- To create an authentication token
- In the Dynatrace menu, go to Access tokens and select Generate new token.
- Provide a Token name.
- In the Search scopes box, search for
Ingest OpenTelemetry traces
and select the checkbox. - Select Generate token.
- Select Copy to copy the token to your clipboard.
- Save the token in a safe place; you can't display it again.
- Replace
<TOKEN>
in the code snippet above with your token.
Verify that the traces are ingested into Dynatrace
A few minutes after restarting your app, look for your spans:
- In the Dynatrace menu, go to Distributed traces and select the Ingested traces tab.
- Your spans will be part of an existing PurePath distributed trace if the root of your call is already monitored by OneAgent.
If your application does not receive any traffic, there will be no traces.
(Optional) Configure data capturing to meet privacy requirements
While Dynatrace automatically captures all OpenTelemetry resource and span attributes, only attribute values specified in the allowlist are stored and displayed in the Dynatrace web UI. This prevents accidental storage of personal data, so you can meet your privacy requirements and control the amount of monitoring data that's stored.
To view your custom span attributes, you need to allow them in the Dynatrace web UI first:
- Span attributes In the Dynatrace menu, go to Settings and select Server-side service monitoring > Span attributes.
- Resource attributes In the Dynatrace menu, go to Settings and select Server-side service monitoring > Resource attributes.