Skip to main content
Odigos provides Go functions to augment and enhance auto-instrumentation. These functions are intended to be used in conjunction with auto-instrumentation to provide enriched functionality and interaction for developers running in an Odigos environment.

Trace and Span Context

The following hooks allow you to access the current trace and span IDs in your application. This is useful when you need to correlate your application’s logs with the distributed traces generated by Odigos. These functions are available in the github.com/odigos-io/odigos/hooks/go module, ie:

Get W3C Trace Context

This function returns the full W3C Trace Context for the current function.

Get Current Trace ID

Returns the current trace ID. If there is no active trace, it returns a zero value.

Get Current Span ID

Returns the current span ID. If there is no active span, it returns a zero value.

Check for zero values

The following helper functions can be used to check if the hooks returned a zero value. Zero values indicate that no trace is currently active at the call site of the hook.

Gin Middleware

Odigos can instrument Gin middleware using a provided wrapper handler from github.com/odigos-io/odigos/hooks/go/gin:
For example, see the following application that uses 2 middleware handlers, LoggerMiddleware and AuthMiddleware:
The created spans will list the middleware function in the span attribute odigos.gin.middleware.func. In the above example, these are main.main.LoggerMiddleware.func2 and main.main.AuthMiddleware.func3.

Example Application

First, follow the steps in the Quickstart to set up a Kind cluster, install Jaeger, and install Odigos (note: Go hooks are only available with an enterprise token, which you can set in this walkthrough by installing with --onprem-token=<YOUR_TOKEN>). Next create a folder for your sample app and initialize the Go module:
Now, here’s a simple example using net/http that demonstrates how to use the Go hooks:
Initialize the Go module:
Create the following Dockerfile:
Build and push the image:
Create the following deployment.yaml:
Apply the deployment using the IMAGE_TAG environment variable you defined above:
Now, follow the Quickstart steps to Build a pipeline in order to instrument the sample application. To generate traces, connect to the service with kubectl port-forward:
And in another terminal, generate spans with:
And you will see the current trace and span ID returned:
Verify this is the same trace context shown in the Jaeger UI:
And open localhost:16686 in your browser.

Troubleshooting

If you’re not seeing trace or span IDs:
  1. Make sure Odigos is properly configured and running
  2. Verify that your application is being instrumented by Odigos
  3. Check that you’ve initialized the hooks correctly
  4. Ensure your application is receiving requests that are part of a trace