telemetry

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package telemetry records trace spans. The runner exports them to the company's tools; in aicoded dev they show on the dev UI's Traces page.

The app already gets a span for every request it serves and every call it makes to another app, and for every query, file and mail operation of the building blocks. Add a span with Start around a step of the app's own that takes time:

ctx, span := telemetry.Start(ctx, "import rows")
defer span.End()

Span.SetAttr records an attribute, and Span.RecordError marks the span as failed. People who must not see the app's data read spans: record ids, counts and codes, never a form value, a name, an email address or any other personal data.

Read more in the guide docs/guides/telemetry.md, which aicoded explain and the MCP tool howto print as guides/telemetry.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Span

type Span struct {
	// contains filtered or unexported fields
}

Span is one timed operation. End it once; SetAttr and RecordError after End do nothing, and a second End does nothing.

func Start

func Start(ctx context.Context, name string) (context.Context, *Span)

Start begins a span named name as a child of the current span of ctx, or as a new trace. The returned context carries the new span.

Example

Add a span around a step of the app's own that takes time. Its attributes hold ids, counts and codes, never personal data.

package main

import (
	"context"
	"database/sql"
	"strconv"
	"time"

	"aicoded.dev/framework/telemetry"
)

var (
	ctx context.Context // the context of a request: it carries the request's span
	db  *sql.DB         // the app's database, from sqldb.Open
)

// Add a span around a step of the app's own that takes time. Its attributes hold ids, counts and
// codes, never personal data.
func main() {
	ctx, span := telemetry.Start(ctx, "archive old notes")
	defer span.End()
	res, err := db.ExecContext(ctx, "UPDATE notes SET archived = TRUE WHERE created < ?", time.Now().AddDate(-1, 0, 0))
	if err != nil {
		span.RecordError(err)
		return
	}
	n, err := res.RowsAffected()
	if err != nil {
		span.RecordError(err)
		return
	}
	span.SetAttr("notes", strconv.FormatInt(n, 10))
}

func (*Span) End

func (s *Span) End()

End finishes the span and hands it to the runner.

func (*Span) RecordError

func (s *Span) RecordError(err error)

RecordError marks the span as failed; a nil err is ignored. It does nothing after End. The span keeps the error's text under aicoded dev in environment `dev`, and anywhere else only its kinds and codes, such as E-codes, MySQL error numbers and status codes, since the text can quote personal data.

func (*Span) SetAttr

func (s *Span) SetAttr(key, value string)

SetAttr records an attribute. It does nothing after End.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL