Documentation
¶
Overview ¶
Package arcotel is a small OpenTelemetry setup for Go apps on Arco Cloud.
It is plain OpenTelemetry with a few defaults: it exports OTLP/HTTP to OTEL_EXPORTER_OTLP_ENDPOINT and does nothing at all when that variable is unset (your laptop, Docker, an environment without tracing).
shutdown := arcotel.Setup(ctx) defer shutdown(context.Background()) http.ListenAndServe(addr, arcotel.Handler(mux))
Index ¶
- Constants
- func AWS(cfg *aws.Config)
- func Handler(h http.Handler) http.Handler
- func OpenDB(driverName, dsn string, opts ...DBOption) (*sql.DB, error)
- func Propagator() propagation.TextMapPropagator
- func Route(r *http.Request, pattern string)
- func Sampler() sdktrace.Sampler
- func Setup(ctx context.Context) func(context.Context) error
- func Transport(base http.RoundTripper) http.RoundTripper
- func WrapDB(c driver.Connector, opts ...DBOption) *sql.DB
- type DBOption
Constants ¶
const FlushPath = "/_arco/otel/flush"
FlushPath is the endpoint Handler answers itself: a POST there flushes the tracer provider and returns 204 once every span ended before it has been exported. The platform calls it after each response; your handler never sees this path.
Variables ¶
This section is empty.
Functions ¶
func AWS ¶
AWS instruments an AWS SDK for Go v2 config: every call made by clients built from cfg afterwards is a span, and trace context is injected with the global propagator (Propagator() once Setup has run). Call it once, before creating clients.
cfg, _ := config.LoadDefaultConfig(ctx) arcotel.AWS(&cfg) s3Client := s3.NewFromConfig(cfg)
func Handler ¶
Handler wraps h with OpenTelemetry server instrumentation (otelhttp) and the flush endpoint (FlushPath). With Go 1.22+ http.ServeMux patterns the route is recorded automatically; with other routers call Route.
func OpenDB ¶
OpenDB opens a *sql.DB whose queries become spans, like sql.Open(driverName, dsn). The SQL text is recorded (db.statement / db.query.text); parameter values never are. Import the driver as usual, for example _ "github.com/jackc/pgx/v5/stdlib" for driverName "pgx". db.system comes from the driver name, or from WithDBSystem.
func Propagator ¶
func Propagator() propagation.TextMapPropagator
Propagator returns the W3C trace-context propagator Arco Cloud expects. It behaves like propagation.TraceContext, except that a span whose tracestate carries arco=d (keep-or-drop decision deferred) is injected with the sampled flag cleared: traceparent ends in -00 and tracestate keeps arco=d, so the services the app calls see the trace as still deferred. Extract is unchanged.
Setup installs it (with the baggage propagator). Apps that configure OpenTelemetry themselves should install it next to Sampler():
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
arcotel.Propagator(), propagation.Baggage{}))
func Route ¶
Route sets http.route (and the span name "METHOD route") on the server span of r. Use it with routers other than http.ServeMux, for example with chi's matched route pattern. A ServeMux-style pattern such as "GET /orders/{id}" is accepted; its method and host are stripped. It does nothing outside a traced request.
func Sampler ¶
Sampler returns the sampler Arco Cloud expects. It is parent-based:
- a parent with the sampled flag (-01) is recorded and sampled;
- an unsampled parent (-00) whose tracestate carries arco=d (the decision is deferred until the request ends) is recorded and sampled (so it can be exported), and the tracestate is kept; Propagator() then sends -00 with arco=d on outgoing calls, so the decision stays deferred downstream;
- any other unsampled parent (a bare -00) is dropped;
- a span without a parent is sampled.
Apps that configure OpenTelemetry themselves can use it on its own:
sdktrace.NewTracerProvider(sdktrace.WithSampler(arcotel.Sampler()), ...)
A stock ParentBased sampler records nothing under a -00 parent, so without this sampler such an app only gets the spans created outside the request.
func Setup ¶
Setup installs a global tracer provider that exports OTLP/HTTP to OTEL_EXPORTER_OTLP_ENDPOINT through a batch processor, using Sampler(), and installs Propagator() (W3C trace context) with the baggage propagator. The service name comes from OTEL_SERVICE_NAME; the other standard OTEL_* variables are honoured.
Without OTEL_EXPORTER_OTLP_ENDPOINT it does nothing: the global provider is left alone and the returned shutdown is a no-op. Setup never fails the app: if the exporter cannot be built, the error goes to the OpenTelemetry error handler and Setup behaves as a no-op.
Call the returned function before the process exits to flush pending spans.
func Transport ¶
func Transport(base http.RoundTripper) http.RoundTripper
Transport wraps base (http.DefaultTransport when nil) so every outgoing request is a client span and carries the trace context to the next service, using the global propagator (Propagator() once Setup has run).
client := &http.Client{Transport: arcotel.Transport(nil)}
Types ¶
type DBOption ¶
type DBOption func(*dbConfig)
DBOption configures OpenDB and WrapDB.
func WithDBSystem ¶
WithDBSystem sets the db.system attribute of the query spans (for example "postgresql", "mysql" or "sqlite"), for a driver that OpenDB or WrapDB does not recognise. The service map groups queries by it.