Installing the SDK
Before you can use Honeycomb’s OpenTelemetry Web SDK, you need to install it. Navigate to the root directory of your web application and install the package:
Alternatively, install individual instrumentation packages.
Finally, confirm that the install was successful by opening your
package.json file and checking that the Dependencies list now contains @honeycomb/opentelemetry-web.
Initializing
Initialize the SDK at the start of your web application. This ensures that events such as startup time and early asset loads are captured.Configuring
The Honeycomb OpenTelemetry Web SDK can be configured using these configuration options. For example:Adding resource attributes
Resource attributes are available on every span your instrumentation emits. Adding custom, application-specific attributes makes it easier to correlate your data to important business information. You can set resource attributes using theresourceAttributes configuration option.
Enabling sampling
The SDK includes optional deterministic head sampling. The sample rate is1 by default, meaning every trace is exported.
The example below sets a sampleRate of 40, meaning 1 in 40 traces will be exported.
Sending to OpenTelemetry Collector
In production, we recommend running an OpenTelemetry Collector. Your application sends telemetry to your Collector instead of directly to Honeycomb. Your Collector then forwards the telemetry data to Honeycomb, keeping your API key stored securely in the Collector’s configuration. Configure your Collector’s URL by setting theendpoint option when initializing the Honeycomb Web SDK:
Sending to Honeycomb
To send telemetry data directly to Honeycomb, set theapiKey option with your Ingest API Key.
Web Vitals instrumentation options
Configure web vitals instrumentation by passing aWebVitalsInstrumentationConfig object with your options.
Visualizing traces locally
Honeycomb’s Web Instrumentation package can create a link to a trace visualization in the Honeycomb UI for local traces. Local visualizations enables a faster feedback cycle when adding, modifying, or verifying instrumentation. Enable local visualizations by setting thelocalVisualizations configuration option to true.
Disable local visualizations for production environments.
Local visualization creates additional overhead and should only be used during development or testing.
Enabling debug logging
Turn on debug logging by setting thedebug configuration option to true.
Adding automatic instrumentation
The Honeycomb OpenTelemetry Web SDK includes auto-instrumentation for: Automatic instrumentation is enabled by default. You can enable or disable individual auto-instrumentation libraries in your configuration using theinstrumentations option.
Adding custom instrumentation
Automatic instrumentation is a fast way to instrument your code, but you get more insight into your application by adding custom, otherwise known as manual, instrumentation. Adding custom instrumentation requires the the OpenTelemetry API package.Adding attributes to an active span
You can retrieve the currently active span in a trace and add attributes to it. This lets you add more context to traces and gives you more ways to group or filter traces in your queries.Acquiring a tracer
For manual tracing, you need to get a tracer: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.
Creating spans
Create custom spans to get a clear view of the critical parts in your application.Adding 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, 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.
Adding custom span processing
The Honeycomb Web SDK uses span processors as synchronous hooks for when a span starts and when a span ends. This lets you mutate spans after they have been created by automatic instrumentation or manually. Some examples of the actions you can take on spans in a span processor include:- Add attributes
- Add span events
- Add span links
- Update the name of a span
- Record an exception on a span
- Get the span context to create child spans
- Stop spans from being sent
Example: Basic custom span processor
Here is a basic example of a custom span processor:Example: Adding user information after SDK initialization
Here is an example span processor that adds custom information for users:Example: Dynamic page routes with React Router
Here is an example span processor that adds attributes to spans based on the state of the React Router. It sets thepage.route attribute to the generic dynamic route, and records the span as an error if there are errors in the router state.
Propagating span context
A Context Manager stores and propagates global span context through your system. OpenTelemetry provides a context manager for browser instrumentation based on the Zone.js library to track global context across asynchronous execution threads. This context manager can be added used in your instrumentation like so:Automatically propagate the trace context header
Use request automatic instrumentation to automatically send spans for every HTTP request.@opentelemetry/instrumentation-xml-http-request automatically instruments XHR requests and @opentelemetry/instrumentation-fetch automatically instruments fetch requests.
If your browser application uses a request library to make requests, such as axios or superagent, these requests are also automatically instrumented by enabling the xml-http-request or fetch instrumentation, depending on what the library uses to make requests.
When using the Honeycomb Instrumentation snippet (as documented on this page), uncomment the propagateTraceHeaderCorsUrls array and add regex to include all target domains.
This method allows you to propagate to your backend services without leaking trace IDs to third-party services.
Manually propagate the trace context header
It is also possible to manually propagate the trace context header if automatic instrumentation is not an option:Troubleshooting
Running into issues? Here are some common problems and ways to fix them. To explore common issues when sending data, visit Common Issues with Sending Data in Honeycomb.Dataset not appearing in Honeycomb
TheapiKey variable is used to send your data to Honeycomb.
Make sure you have replaced the placeholder value for it with your Honeycomb Ingest API Key and that your API key permissions include “Can create datasets”.
If Honeycomb is successfully instantiating but your API key is not included, you should see output similar to the following in your browser console:
Dataset has unexpected name
We use theserviceName variable to name your dataset in Honeycomb.
Be sure you have replaced the placeholder value for it with a name that you will find useful.
Next.js “Navigator Is Undefined” error
If a “navigator is undefined” error appears when you attempt to start your local server while following Next.js instructions, the instrumentation is being run in a server-side rendering path. To fix this, try adding the'use client'; directive at the top of the file where you instantiate Honeycomb’s web instrumentation.
Adding the 'use client'; directive tells React to only execute the file in a client environment.
If you are still seeing an error after adding the 'use client'; directive, try wrapping the function in a try/catch block.
By wrapping the function, you can catch the error and avoid instantiation in server-side environments, ensuring that your application starts up even if the code is executed in a server-side environment.
Receiving 464 errors
You may receive a464 error response from the Honeycomb API when sending telemetry using gRPC and HTTP1.
The gRPC format depends on using HTTP2 and any request over HTTP1 will be rejected by the Honeycomb servers.