Skip to main content
This tutorial takes you from a blank Go module to a working instrumented app. Each step is self-contained — skip ahead if you already know the material.
All examples assume TRULAYER_API_KEY is set in your environment. Install the SDK with go get github.com/trulayer/client-go. Requires Go 1.22 or later.

1. Install the SDK

Add the SDK to your module:
The SDK has zero runtime external dependencies — it uses the Go standard library only.

2. Initialise the client

Construct a *Client once at app startup and defer Shutdown so buffered traces flush before your process exits.
Client is safe for concurrent use. Construct it once and pass it through your application — do not construct a new client per request.

3. Create a trace

A trace represents one unit of work — for example, answering a user question. Call NewTrace, set the input, run your logic, set the output, then call End.
End is non-blocking. It enqueues the trace data and returns immediately; the background batch sender handles the network call.

4. Add spans for sub-steps

Spans break a trace into individual steps. Create each span from the trace, do the work, then call End on the span before moving to the next step.
The context returned by NewSpan carries the span, which lets the SDK establish parent-child relationships automatically when you nest spans.

5. Choose the right span type

Use the SpanType constants to classify each span:

6. Run a complete example

Below is a runnable program combining everything above. It mirrors the basic_trace demo in the SDK repository.

7. Test offline with dry-run mode

Set TRULAYER_DRY_RUN=true to disable all HTTP calls without changing any application code. No API key is required.
The SDK still constructs traces and spans normally — your code runs as usual, but nothing is sent over the network. Use this in CI and unit tests. You can also activate dry-run programmatically by passing an empty API key:

8. Attach metadata and tags

Use SetMetadata on a trace or span to attach arbitrary key-value data visible in the dashboard. Use SetTag for structured string-only key-value pairs (max 20 keys, 64 characters per key and value).
Avoid putting PII or secrets in metadata — it is visible to all members of your team in the dashboard. Pass WithTraceExternalID to correlate the TruLayer trace with your own request ID:

10. Submit feedback

After ingestion you can attach user feedback to a trace. You need the trace ID, which you can capture from t.ID() inside your handler and return it alongside your response.
Valid Label values are "good", "bad", and "neutral". You can also pass a numeric Score (*float64) and arbitrary Metadata.

11. Flush before exit in short-lived scripts

In short-lived command-line programs, call Flush explicitly after your main logic to ensure buffered traces are sent before the process exits. Shutdown does this automatically, but calling Flush mid-process lets you confirm delivery before continuing.
For long-lived services (HTTP servers, gRPC servers), the periodic flush — every two seconds by default — handles most cases. Wire Shutdown into your graceful-shutdown hook to drain the queue cleanly.

12. Cap shutdown time

In production, set a deadline on Shutdown to avoid holding up your process exit:

Next steps