Making Instrumentation Extensible
Tags: instrumentation, observability, sre, devops
Published: 2020-12-09 20:06:08

<span style="font-weight: 400;">Observability-driven development requires both rich query capabilities and sufficient instrumentation in order to capture the nuances of developers' intention and useful dimensions of cardinality. When our systems are running in containers, we need an equivalent to our local debugging tools that is as easy to use as Printf and as powerful as gdb. We should empower developers to write instrumentation by ensuring that it's easy to add context to our data, and requires little maintenance work to add or replace telemetry providers after the fact. Instead of thinking about individual counters or log lines in isolation, we need to consider how the telemetry we might want to transmit fits into a wider whole.</span>

<img class="aligncenter wp-image-4809 size-medium" src="https://www.honeycomb.io/wp-content/uploads/2019/06/bee-extension-350x250.jpg" alt="photo of a chain of bees connecting two parts of a hive" width="300" height="214" />

<span style="font-weight: 400;">Aggregated counters, gauges, and histograms can provide us with information broken down by host or endpoint, but not necessarily higher cardinality fields that we need for a rich understanding of our distributed systems. Automatic instrumentation of a language server framework, such as framework support for Node.js Express or Go http.Server in <a href="https://www.honeycomb.io/auto-instrumentations/">Honeycomb's Beelines</a>, can only provide a modest amount of context. It will capture request header fields such as URL and response durations/error codes, but not anything from the business logic or involving the logged-in user's metadata. Because observability requires the ability to understand the impact of user behavior upon our applications, we cannot just stop with collecting surface level data. Thus, we'll need to make changes to our code to instrument it.</span>
<h2>Instrumentation should be reusable</h2>
<span style="font-weight: 400;">Typically, instrumenting code involves adding a vendor's library or a standard package like OpenCensus or slf4j to one's dependencies, then calling the library directly from instrumented code. If multiple providers and kinds of telemetry (e.g. logs, metrics, traces, events…) are in use, calls to each wind up sprinkled across the codebase. But should we have to re-instrument our entire codebase every time we gain access to new methods of data aggregation/visualization or change observability providers? Of course not. This gives rise to the need to separate observability plumbing from your business logic, or domain-specific code.</span>

<span style="font-weight: 400;">To address this problem of abstracting instrumentation, Tyler Treat envisions a solution involving centralized inter-process collection in "</span><a href="https://bravenewgeek.com/the-observability-pipeline/"><span style="font-weight: 400;">The Observability Pipeline</span></a><span style="font-weight: 400;">", and Pete Hodgson suggests abstracting collection in-process in "</span><a href="https://martinfowler.com/articles/domain-oriented-observability.html"><span style="font-weight: 400;">Domain Oriented Observability</span></a><span style="font-weight: 400;">". Tyler's article explains creating structured events, and then streaming them to an out of process service that can aggregate them and send them onwards to a variety of instrumentation sinks. Pete's article suggests creating a separate class for handling the vendor-specific pieces of instrumentation, but still relies upon tight coupling between the instrumentation code and the domain-specific code--e.g. creating a method in the instrumentation code to handle each potential property we might want to record about an event (e.g. <code>discountCodeApplied()</code>, <code>discountLookup{Failed,Succeeded}()</code>).</span>
<h2>Why not both?</h2>
<span style="font-weight: 400;">However, there's a simpler, within-process approach that is easier for developers to understand, test, configure, maintain, and operate. It's a fusion of that described by Pete in "Event-Based Observability" and "Collecting Instrumentation Context", and by Tyler's distributed event buffering solution. With the improved solution, we neither need to have an advanced understanding of mocking functions and classes, nor do we need to operate a Kafka pipeline from day 0. Instead, we just generate and consume <a href="https://www.honeycomb.io/blog/how-are-structured-logs-different-from-events/">structured events</a>.</span>

<img class="aligncenter wp-image-4811 size-medium" src="https://www.honeycomb.io/wp-content/uploads/2019/06/bee-chain-350x250.jpg" alt="photo of a vertical chain of bees constructing a hive" width="300" height="214" />

<span style="font-weight: 400;">Within each span of work done in the domain-specific business logic, we populate a weakly-typed context dictionary with key/value pairs added from within instrumented code, as well as the default standard contextual fields (e.g. requestId, startTime, endTime, etc). Child units of work become separate contexts and spans, with appropriate fields (e.g. parentId, requestId) templated (or "partially applied" in Pete's words) from the parent context/span. Adding telemetry becomes as easy as Printf for developers -- it's just setting a <code>ctx[key] = val</code> only for keys and values relevant to your code. We no longer need to create one function call to the instrumentation adapter for each telemetry action. Using Pete's example, we might set <code>discountCode =&gt; FREESHIPPING</code>, <code>responseCode =&gt; 403</code>, or <code>discountLookupSucceeded =&gt; {true,false,nil}</code> within </span><i><span style="font-weight: 400;">one</span></i><span style="font-weight: 400;"> event instead of making the multiple function calls above, or emitting multiple distinct "Announcement" objects for only one work unit. Writing tests to validate that the generated context map is correct becomes straightforward to do in table-based testsuites (e.g. <code>go functest</code>), rather than requiring mocking functions and classes.</span>

<span style="font-weight: 400;">Once the work unit finishes, its context dictionary is sent in-process to the instrumentation adapter where any number of listeners can interpret it. Each listener sees the context maps for each received event, decides whether it's relevant to it, and if so, translates it according to its own rules into metrics, traces/events/structured logs, or human-readable logs. We no longer need to duplicate calls to the same instrumentation provider from each kind of telemetry function call, but can create single listeners for each common metric (e.g. response time metrics collection, response code) that act on a wider range of events. We can then measure the correctness of listeners, ensuring that each processor is only interested in the correct set of structured events, and dispatches them to the upstream structured event, log, metric, or trace provider(s)' APIs appropriately.</span>
<h2>Correspondence is more useful when it's about the outcome</h2>
<span style="font-weight: 400;">Unlike Tyler's streaming design there need not be a 1:1 correspondence between listeners/routers and instrumentation sinks. Instead, the correspondence is between the action we'd like to coalesce or report on, and what related calls we make -- e.g. performing more than one metric counter increment, etc. to the same sink, or even scattering increments across many different sinks if we're transitioning between providers. This makes the code much more testable, as it's focused on the intent of "record these values from this specific kind of event, to whatever Sinks are relevant", rather than a catch-all of "duplicate everything in the kitchen we do in Sink A in Sink B instead". And the value of event stores such as Honeycomb quickly becomes clearer -- because you don't </span><i><span style="font-weight: 400;">have</span></i><span style="font-weight: 400;"> to do anything different to aggregate or process each such structured event, only pass it on to us directly. Let us worry about how to efficiently query the data when you ask a question, such as <code>P99(duration_ms)</code> or <code>COUNT WHERE err exists GROUP BY customer_id ORDER BY COUNT desc</code>.</span>

<span style="font-weight: 400;">Decoupling event creation from event consumption, even within the same process, is a great step between instrumentation spaghetti and needing a Kafka, Kinesis or PubSub queue. Never create a distributed system unless you need to, and run as few distributed systems as possible. Same-process structured event creation and structured event consumption is super easy to work with, test, and reason about, to boot! As you grow and your needs scale, you may wind up reaching for that Kafka queue. But you'll have an easier migration path, if so.</span>
<h2>Ideas for future-proofing</h2>
<span style="font-weight: 400;">How does this relate to OpenTelemetry née Open{Census,Tracing}? Despite the creation of the new consensus standard, the ongoing transition to OpenTelemetry is proof indeed that we ought to future-proof our work by ensuring we can switch to and from instrumentation providers, including those that do not support the current newest standard, without further breaking domain code. Instead of using the OpenTelemetry API directly within your domain-specific code, it still may be wise to use one context/span propagation library of your choice (which could still be OTel's), and write an InstrumentationAdapter that passes data it receives through to OpenTelemetry's metrics &amp; trace consumers, as well as to legacy and future instrumentation providers.</span>

<span style="font-weight: 400;">I hope that this article was helpful! If you're looking for more detailed examples of how Honeycomb Beelines work, check out our <a href="https://github.com/honeycombio/examples">Examples repo in Github </a>, such as <a href="https://github.com/honeycombio/examples">this</a> <a href="https://github.com/honeycombio/examples/blob/master/golang-gatekeeper">example of using our Beeline for Go alongside custom instrumentation</a>. </span>

<hr />

Looking to find out more? Get started with [Honeycomb for free](https://ui.honeycomb.io/signup?&utm_source=Devto&utm_Devto=blog&utm_campaign=referral&utm_keyword=%7Bkeyword%7D&utm_content=making-instrumentation-extensible).