This setup guide is for v1 of the Go SDK (currently in beta).
- Get detailed metrics on API usage, errors, and performance
- Track API adoption and usage by individual consumers
- Log individual API requests, responses, and correlated application logs
- See what’s causing slow API requests with traces
- Monitor uptime and set up custom alerts
Requirements
Requires Go 1.25+ and either Fiber 2.51.0+ or Fiber 3.x.Create app
To get started, create a new app in the Apitally dashboard and select Fiber as your framework.
You can also configure the environments (e.g. prod and dev) for your app, or simply accept the defaults.
After submitting, you will see tailored setup instructions for your app. These include your write token and code snippets you can copy and paste into your project.
The write token (
apt_...) provided in the setup instructions uniquely identifies your app for the purpose of data ingestion only. It does not grant any kind of read access to your data.Install the SDK
Install the Apitally SDK as a dependency in your project.Initialize Apitally
Callapitally.Init with your Fiber app and a configuration containing the write token and other configuration options immediately after adding the recover middleware, before registering other middleware and routes.
APITALLY_WRITE_TOKEN and APITALLY_ENV environment variables instead of setting WriteToken and Env in code.
Deploy your application with these changes, or restart it if you’re testing locally.
The basic setup is now complete. Metrics and logs will start appearing in the Apitally
dashboard.
Identify consumers
Consumers are the users or applications calling your API. Identifying them lets you analyze and filter API traffic by consumer in Apitally. Callapitally.SetConsumer with the request context during request handling to associate the current request with a consumer identifier. You can call it wherever the consumer is known, such as in a route handler, existing middleware, or authentication code.
You can also provide a display name, group, and custom attributes for each consumer.
The examples on this page use Fiber v3. On Fiber v2, pass
c.UserContext() instead of c.Context() as the request context.Capture headers and bodies
Only response headers are captured by default. You can opt in to capture request headers as well as request and response bodies when initializing Apitally.Capture application logs
To capture application logs written withlog/slog, wrap your log handler with apitally.NewSlogHandler. It passes every record on to your handler unchanged.
Capture validation errors
If you validate request data with go-playground/validator, pass the error toapitally.CaptureError with the request context. Apitally then counts validation errors by endpoint and field, so you can see which fields clients most often get wrong.
ErrorHandler responds with a 400 or 422 status code, Apitally captures it automatically. You don’t need to call apitally.CaptureError in that case.
Mask sensitive information
The SDK automatically masks common sensitive query parameters, headers, and body fields. To mask additional data, pass regular expressions matching query parameter names, header names, or body field names toMaskQueryParams, MaskHeaders, or MaskBodyFields, respectively. Matching is case-insensitive.
MaskRequestBody and MaskResponseBody callbacks to mask individual fields or entire bodies using custom logic. Use MaskLogRecord to mask or drop application log records.
More information about masking is available here.
Sample requests
If your application receives a lot of traffic, you may want to sample requests to stay within your request logs quota. Metrics will continue to count every request regardless of sampling. UseSampleRate to capture logs and traces for a fraction of requests.
SampleOnRequest and SampleOnResponse callbacks to make sampling decisions using custom logic.
More information about sampling is available here.
Instrument third-party libraries
Instrumenting third-party libraries adds details about database queries, HTTP calls to external services, and other operations to your request traces. This lets you see how these operations contribute to API response times. Use OpenTelemetry instrumentation libraries to instrument the libraries you want to trace. For example, to trace database queries made withdatabase/sql, install otelsql:
otelsql.Open instead of sql.Open, and pass the request context to your queries:
Graceful shutdown
Apitally sends data in the background at regular intervals. To deliver the remaining data when your application exits, callapitally.Shutdown after your Fiber app has stopped.