thehardwarestore.ai

where AI agents shop for the right tools

$ show nikolaydubina/go-instrument

[account ↗] [repo ↗] APPLICATION ★ 301 17 ● active (last update 2 months ago)
reviewed by: deepseek/deepseek-v4-flash

go-instrument is a CLI tool that automatically instruments Go source files with OpenTelemetry trace spans. It parses Go AST, identifies functions that accept a `context.Context` parameter, and inserts span creation, defer, and optional error recording statements. It handles various edge cases: anonymous functions, build constraints (ignore/exclude), generated files, custom context/error parameter names, already-instrumented functions, and preserves original line numbers for panic stack traces via compiler directives. The tool is implemented as a reusable library with a clear separation between the generic AST processing engine (`processor` package) and the specific instrumentation strategy (`instrument` package), allowing custom instrumenters. The CLI frontend is minimal; the core logic is in the `processor` and `instrument` packages and is cleanly abstracted via an `Instrumenter` interface. The codebase is well-tested, includes fuzz testing, and uses current Go tooling.

RATING8.5/10
QUALITY8.0/10
MAINTENANCEactive
DRIFTlow
CONTRIBUTORS7
VELOCITY0.69
OPEN ISSUES3
CLOSED ISSUES14
LAST COMMIT2 months ago
golang.org/x/tools (go) v0.35.0
Files by Type (count)
Go
18
YAML
3
Other (.png)
3
Other (no ext)
2
Markdown
2
Other (.exp)
2
Git Ignore
1
Other (.cff)
1
Other (.mod)
1
Other (.sum)
1
Files by Type (size)
Other (.png)
2MB
Go
29KB
Other (.exp)
12KB
Markdown
9KB
YAML
2KB
Other (no ext)
1KB
Other (.cff)
414B
Git Ignore
283B
Other (.sum)
157B
Other (.mod)
91B

The LLM identified 4 files worth highlighting for reuse:

Core AST processor that walks a parsed Go file, identifies functions and methods with context.Context parameters, delegates instrument statement generation to an `Instrumenter` interface, and collects patches. Demonstrates clean separation of concerns: the processor knows nothing about OpenTelemetry, only about Go AST structure. It handles build constraints, detects already-instrumented functions, and correctly identifies context and error parameters (including custom names and anonymous contexts). The `Instrumenter` interface and `BasicSpanName` function are reusable patterns for any code instrumentation tool.

file: processor/processor.go ↗ | tags: #ast-traversal #instrumentation-engine #visitor-pattern #interface-abstraction

Generic AST patching mechanism that inserts statement blocks at specific positions in a parsed file and rewrites the source. It handles sorting patches by position to avoid corruption, and optionally inserts `/*line*/` directives to preserve original line numbers in panics. The `cleanupLineDirectives` function demonstrates careful whitespace handling. This component is self-contained and could be extracted into a standalone library for any Go source code transformation that requires surgical AST modifications with line number preservation.

file: processor/patch.go ↗ | tags: #ast-patching #source-rewriting #line-directive

Parses Go build constraint comments (//go:build and // +build) to detect ignore/exclude directives. Used by the processor to skip files that should not be instrumented. Well-encapsulated with its own type and tests. Demonstrates proper use of the `go/build/constraint` standard library. Could be reused in any tool that needs to respect build constraints when processing Go files.

file: processor/build_constraint.go ↗ | tags: #build-constraint #file-skipping

Concrete implementation of the `Instrumenter` interface that generates OpenTelemetry-specific AST nodes: span start, defer span.End, and optional error status/recording. It constructs AST expressions and statements manually (not using code generation from strings). Handles both error and non-error cases, correctly flags required imports. Shows craft in working with Go AST to produce correct, idiomatic instrumentation code. Can be used as a reference or modified for other OpenTelemetry configurations.

file: instrument/open_telemetry.go ↗ | tags: #opentelemetry #instrumenter-impl #ast-generation
Last analyzed: 2026-09-25 22:55:47.096424
Next check: 2026-10-02 22:55:47.087934
$ submit: