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.Documentation Index
Fetch the complete documentation index at: https://docs.trulayer.ai/llms.txt
Use this file to discover all available pages before exploring further.
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: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. CallNewTrace, 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 callEnd on the span before moving to the next step.
NewSpan carries the span, which lets the SDK establish parent-child relationships automatically when you nest spans.
5. Choose the right span type
Use theSpanType constants to classify each span:
| Constant | Use for |
|---|---|
trulayer.SpanTypeLLM | Language model calls |
trulayer.SpanTypeTool | Tool or function calls |
trulayer.SpanTypeRetrieval | Vector search, document fetch |
trulayer.SpanTypeOther | Anything else |
6. Run a complete example
Below is a runnable program combining everything above. It mirrors thebasic_trace demo in the SDK repository.
7. Test offline with dry-run mode
SetTRULAYER_DRY_RUN=true to disable all HTTP calls without changing any application code. No API key is required.
8. Attach metadata and tags
UseSetMetadata 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).
9. Link to an upstream request ID
PassWithTraceExternalID 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 fromt.ID() inside your handler and return it alongside your response.
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, callFlush 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.
Shutdown into your graceful-shutdown hook to drain the queue cleanly.
12. Cap shutdown time
In production, set a deadline onShutdown to avoid holding up your process exit:
Next steps
- Read the Go SDK reference for every method and option.
- Browse the API reference for the full HTTP API surface.
- See traces and spans for conceptual background.
- Learn how to submit feedback from your production app.