Tracing
Find which operation ran inside an HTTP request, then connect its spans to your logs. When tracing is active, the HTTP runtime starts a server span and reads W3C traceparent and tracestate headers.
A distributed trace joins spans across services. An Inspect is a bounded record of one execution in Lighthouse, with richer local details. Several Inspects can belong to one distributed trace.
Start with Local Capture
Use the Web API app from Quickstart. Stop forj dev before changing its files. Set these values in .env for this collector-free example.
LIGHTHOUSE_ENABLED=true
LIGHTHOUSE_INSPECT_ENABLED=true
LIGHTHOUSE_INSPECT_SAMPLE_RATE=1.0
OTEL_TRACES_EXPORTER=none
OTEL_LOGS_EXPORTER=noneKeep the generated LIGHTHOUSE_URL and LIGHTHOUSE_SECRET. Inspect capture defaults to off, while .env.local enables it in new projects. With capture enabled, the default OTel sampler is parentbased_always_on. The request below supplies a sampled parent.
Add a Span and a Contextual Log
Replace internal/hello/controller.go with this file. Replace example.com/photodrop with your module path from go.mod. The existing controller provider and /api/v1 route group already wire this controller into the app.
package hello
import (
"net/http"
"example.com/photodrop/internal/logger"
"github.com/goforj/web"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
// Controller exposes the greeting route.
type Controller struct {
logger *logger.AppLogger
}
// NewController receives the app's logger through Wire.
func NewController(logger *logger.AppLogger) *Controller {
return &Controller{logger: logger}
}
// Routes keeps the greeting under the app's existing API route group.
func (c *Controller) Routes() []web.Route {
return []web.Route{
web.NewRoute(http.MethodGet, "/hello", c.Hello),
}
}
// Hello adds application work to the request's existing trace.
func (c *Controller) Hello(r web.Context) error {
ctx, span := otel.Tracer("photodrop/hello").Start(r.Context(), "hello.greet")
defer span.End()
c.logger.WithContext(ctx).Info().Msg("Greeting prepared")
return r.JSON(http.StatusOK, map[string]string{
"message": "Hello, World!",
"trace_id": trace.SpanFromContext(ctx).SpanContext().TraceID().String(),
})
}hello.greet is a child of the HTTP server span. WithContext(ctx) associates the log with that child span. Pass ctx into service, database, and other calls inside the span. End the span after its work completes. For failures, use the standard OTel RecordError and SetStatus APIs, with error text safe for the destination.
The response exposes the active OTel trace ID for this example. It does not expose an Inspect lookup ID.
Verify the Request
From the Project root, start the app again.
forj devOpen the Lighthouse URL printed by forj dev. Keep its browser connection open, then select the HTTP request Inspect view. Local capture requires an active Lighthouse connection. With external export disabled, the HTTP tracing path can skip recording when Lighthouse has no connected browser.
In another terminal, send this request. The fixed IDs are example values for local verification.
curl -sS \
-H 'traceparent: 00-11111111111111111111111111111111-2222222222222222-01' \
http://localhost:3000/api/v1/hello
# {"message":"Hello, World!","trace_id":"11111111111111111111111111111111"}Find Greeting prepared in the app log. Its trace_id matches the response, and its span_id identifies hello.greet. In Lighthouse, select the completed GET /api/v1/hello Inspect after its delivery batch arrives. Its timeline contains the greeting span. Its OTel trace ID matches the response and log.
The Inspect record's trace_id is its unique lookup ID. Its otel_trace_id is the distributed trace ID. Use the OTel value when searching a tracing backend. Repeating the request can create another Inspect with the same OTel trace ID and a different lookup ID.
Export Traces to a Backend
Outside the generated local Grafana setup, export stays off until configured. To use an existing OTLP HTTP/protobuf receiver, replace the example export setting in deployment configuration and restart the app.
OTEL_SERVICE_NAME=photodrop-api
OTEL_TRACES_EXPORTER=otlp
OTEL_EXPORTER_OTLP_ENDPOINT=https://collector.example.com:4318Replace the endpoint with your receiver. The generic HTTP endpoint appends /v1/traces. See the OpenTelemetry environment reference for signal-specific URLs, gRPC, authentication, sampling, and export limits. Remote log export has separate controls.
Repeat the request with a connected backend. Search for 11111111111111111111111111111111 and verify the GET /api/v1/hello server span and its hello.greet child. External export works independently of Lighthouse. Invalid exporter configuration fails app construction. An unreachable receiver loses telemetry without preventing request handling. App shutdown flushes the SDK within the shutdown budget.
Projects rendered with Grafana, Observability, and Docker offer a local backend. With APP_ENV=local, activate the grafana Compose profile and remove the explicit OTEL_TRACES_EXPORTER=none override. Start the stack with docker-compose up -d --build, then run forj dev. The app exports through Alloy to Tempo. Open http://localhost:13001/d/goforj-traces-overview. See internal/observability/README.md for the rendered stack.
Preserve Context and Limit Capture
For outbound HTTP, inject *inspects.Manager and use manager.HTTPClient(http.DefaultClient) with http.NewRequestWithContext(ctx, ...). The client adds spans and propagates W3C context. Generated queue and event managers also propagate context across supported transports. The app's internal/inspects/README.md documents instrumentation and transport boundaries.
Framework instrumentation keeps raw SQL, cache keys, object paths, request bodies, and job payloads in Lighthouse enrichment rather than automatically exporting them as span attributes. Application-created attributes, events, and errors follow your capture policy. Avoid credentials and personal data. Automatic redaction does not make arbitrary application text safe.
OTEL_TRACES_EXPORTER=none disables external traces while allowing local capture. OTEL_SDK_DISABLED=true disables span recording for both outputs. OTel sampling controls recording. LIGHTHOUSE_INSPECT_SAMPLE_RATE further reduces local detail capture. Finished Inspects can be dropped when their buffer is full or Lighthouse is unavailable. Use the Inspect retention controls to size that recent debugging window.