Before You Begin
Before you can set up automatic instrumentation for your Node.js application, you will need to do a few things.Prepare Your Development Environment
To complete the required steps, you will need:- A working Node.js environment
- An application written in Node.js
Get Your Honeycomb API Key
To send data to Honeycomb, you’ll need to sign up for a free Honeycomb account and create a Honeycomb Ingest API Key. To get started, you can create a key that you expect to swap out when you deploy to production. Name it something helpful, perhaps noting that it’s a getting started key. Make note of your API key; for security reasons, you will not be able to see the key again, and you will need it later! If you want to use an API key you previously stored in a secure location, you can also look up details for Honeycomb API Keys any time in your Environment Settings, and use them to retrieve keys from your storage location.Add Automatic Instrumentation
Automatic instrumentation is enabled by adding instrumentation packages. Add custom, or manual, instrumentation using the OpenTelemetry API.Acquire Dependencies
- npm
- yarn
Open your terminal, navigate to the location of your project on your drive, and install OpenTelemetry’s automatic instrumentation meta package and OpenTelemetry’s Node.js SDK package:
Alternatively, install individual instrumentation packages.If using TypeScript, install
| Module | Description |
|---|---|
auto-instrumentations-node | OpenTelemetry’s meta package that provides a way to add automatic instrumentation to any Node application to capture telemetry data from a number of popular libraries and frameworks, like express, dns, http, and more. |
sdk-node | OpenTelemetry’s Node.js distribution package that streamlines configuration and allows you to instrument as quickly and easily as possible. |
ts-node to run the code:Initialize
- TypeScript
- JavaScript
Create an initialization file, commonly known as the
tracing.ts file:Configure
Use environment variables to configure the OpenTelemetry SDK:| Variable | Description |
|---|---|
OTEL_SERVICE_NAME | Service name. When you send data, Honeycomb creates a dataset in which to store your data and uses this as the name. Can be any string. |
OTEL_EXPORTER_OTLP_PROTOCOL | The data format that the SDK uses to send telemetry to Honeycomb. For more on data format configuration options, read Choosing between gRPC and HTTP. |
OTEL_EXPORTER_OTLP_ENDPOINT | Honeycomb endpoint to which you want to send your data. |
OTEL_EXPORTER_OTLP_HEADERS | Adds your Honeycomb API Key to the exported telemetry headers for authorization. Learn how to find your Honeycomb API Key. |
If you use Honeycomb Classic, you must also specify the Dataset using the
x-honeycomb-dataset header.If you are sending data directly to Honeycomb, you must configure the API key and service name.
If you are using an OpenTelemetry Collector, configure your API key at the Collector level instead.
Run
- TypeScript
- JavaScript
Run the Node.js app and include the initialization file you created:Be sure to replace
YOUR_APPLICATION_NAME with the name of your application’s main file.Alternatively, you can import the initialization file as the first step in your application lifecycle.In Honeycomb’s UI, you should now see your application’s incoming requests and outgoing HTTP calls generate traces.Add Custom Instrumentation
Automatic instrumentation is the easiest way to get started with instrumenting your code. To get additional insight into your system, you should also add custom, or manual, instrumentation where appropriate. Follow the instructions below to add custom instrumentation to your code. To learn more about custom, or manual, instrumentation, visit the comprehensive set of topics covered by Manual Instrumentation for JavaScript in OpenTelemetry’s documentation.Acquire Dependencies
To start adding custom instrumentation, ensure that the OpenTelemetry API package exists as a direct dependency in your project. This package provides access to the high-level instrumentation APIs, which gives the ability to retrieve the current span to enrich with additional attributes, to create new spans, and to generate metrics.Add Attributes to Spans
Adding attributes to a currently executing span in a trace can be useful. For example, you may have an application or service that handles users, and you want to associate the user with the span when querying your service in Honeycomb. To do this, get the current span from the context and set an attribute with the user ID:user.id field to the current span, so you can use the field in WHERE, GROUP BY or ORDER clauses in the Honeycomb query builder.
Initialize a Tracer
To create spans, you need to initialize aTracer.
Tracer, OpenTelemetry requires you to give it a name as a string.
This string is the only required parameter.
When traces are sent to Honeycomb, the name of the Tracer is turned into the library.name field, which can be used to show all spans created from a particular tracer.
In general, pick a name that matches the appropriate scope for your traces.
If you have one tracer for each service, then use the service name.
If you have multiple tracers that live in different “layers” of your application, then use the name that corresponds to that “layer”.
The library.name field is also used with traces created from instrumentation libraries.
You can then use this tracer to create custom spans.
Create New Spans
Automatic instrumentation can show the shape of requests to your system, but only you know the truly important parts. To get the full picture of what is happening, you must add custom, or manual, instrumentation and create some custom spans. To do this, grab the tracer from the OpenTelemetry API:Add Multi-Span Attributes
Sometimes you want to add the same attribute to many spans within the same trace. This attribute may include variables calculated during your program, or other useful values for correlation or debugging purposes. To add this attribute to multiple spans, leverage the OpenTelemetry concept of baggage. Baggage allows you to add akey with a value as an attribute to every subsequent child span within the current application context.
-
Install the baggage span processor package using your terminal:
-
When configuring the OpenTelemetry SDK tracer provider, add the
BaggageSpanProcessor: -
Add a baggage entry for the current trace and replace
keyandvaluewith your desired key-value pair:Any Baggage attributes that you set in your application will be attached to outgoing network requests as a header. If your service communicates to a third party API, do NOT put sensitive information in the Baggage attributes.
Sampling
You can configure the OpenTelemetry SDK to sample the data it generates. Honeycomb weights sampled data based on sample rate, so you must set a resource attribute containing the sample rate. Use aTraceIdRatioBased sampler, with a ratio expressed as 1/N.
Then, also create a resource attribute called SampleRate with the value of N.
This allows Honeycomb to reweigh scalar values, like counts, so that they are accurate even with sampled data.
In the example below, our goal is to keep approximately half (1/2) of the data volume.
The resource attribute contains the denominator (2), while the OpenTelemetry sampler argument contains the decimal value (0.5).
To get access to a sampler, install the core OpenTelemetry package:
TraceIdRatioBasedSampler, and add as a sampler to the NodeSDK along with the SampleRate in the Resource.
Choosing between gRPC and HTTP
Most OpenTelemetry SDKs have an option to export telemetry as OTLP either over gRPC or HTTP/protobuf, with some also offering HTTP/JSON. If you are trying to choose between gRPC and HTTP, keep in mind:- Some SDKs default to using gRPC, and it may be easiest to start with the default option.
- Some firewall policies are not set up to handle gRPC and require using HTTP.
- gRPC may improve performance, but its long-lived connections may cause problems with load balancing, especially when using Refinery.
Endpoint URLs for OTLP/HTTP
When using theOTEL_EXPORTER_OTLP_ENDPOINT environment variable with an SDK and an HTTP exporter, the final path of the endpoint is modified by the SDK to represent the specific signal being sent.
For example, when exporting trace data, the endpoint is updated to append v1/traces.
When exporting metrics data, the endpoint is updated to append v1/metrics.
So, if you were to set the OTEL_EXPORTER_OTLP_ENDPOINT to https://api.honeycomb.io, traces would be sent to https://api.honeycomb.io/v1/traces and metrics would be sent to https://api.honeycomb.io/v1/metrics.
The same modification is not necessary for gRPC.
OTEL_EXPORTER_OTLP_<SIGNAL>_ENDPOINT instead of the more generic OTEL_EXPORTER_OTLP_ENDPOINT.
When using a signal-specific environment variable, these paths must be appended manually.
Set OTEL_EXPORTER_OTLP_TRACES_ENDPOINT for traces, appending the endpoint with v1/traces, and OTEL_EXPORTER_OTLP_METRICS_ENDPOINT for metrics, appending the endpoint with v1/metrics.
Send both traces and metrics to Honeycomb using this method by setting the following variables:
/v1/traces like so: