<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Azhar Momin - #go</title><description>Posts tagged with go on Azhar Momin.</description><link>https://azharmomin.dev/</link><language>en-us</language><atom:link href="https://azharmomin.dev/tags/go/rss.xml" rel="self" type="application/rss+xml"/><item><title>Building Import-Driven Instrumentation for otelc</title><link>https://azharmomin.dev/posts/building-import-driven-instrumentation-for-otelc/</link><guid isPermaLink="true">https://azharmomin.dev/posts/building-import-driven-instrumentation-for-otelc/</guid><description>How I got involved with OpenTelemetry Go Compile Instrumentation through LFX Mentorship, and the work I did on import-driven instrumentation selection.</description><pubDate>Wed, 12 Aug 2026 06:30:00 GMT</pubDate><content:encoded>&lt;p&gt;&lt;img src=&quot;https://azharmomin.dev/_astro/otelc-import-driven-banner.VRw3J8x7.png&quot; alt=&quot;Import-Driven Instrumentation for otelc banner&quot; /&gt;&lt;/p&gt;&lt;p&gt;In &lt;a href=&quot;https://azharmomin.dev/posts/my-lfx-mentorship-so-far-and-what-id-tell-future-mentees/&quot;&gt;my last post&lt;/a&gt;, I wrote about how I got into OpenTelemetry Go Compile
Instrumentation and applied to LFX Mentorship. This one is about the
actual work, what I built, the bugs I ran into, and how I fixed them.&lt;/p&gt;
&lt;h2 id=&quot;why-otelc-specifically&quot;&gt;Why otelc specifically?&lt;/h2&gt;
&lt;p&gt;I’m interested in both low-level tooling and observability, and otelc
sits right at the intersection of the two. That’s really what pulled me
toward this project instead of OpenTelemetry in general.&lt;/p&gt;
&lt;h2 id=&quot;what-is-otelc-briefly&quot;&gt;What is otelc, briefly?&lt;/h2&gt;
&lt;p&gt;otelc instruments Go code at compile time. Instead of requiring developers to
manually instrument their code, otelc rewrites the program during compilation
so that instrumentation is inserted automatically. The rest of this post assumes
a little bit of that context, so here’s the short version before I get into
what I actually worked on.&lt;/p&gt;
&lt;h2 id=&quot;what-i-worked-on&quot;&gt;What I worked on&lt;/h2&gt;
&lt;h3 id=&quot;import-driven-instrumentation-selection&quot;&gt;Import-driven instrumentation selection&lt;/h3&gt;
&lt;p&gt;otelc ships with a built-in set of instrumentations, each made up of
modules and rule files. But there was no good way to bring in third-party
instrumentations easily, and no way to disable a built-in instrumentation
at compile time (you could only toggle them at runtime).&lt;/p&gt;
&lt;p&gt;This came up in a SIG meeting, and was tracked in
&lt;a href=&quot;https://github.com/open-telemetry/opentelemetry-go-compile-instrumentation/issues/567&quot;&gt;this issue&lt;/a&gt;.
Orchestrion, Datadog’s own compile-time instrumentation tool for Go,
already had a solution: an &lt;code&gt;orchestrion.tool.go&lt;/code&gt; file that developers
could use to declare instrumentation explicitly. We used that as a
starting point and built something similar for otelc.&lt;/p&gt;
&lt;p&gt;The mechanism works like this: otelc looks for a file (&lt;code&gt;otel.instrumentation.go&lt;/code&gt;,
or &lt;code&gt;otelc.tool.go&lt;/code&gt; as an alias) containing a list of blank imports. This
file isn’t included in the actual build (it’s marked with a &lt;code&gt;tools&lt;/code&gt;
build tag, following the standard &lt;code&gt;tools.go&lt;/code&gt; convention), but Go still
tracks its imports in &lt;code&gt;go.mod&lt;/code&gt;. otelc reads this file before building,
loads the imported packages using &lt;code&gt;golang.org/x/tools/go/packages&lt;/code&gt;, and scans
each package’s directory for rule files (&lt;code&gt;otelc.yaml&lt;/code&gt;, &lt;code&gt;*.otelc.yaml&lt;/code&gt;)
or another &lt;code&gt;otel.instrumentation.go&lt;/code&gt;, allowing instrumentation packages
to bundle together other instrumentation packages. Those rule files
define how to instrument that package.&lt;/p&gt;
&lt;p&gt;Here’s roughly what one of these files looks like in practice:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light-high-contrast github-dark&quot; style=&quot;--shiki-light:#0e1116;--shiki-dark:#e1e4e8;--shiki-light-bg:#ffffff;--shiki-dark-bg:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;go&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#66707B;--shiki-dark:#6A737D&quot;&gt;//go:build tools&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A0111F;--shiki-dark:#F97583&quot;&gt;package&lt;/span&gt;&lt;span style=&quot;--shiki-light:#702C00;--shiki-dark:#B392F0&quot;&gt; tools&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#A0111F;--shiki-dark:#F97583&quot;&gt;import&lt;/span&gt;&lt;span style=&quot;--shiki-light:#0E1116;--shiki-dark:#E1E4E8&quot;&gt; (&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0E1116;--shiki-dark:#E1E4E8&quot;&gt;    _ &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032563;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#702C00;--shiki-dark:#B392F0&quot;&gt;go.opentelemetry.io/otelc/instrumentation/net/http/client&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032563;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0E1116;--shiki-dark:#E1E4E8&quot;&gt;    _ &lt;/span&gt;&lt;span style=&quot;--shiki-light:#032563;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;--shiki-light:#702C00;--shiki-dark:#B392F0&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;--shiki-light:#032563;--shiki-dark:#9ECBFF&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span style=&quot;--shiki-light:#0E1116;--shiki-dark:#E1E4E8&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;mermaid-block&quot; data-mermaid-block=&quot;&quot;&gt;&lt;pre class=&quot;mermaid-source-raw&quot; hidden&gt;flowchart TD
    A[&quot;otel.instrumentation.go&amp;#x3C;br/&gt;(blank imports, tools build tag)&quot;] --&gt; B[&quot;otelc reads imports&quot;]
    A -.-&gt; C[&quot;Imports are still tracked in go.mod&quot;]
    B --&gt; D[&quot;Load packages&amp;#x3C;br/&gt;golang.org/x/packages&quot;]
    D --&gt; E[&quot;Scan package directories&amp;#x3C;br/&gt;otelc.yaml / *.otelc.yaml&amp;#x3C;br/&gt;or nested otel.instrumentation.go&quot;]
    E --&gt; F[&quot;Apply instrumentation rules&quot;]&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The naming itself was already decided in a UX design document from before
I joined the project, so that part was settled. I’d originally written this
as a single PR, but it ended up being around 5,000 lines, which was far
too large to review properly. Reviewers asked me to split it into smaller
PRs, so I did. During that process, one review comment asked for an integration
test covering a recursive instrumentation path, where one &lt;code&gt;otel.instrumentation.go&lt;/code&gt;
file references another. Writing that test surfaced a real bug: otelc was only
looking for hook code files inside its own embedded bundle, not in packages loaded
from external import paths. If that comment hadn’t come up, this bug might
have gone unnoticed for a while. I fixed it by updating otelc to load hook code
through &lt;code&gt;golang.org/x/tools/go/packages&lt;/code&gt; as well. The implementation landed across a couple of
PRs: the hook loading fix in &lt;a href=&quot;https://github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pull/617&quot;&gt;#617&lt;/a&gt;,
followed by the core import-driven instrumentation support in
&lt;a href=&quot;https://github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pull/612&quot;&gt;#612&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;the-initinstrumentation-deadlock&quot;&gt;The initInstrumentation deadlock&lt;/h3&gt;
&lt;p&gt;Every instrumentation in otelc followed the same rough pattern: lazily
set up the OTel SDK (if it wasn’t already running) and start runtime
metrics, guarded by a &lt;code&gt;sync.Once&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This caused a deadlock. Here’s what happened: when
a user’s code called &lt;code&gt;grpc.NewClient&lt;/code&gt;, the gRPC instrumentation hook would
call &lt;code&gt;initInstrumentation&lt;/code&gt;, which runs inside a &lt;code&gt;sync.Once.Do&lt;/code&gt;. If the
user had their OTLP exporter configured to use gRPC, the SDK setup inside
that same &lt;code&gt;Once.Do&lt;/code&gt; would itself call &lt;code&gt;grpc.NewClient&lt;/code&gt;, which tried to
call &lt;code&gt;initInstrumentation&lt;/code&gt; again, re-entering the same &lt;code&gt;Once.Do&lt;/code&gt; block
that hadn’t finished running yet. That’s a deadlock.&lt;/p&gt;
&lt;div class=&quot;mermaid-block&quot; data-mermaid-block=&quot;&quot;&gt;&lt;pre class=&quot;mermaid-source-raw&quot; hidden&gt;sequenceDiagram
    participant U as User code
    participant G as gRPC hook
    participant I as initInstrumentation&amp;#x3C;br/&gt;(sync.Once)
    participant S as OTel SDK setup

    U-&gt;&gt;G: grpc.NewClient()
    G-&gt;&gt;I: Once.Do(...)
    I-&gt;&gt;S: SetupOTelSDK()
    S-&gt;&gt;G: grpc.NewClient() (OTLP exporter)
    G-&gt;&gt;I: Once.Do(...)
    Note over I: sync.Once is still&amp;#x3C;br/&gt;executing from the&amp;#x3C;br/&gt;first call -&gt; deadlock&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;My first fix was simpler than it should have been: check the value of
&lt;code&gt;OTEL_EXPORTER_OTLP_ENDPOINT&lt;/code&gt; and skip instrumentation for any
&lt;code&gt;grpc.NewClient&lt;/code&gt; call pointed at that endpoint. That worked for the
obvious case, but it broke down as soon as the two values didn’t match
exactly, since the values could point at the same collector without
matching as strings at all:&lt;/p&gt;
&lt;pre class=&quot;astro-code astro-code-themes github-light-high-contrast github-dark&quot; style=&quot;--shiki-light:#0e1116;--shiki-dark:#e1e4e8;--shiki-light-bg:#ffffff;--shiki-dark-bg:#24292e; overflow-x: auto;&quot; tabindex=&quot;0&quot; data-language=&quot;text&quot;&gt;&lt;code&gt;&lt;span class=&quot;line&quot;&gt;&lt;span&gt;endpoint = &quot;http://collector-localhost:4317&quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;line&quot;&gt;&lt;span&gt;target   = &quot;localhost:4317&quot;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This kept happening because I was relying on dumb string comparison to
detect whether the two calls pointed at the same target. Instead of
trying to make that comparison smarter, I took a different approach.&lt;/p&gt;
&lt;p&gt;The fix was to stop having every instrumentation independently lazy-init
the SDK. Instead, I moved that startup logic out into a single injected
rule that runs once in the user’s main package, before any instrumentation
hook runs. Every hook can now assume the SDK is already set up by the time
it runs, so there’s no endpoint comparison to get wrong in the first
place. It also meant third-party rules don’t need to follow the old init pattern at all,
they can simply assume the SDK is already available, which fit nicely with the
import-driven instrumentation work happening around the same time. Users who
want to bring their own tracer or meter provider can also remove this rule and wire
up their own. The implementation landed in &lt;a href=&quot;https://github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pull/625&quot;&gt;#625&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id=&quot;otelc-pin-and-auto-pinning&quot;&gt;otelc pin and auto-pinning&lt;/h3&gt;
&lt;p&gt;The second thing I worked on was the &lt;code&gt;otelc pin&lt;/code&gt; command, which generates
an &lt;code&gt;otel.instrumentation.go&lt;/code&gt; file based on a user’s dependency graph,
inspired by Orchestrion’s own &lt;code&gt;pin&lt;/code&gt; command. Orchestrion’s version points
to a single package that bundles every integration together. I changed
this so &lt;code&gt;otelc pin&lt;/code&gt; looks at the user’s actual dependencies and infers
which instrumentations are relevant, rather than pulling in everything.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;otelc pin&lt;/code&gt; also keeps the generated file clean by automatically removing
imports that don’t resolve to any rule files (or other
&lt;code&gt;otel.instrumentation.go&lt;/code&gt; files). The result is an explicit list of the
instrumentations that are actually relevant to the project.&lt;/p&gt;
&lt;p&gt;Auto-pinning was also mentioned in the original issue, but initially I
wasn’t convinced it was necessary. If a project didn’t have an
&lt;code&gt;otel.instrumentation.go&lt;/code&gt; file, otelc could simply fall back to its default
instrumentations. Generating a temporary file just to represent that default
set seemed like an unnecessary extra step.&lt;/p&gt;
&lt;p&gt;While testing otelc against a project using an older version of gRPC, I ran into
another problem. I already had a fix open for that problem, but auto-pinning
addressed its underlying cause in a much cleaner way.&lt;/p&gt;
&lt;p&gt;At the time, otelc’s build process matched instrumentation rules first,
then added the matched instrumentation modules to &lt;code&gt;go.mod&lt;/code&gt; and ran
&lt;code&gt;go mod tidy&lt;/code&gt;. That dependency resolution could bump the project’s gRPC
version. The instrumentation rules had already been selected against the
old version, though, so otelc could end up building against a dependency
graph different from the one it had used to decide which rules to apply
which resulted in the build failing.&lt;/p&gt;
&lt;p&gt;My original fix was to run the setup process in multiple passes until the
dependency graph stopped changing. It worked, but auto-pinning turned out to
solve the same problem much more cleanly.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;otelc go build&lt;/code&gt; now generates a temporary &lt;code&gt;otel.instrumentation.go&lt;/code&gt; early
when the user hasn’t provided one. Dependencies are resolved and added to
&lt;code&gt;go.mod&lt;/code&gt; as part of that pinning phase, before rule matching happens. By
the time otelc selects instrumentation rules, it’s looking at the dependency
versions that will actually be used for the build. The setup phase that
follows no longer needs to modify the dependency graph, so the selected
rules stay valid.&lt;/p&gt;
&lt;div class=&quot;mermaid-block&quot; data-mermaid-block=&quot;&quot;&gt;&lt;pre class=&quot;mermaid-source-raw&quot; hidden&gt;flowchart TB
    subgraph Before
        direction LR
        B1[&quot;Match rules&quot;] --&gt; B2[&quot;Add deps to go.mod&amp;#x3C;br/&gt;go mod tidy&quot;] --&gt; B3[&quot;Rules may now be stale&quot;]
    end
    subgraph After
        direction LR
        A1[&quot;Pin phase&amp;#x3C;br/&gt;Resolve deps early&quot;] --&gt; A2[&quot;Match rules against final&amp;#x3C;br/&gt;dependency graph&quot;] --&gt; A3[&quot;Setup phase&amp;#x3C;br/&gt;Rules stay valid&quot;]
    end&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This also gives us a cleaner path toward
&lt;a href=&quot;#whats-next&quot;&gt;registry-backed integration search&lt;/a&gt;. If otelc eventually
discovers compatible instrumentations from the &lt;a href=&quot;https://github.com/open-telemetry/opentelemetry-ecosystem-explorer&quot;&gt;OpenTelemetry Ecosystem Explorer&lt;/a&gt;
registry, the same pinning step can resolve those integrations and their
dependencies before rule matching, rather than requiring the rest of the build
pipeline to care where an instrumentation came from. I talk more about that idea
in the registry-backed integration section below.&lt;/p&gt;
&lt;p&gt;One rough edge that’s still open is that &lt;code&gt;otelc pin&lt;/code&gt; currently adds
&lt;code&gt;replace&lt;/code&gt; directives pointing to local paths in &lt;code&gt;go.mod&lt;/code&gt;, which makes
checking the generated file into version control awkward. Decoupling
instrumentations from the tool itself, also described below, should fix
that. The initial implementation landed in &lt;a href=&quot;https://github.com/open-telemetry/opentelemetry-go-compile-instrumentation/pull/655&quot;&gt;#655&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&quot;whats-next&quot;&gt;What’s next&lt;/h2&gt;
&lt;p&gt;A few things I’m looking at for the rest of the mentorship:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Decoupling instrumentations from otelc’s embedded bundle.&lt;/strong&gt; Right
now, instrumentation packages are embedded directly into the otelc
binary. That means adding a new instrumentation requires a new release
of the tool, even if it’s otherwise fully compatible. Decoupling this
would also let us build a proper dependency graph between
instrumentations (for example, automatically pulling in a runtime/GLS
instrumentation whenever the OTel SDK instrumentation is used), and
would let users check reproducible &lt;code&gt;otel.instrumentation.go&lt;/code&gt; files
into their own repos without local path replaces.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Registry-backed integration search.&lt;/strong&gt; Instead of only looking at the
embedded bundle, otelc could look up compatible instrumentations
through the &lt;a href=&quot;https://github.com/open-telemetry/opentelemetry-ecosystem-explorer&quot;&gt;OpenTelemetry Ecosystem Explorer&lt;/a&gt;
registry, which would let third-party instrumentations get picked up
automatically once published there. This is currently being
researched, see
&lt;a href=&quot;https://github.com/open-telemetry/opentelemetry-ecosystem-explorer/issues/916&quot;&gt;this issue&lt;/a&gt;
for the open questions around how Go compile-time instrumentations
should be represented there and how otelc would consume that data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Supporting &lt;code&gt;.otel.yml&lt;/code&gt; files.&lt;/strong&gt; Similar in spirit to
&lt;code&gt;otel.instrumentation.go&lt;/code&gt;, but without tracking dependencies in
&lt;code&gt;go.mod&lt;/code&gt;. Useful for customizing instrumentations without wanting them
tracked in version control, though it won’t produce fully reproducible
builds.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;acknowledgements&quot;&gt;Acknowledgements&lt;/h2&gt;
&lt;p&gt;The work described here went through a lot of review. Thanks to
&lt;a href=&quot;https://github.com/kakkoyun&quot;&gt;Kemal Akkoyun&lt;/a&gt; for raising the original
issue that led to import-driven instrumentation selection, and to both
&lt;a href=&quot;https://github.com/kakkoyun&quot;&gt;Kemal&lt;/a&gt; and
&lt;a href=&quot;https://github.com/darccio&quot;&gt;Dario Castañé&lt;/a&gt; for reviewing the PRs linked
throughout this post, including the review comment that led me to the
recursive-instrumentation bug. Thanks also to everyone else in the
OpenTelemetry Go Compile Instrumentation community who reviewed and
discussed this work along the way.&lt;/p&gt;</content:encoded><category>lfx</category><category>opentelemetry</category><category>go</category><category>observability</category><category>open-source</category></item></channel></rss>