Requirements
- A Ruby app, preferably one that listens for HTTP requests or makes SQL calls
- Ruby 2.3 or greater
- A Honeycomb API key
Quick Installation
If using the dataset-only data model, refer to the Honeycomb Classic tab for instructions.
Not sure?
Learn more about Honeycomb versus Honeycomb Classic.
- Honeycomb
- Honeycomb Classic
To install the Ruby Beeline for your application:
-
Add
honeycomb-beelineto your Gemfile: -
If you have a Rails application, you can run the generator to create your configuration file:
Otherwise in your code, initialize the Beeline with your API key and service name:
Add Custom Fields To Events
The Ruby Beeline tracks the current span within the overall trace. This allows you to add as many additional custom fields as you like. Here is an example of adding a custom field to the current span:app. namespace.
For example, the field above would appear in your event as app.big_num.
The namespace groups your fields together to make them easy to find and examine.
If you prefer to avoid the app. namespace, for example in order to overwrite an automatically-populated field, you can have your block take the current span as a parameter and call add_field directly on that instance.
add_field_to_trace instead.
add_field_to_trace adds the field to both the currently active span and all other spans that have yet to be sent and are involved in this trace that occur within this process.
Additionally, these fields are packaged up and passed along to downstream processes if they are also using a beeline.
This function is good for adding information that is better scoped to the request than this specific unit of work, such as user IDs, globally relevant feature flags, or errors.
Fields added here are also prefixed with app.
Adding Spans To a Trace
We encourage people to think about instrumentation in terms of “units of work”. As your program grows, what constitutes a unit of work is likely to be portions of your overall service rather than an entire run. Spans are a way of breaking up a single external action (say, an HTTP request) into several smaller units in order to gain more insight into your service. Together, many spans make a trace, which you can visualize traces within the Honeycomb query builder. Adding spans with the Ruby Beeline is easy! Here is an example, where calls toslow_operation get their own span within the trace:
- a name - in this case
slow_operation - a duration - how much time elapsed between when the span was started and sent
- a service_name - generally configured during Beeline initialization
- several IDs - trace, span, and parent identifiers (UUIDs)
Honeycomb.add_field method.
Other Integrations
The Ruby Beeline integrates with other common ruby gems in order to populate spans and traces with useful information.Active Support
The Ruby Beeline can listen to any ActiveSupport instrumentation calls in your application to produce spans from subscribed event types. This is the primary means by which the Rails framework is instrumented.Subscribe And Use The Default Handler
Add the identifier for the ActiveSupport notification to an array assigned to the Beeline configuration’snotification_events property.
The Beeline will generate a span for the notification event with the event’s keys added as fields.
Event keys whose values are complex objects will be coerced to strings with #to_s.
Customize Notification Handling by Event Type
To subscribe to a notification event type and customize the fields that will appear on its span, use the Beeline configuration’s#on_notification_event method.
Call #on_notification_event with a specific event name, provide a block that receives a name, span, and payload, then customize the span within the block.
For example, to add fields to the span for a perform.active_job notification event with values from the Job object on its job key:
AWS
The Ruby Beeline will automatically instrument any calls made using v2 or v3 of theaws-sdk gem and create a span for each call.
Faraday
The Ruby Beeline will attempt to propagate outgoing HTTP requests with a trace propagation header. This allows other services that are using a Beeline to produce a distributed trace.Rack
The Ruby Beeline provides a piece of rack middleware that can be used in your rack applications. This will create a trace and root span for each request and continue any distributed traces with trace ids included from other Beelines in the header.Rails
The Ruby Beeline provides a Rails generator for easy configuration.rails generate honeycomb command will generate a Rails initializer with some basic Active Support events selected and a sample presend_hook to demonstrate removing potentially sensitive information from notification events.
The Rails Guide for Active Support Instrumentation documents many more types of events to which you can subscribe to send to Honeycomb.
The handling of notification events can be customized based on the event identifier; see the Active Support section for details.
The Beeline does not sanitize raw SQL.
Database queries from
.find_by_sql() or ActiveRecord.connection.execute will be transmitted to Honeycomb in their entirety.
To prevent sending sensitive information to Honeycomb, use the ActiveRecord query interface and ensure prepared_statements is set to true in config/database.yml for your database adapter.
Most Rails database adapters default to using prepared statements, but some, like MySQL, do not.Rake
The Ruby Beeline will automatically instrument your rake tasks and create a trace and root span for each rake task.Sequel
The Ruby Beeline will automatically instrument your sequel database calls and create a span for each call.Sinatra
The Ruby Beeline enhances the provided rack middleware to collect extra information for sinatra application.Proxy Configuration
The Beeline will use anyhttps_proxy, http_proxy, and no_proxy environment variables set for the process for all event traffic to Honeycomb.
Note: To use the standard proxy environment variables, ensure that the underlying Ruby Libhoney client is version 1.18.0 or greater.
Proxy support was added in Libhoney version 1.14.0 and was configured via a proxy_config Array passed during client initialization.
Array-style proxy configuration will be deprecated in Libhoney 2.0.
Augmenting Or Scrubbing Spans
If you have some transformations you would like to make on every span before it leaves the process for Honeycomb, thepresend_hook is your opportunity to make these changes.
Examples are scrubbing sensitive data (for example, you may want to ensure specific fields are dropped or hashed) or augmenting data (like making out-of-band translations between an ID and a more human readable version).
Similar to the sample_hook discussed below, you pass the presend_hook a block that will be called on every span with the fields of that span as an argument.
The function is free to mutate the hash passed in and those mutations will be what finally gets sent to Honeycomb.
As an example, say we have some HTTP requests that provide sensitive values which have been added to a span.
This code will examine all spans just before they are sent to Honeycomb and replace the sensitive values with “[REDACTED]”.
Sampling Events
To sample a portion of events for very high throughput services, include a custom client with asample_rate in the initialization of the Ruby Beeline.
This sends 1/n of all events, so a sample rate of 5 would send 20% of your events.
sample_rate must be a positive integer.
Sampling is performed by default on a per-trace level in the Ruby Beeline, so adding sampling will not break your traces.
Either all spans in a trace will be sent, or no spans in the trace will be sent.
The Honeycomb engine weights query results based on sample rate to ensure that sampled computations return correct results.
Customizing Sampling Logic
Our Beeline lets you define asample_hook in order to customize the logic used for deterministic per-trace sampling.
For example, assume you have instrumented an HTTP server.
You would like to keep all errored requests and heavily sample healthy traffic (200 response codes).
Also, you do not really care about 302 redirects in your app, so want to drop those.
You could define a sampler function like so:
Defining a sampling hook overrides the deterministic sampling behavior for trace IDs.
Unless you take
trace.trace_id into account (as we did above by extending the DeterministicSampler), you will get incomplete traces.Distributed Trace Propagation
When a service calls another service, you want to ensure that the relevant trace information is propagated from one service to the other. This allows Honeycomb to connect the two services in a trace. Distributed tracing enables you to trace and visualize interactions between multiple instrumented services. For example, your users may interact with a front-end API service, which talks to two internal APIs to fulfill their request. In order to have traces connect spans for all these services, it is necessary to propagate trace context between these services, usually by using an HTTP header. Both the sending and receiving service must use the same propagation format, and both services must be configured to send data to the same Honeycomb environment.Interoperability With OpenTelemetry
Trace context propagation with OpenTelemetry is done by sending and parsing headers that conform to the W3C Trace Context specification. To get Beelines and OpenTelemetry instrumentation to interoperate, you will need to use W3C headers. The Beeline includes marshal and unmarshal functions that can generate and parse W3C Trace Context headers. Honeycomb Beelines default to using a Honeycomb-specific header format on outgoing requests, but can automatically detect incoming W3C headers and parse them appropriately. In mixed environments where some services are using OpenTelemetry and some are using Beeline, W3C header propagation should be used. To propagate trace context, a parser hook and propagation hook are needed. The parser hook is responsible for reading the trace propagation context out of incoming HTTP requests from upstream services. The propagation hook is responsible for returning the set of headers to add to outbound HTTP requests to propagate the trace propagation context to downstream services.Older versions of Honeycomb Beelines required HTTP parsing hooks to properly parse incoming W3C headers.
Current versions of Honeycomb Beelines can automatically detect incoming W3C headers and parse them appropriately.
Check the release notes for your Beeline version to confirm whether an upgraded version is needed.
http_trace_parser_hook is a function that takes a rack env as an argument and returns a Honeycomb::Propagation::Context.
The rack env is provided to the function so that the author can make decisions about whether to trust the incoming headers based on information contained in the request (perhaps you do not want to accept headers from the public internet).
An http_trace_propagation_hook is a function that takes a Faraday env and a Honeycomb::Propagation::Context as arguments and returns a hash of name-value pairs representing serialized headers.
Similar to the parsing hook described above, the Faraday env and Propagation Context objects are passed to this function so that the author can make decisions about whether to include trace context in the outgoing request (for example, you may not wish to send trace context headers when calling a third-party API).
This is an example of adding these hooks to parse and propagate W3C headers:
Troubleshooting The Beeline
No Traces for a Service
The service name is a required configuration value. If it is unspecified, all trace data will be sent to a default dataset calledunknown_service.
Enable Debug Mode
Add the debug line in the config block.Use a LogClient
By using a Libhoney LogClient when configuring the Ruby Beeline you can send the events to STDOUT.Using ENV Variables to Control Framework Integrations
If you are having issues with a specific integration with Rails, Faraday, Sequel or other frameworks, use the following ENV variables to determine which integration may be causing the issue or to disable it entirely. UseHONEYCOMB_DISABLE_AUTOCONFIGURE=true to stop the Beeline from requiring any of the Beeline integrations.
This will still allow you to use the Beeline but without any of the automatic instrumentation.
You can also use HONEYCOMB_INTEGRATIONS=rails,faraday using a comma-separated list to only load specific integrations.
This will allow you to be more selective about which automatic instrumentation integration you want to use.
The available integration list is: active_support, aws, faraday, rack, rails, railtie, rake, sequel, sinatra.
Customizing Middleware Location in a Rails Application
The Rails framework integration will automatically insert the Rack middleware before theRails::Rack::Logger middleware.
To insert this in some other location, disable the railtie integration as defined above using the ENV variable HONEYCOMB_INTEGRATIONS.
You will then need to insert the Honeycomb::Rails::Middleware into your middleware stack manually.
My Traces Are Showing Missing Root Spans
There can be a number of reasons for missing root spans. One potential reason could be that there is an upstream service, load balancer, or other proxy propagating W3C trace headers as part of your distributed trace. Since beelines accept both Honeycomb and W3C headers, that service propagating a W3C header will cause “missing span” gaps in your trace if the service is not also configured to send telemetry to Honeycomb. The solution is to either instrument that service and configure it to send telemetry to Honeycomb, or to specify in the downstream service’s beeline configuration that only Honeycomb propagation headers should be parsed. To override undesired W3C trace header propagation behavior, configure the Beeline to use anhttp_trace_parser_hook:
Example Event
Below is a sample event from the Ruby Beeline. This example is anhttp_server event, generated when your app handles an incoming HTTP request.
Queries to Try
Here are some examples to get you started querying your app’s behavior:Which Of My App’s Routes Are The Slowest?
GROUP BY:request.pathVISUALIZE:P99(duration_ms)WHERE:name = http_requestORDER BY:P99(duration_ms) DESC
Where Is My App Spending The Most Time?
GROUP BY:nameVISUALIZE:SUM(duration_ms)ORDER BY:SUM(duration_ms) DESC
Which Users Are Using The Endpoint That I Would Like to Deprecate? (Using a Custom Field user.email)
GROUP BY:app.user.emailVISUALIZE:COUNTWHERE:request.path == /my/deprecated/endpoint