Configuration

Configure the otelc tool and the telemetry produced by instrumented applications.

Você está visualizando a versão em versão em inglês desta página porque ela ainda não foi traduzida. Possui interesse em ajudar? Veja como contribuir.

Configuration happens at two points: build time, where you control how the otelc tool instruments your application, and runtime, where the standard OpenTelemetry environment variables control the telemetry the instrumented application produces.

The otelc command

otelc wraps the Go toolchain. Its subcommands:

CommandPurpose
otelc go …Run a go command (such as go build) with instrumentation applied
otelc setupSet up the environment for instrumentation
otelc cleanupRemove all artifacts created by the setup and build phases
otelc versionPrint the tool version

Flags are passed before the subcommand:

FlagEnvironment variablePurpose
--rules <file>Use a custom instrumentation rules file
--debug, -dOTELC_DEBUG=1Enable debug logging for the build
--work-dir, -wOTELC_WORK_DIRDirectory for working files written during the build

For example, to build with a custom rules file and debug output:

otelc --rules my-rules.yaml --debug go build -o myapp .

Runtime environment variables

Instrumented applications respect the standard OpenTelemetry SDK environment variables for exporters, resources, and service identity, for example:

  • OTEL_SERVICE_NAME: service name reported with telemetry
  • OTEL_EXPORTER_OTLP_ENDPOINT: OTLP endpoint to export to
  • OTEL_RESOURCE_ATTRIBUTES: additional resource attributes

In addition, the following variables control which injected instrumentations are active at runtime:

VariablePurpose
OTEL_GO_ENABLED_INSTRUMENTATIONSComma-separated list of instrumentations to enable, for example nethttp,grpc. When set, only the listed instrumentations are active.
OTEL_GO_DISABLED_INSTRUMENTATIONSComma-separated list of instrumentations to disable.

Custom instrumentation rules

Which code gets instrumented is driven by declarative YAML rules. Each rule names a target package, optionally narrows the match with selectors, and declares what to inject. For example, the following rule calls hook functions at the entry and exit of (*sql.DB).Exec:

instrument_sql_exec:
  target: database/sql
  where:
    func: Exec
    recv: '*DB'
  do:
    - inject_hooks:
        before: BeforeExec
        after: AfterExec
        path: github.com/example/sqlinstr

Pass a custom rules file to the build with --rules. Rules can also ship inside Go packages: a package declares instrumentation with an otel.instrumentation.go (or otelc.tool.go) file and provides its rules in otelc.yml or *.otelc.yml files next to the code, and the tool discovers and loads them when it instruments your application. Rules support several injection mechanisms beyond function hooks, including struct field injection, call-site wrapping, and file addition. For the complete schema and rule type reference, see the instrumentation rules documentation in the repository.