telemetry

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 25 Imported by: 3

README

/pkg/telemetry

cd /

[!NOTE] asyncmachine-go is a batteries-included graph control flow library (AOP, actor model, state-machine).

/pkg/telemetry provides several telemetry exporters and is accompanied by generated Grafana dashboards:

dbg

dbg is simple telemetry used by am-dbg TUI Debugger. It delivers DbgMsg and DbgMsgStruct via standard net/rpc. It can also be consumed by a custom client.

OpenTelemetry Traces

Open Telemetry traces integration exposes machine's states and transitions as Otel traces, compatible with Jaeger. Tracers are inherited from parent machines and form a tree, with each machine getting a position index as a prefix, eg 0:mach:MyMachId. The transitions are linked to the states with logged arguments are added as trace's tags. Machine tags are added as well.

Tree Structure
- mach:ID
  - states
    - Foo
      - Foo (trace)
      - Foo (trace)
      - ...
    - ...
  - transitions
    - [add] Foo (trace)
    - ...
  - submachines
    - mach:ID2
      - ...
    - ...
Otel Tracing Setup
Automatic Otel Tracing

See /docs/env-configs.md for the required environment variables.

import amtele "github.com/pancsta/asyncmachine-go/pkg/telemetry"

// ...

var mach *am.Machine

// open telemetry traces
err = amtele.MachBindOtelEnv(mach)
if err != nil {
    mach.AddErr(err, nil)
}
Manual Otel Tracing
import (
    am "github.com/pancsta/asyncmachine-go/pkg/machine"
    amtele "github.com/pancsta/asyncmachine-go/pkg/telemetry"
    "go.opentelemetry.io/otel/trace"
)

// ...

var mach *am.Machine
var tracer trace.Tracer

machTracer := amtele.NewOtelMachTracer(tracer, &amtele.OtelMachTracerOpts{
    SkipTransitions: false,
})
mach.BindTracer(machTracer)

OpenTelemetry Logger

Open Telemetry logger integration exposes machine's logs (any level) as structured Otlp format. It can be very handy for stdout logging.

import (
    "go.opentelemetry.io/otel/sdk/log"
    am "github.com/pancsta/asyncmachine-go/pkg/machine"
    amtele "github.com/pancsta/asyncmachine-go/pkg/telemetry"
)

// ...

var mach *am.Machine
var logExporter log.Exporter

// activate logs
mach.SetLogLevel(am.LogLevelOps)

// create a log provider
logProvider := amtele.NewOtelLoggerProvider(logExporter)
// bind provider to a machine
amtele.BindOtelLogger(mach, logProvider, "myserviceid")

Prometheus Metrics

pkg/telemetry/prometheus binds to machine's transactions, collects values within a defined interval, and exposes averaged metrics. Use it with the provided Grafana dashboard. Tracers are inherited from parent machines.

Automatic Prometheus Setup

See /docs/env-configs.md for the required environment variables.

import amtele "github.com/pancsta/asyncmachine-go/pkg/telemetry"

// ...

var mach *am.Machine

// export metrics to prometheus
metrics := amprom.MachMetricsEnv(mach)
Manual Prometheus Setup
import (
    "time"
    am "github.com/pancsta/asyncmachine-go/pkg/machine"
    amprom "github.com/pancsta/asyncmachine-go/pkg/telemetry/prometheus"
    "github.com/prometheus/client_golang/prometheus/push"
)

// ...

var mach *am.Machine
var promRegistry *prometheus.Registry
var promPusher *push.Pusher

// bind transition to metrics
metrics := amprom.BindMach(mach)

// bind metrics either a registry or a pusher
amprom.BindToRegistry(promRegistry)
amprom.BindToPusher(promPusher)

Loki Logger

Loki is the easiest way to persist distributed logs from asyncmachine. You'll need a promtail client.

Automatic Loki Setup

See /docs/env-configs.md for the required environment variables.

import amtele "github.com/pancsta/asyncmachine-go/pkg/telemetry"

// ...

var mach *am.Machine

// export logs to Loki logger
amtele.BindLokiEnv(mach)
Manual Loki Setup
import (
    "github.com/ic2hrmk/promtail"
    am "github.com/pancsta/asyncmachine-go/pkg/machine"
    amtele "github.com/pancsta/asyncmachine-go/pkg/telemetry"
)

// ...

var mach *am.Machine
var service string

// init promtail and bind AM logger
identifiers := map[string]string{
    "service_name": service,
}
promtailClient, err := promtail.NewJSONv1Client("localhost:3100", identifiers)
if err != nil {
    panic(err)
}
defer promtailClient.Close()
amtele.BindLokiLogger(mach, promtailClient)

Grafana Dashboard

More info about Grafana dashboards can be found in /tools/cmd/am-gen.

Automatic Grafana Setup

See /docs/env-configs.md for the required environment variables.

import amgen "github.com/pancsta/asyncmachine-go/tools/generator"

// ...

var mach *am.Machine

// create a dedicated dashboard for [mach] and submachines
amgen.MachDashboardEnv(mach)
Manual Grafana Setup
import (
    amgen "github.com/pancsta/asyncmachine-go/tools/generator"
    amgencli "github.com/pancsta/asyncmachine-go/tools/generator/cli"
)

// ...

var mach *am.Machine
var service string
var url string
var token string

p := amgencli.GrafanaParams{
    Ids:        mach.Id(),
    Name:       mach.Id(),
    Folder:     "asyncmachine",
    GrafanaUrl: url,
    Token:      token,
    Source:     service,
}
t := &amgen.SyncTracer{p: p}

mach.BindTracer(t)

Inheritance

Most of the telemetry exporters are automatically inherited from parent machines, so the results come in automatically. To define a submachine-parent relationship, use am.Opts.Parent while initializing a machine. Alternatively, tracers can be copied using OptsWithParentTracers, or manually via Machine.Tracers.

Documentation

Status

Testing, not semantically versioned.

monorepo

Go back to the monorepo root to continue reading.

Documentation

Overview

Package telemetry provides telemetry exporters: am-dbg, Prometheus, OpenTelemetry.

Index

Constants

View Source
const (
	EnvService         = "AM_SERVICE"
	EnvLokiAddr        = "AM_LOKI_ADDR"
	EnvOtelTrace       = "AM_OTEL_TRACE"
	EnvOtelTraceTxs    = "AM_OTEL_TRACE_TXS"
	EnvOtelTraceArgs   = "AM_OTEL_TRACE_ARGS"
	EnvOtelTraceNoauto = "AM_OTEL_TRACE_NOAUTO"
)

Variables

This section is empty.

Functions

func BindLokiEnv added in v0.11.0

func BindLokiEnv(mach am.Api) error

BindLokiEnv bind Loki logger to [mach], based on environment vars: - AM_SERVICE (required) - AM_LOKI_ADDR (required) This tracer is NOT inherited by submachines.

func BindLokiLogger added in v0.8.0

func BindLokiLogger(mach am.Api, client promtail.Client)

func BindOtelLogger added in v0.8.0

func BindOtelLogger(
	mach am.Api, provider *ologsdk.LoggerProvider, service string,
)

BindOtelLogger binds an OpenTelemetry logger to a machine.

func MachBindOtelEnv added in v0.10.3

func MachBindOtelEnv(mach am.Api) error

MachBindOtelEnv bind an OpenTelemetry tracer to [mach], based on environment variables: - AM_SERVICE (required) - AM_OTEL_TRACE (required) - AM_OTEL_TRACE_TXS - OTEL_EXPORTER_OTLP_ENDPOINT - OTEL_EXPORTER_OTLP_TRACES_ENDPOINT

This tracer is inherited by submachines, and this function applies only to top-level machines.

func NewOtelLoggerProvider added in v0.8.0

func NewOtelLoggerProvider(exporter ologsdk.Exporter) *ologsdk.LoggerProvider

NewOtelLoggerProvider creates a new OpenTelemetry logger provider bound to the given exporter.

func NewOtelProvider added in v0.10.3

func NewOtelProvider(
	source string, ctx context.Context,
) (trace.Tracer, *sdktrace.TracerProvider, error)

func NormalizeId added in v0.8.0

func NormalizeId(id string) string

Types

type OtelMachTracer added in v0.5.0

type OtelMachTracer struct {
	*am.TracerNoOp

	Tracer        trace.Tracer
	Machines      map[string]*OtelMachineData
	MachinesMx    sync.Mutex
	MachinesOrder []string
	RootSpan      trace.Span

	// TODO bind to env var
	Logf func(format string, args ...any)

	NextIndex int
	// contains filtered or unexported fields
}

OtelMachTracer implements machine.Tracer for OpenTelemetry. Supports tracing of multiple state machines, resulting in a single trace. This tracer is automatically bound to new sub-machines.

func NewOtelMachTracer added in v0.5.0

func NewOtelMachTracer(
	rootMach am.Api, rootSpan trace.Span, otelTracer trace.Tracer,
	opts *OtelMachTracerOpts,
) *OtelMachTracer

NewOtelMachTracer creates a new machine tracer from an OpenTelemetry tracer. Requires OtelMachTracer.Dispose to be called at the end.

func (*OtelMachTracer) End added in v0.5.0

func (mt *OtelMachTracer) End()

func (*OtelMachTracer) HandlerEnd added in v0.5.0

func (mt *OtelMachTracer) HandlerEnd(
	tx *am.Transition, emitter string, handler string,
)

func (*OtelMachTracer) MachineDispose added in v0.5.0

func (mt *OtelMachTracer) MachineDispose(id string)

func (*OtelMachTracer) MachineInit added in v0.5.0

func (mt *OtelMachTracer) MachineInit(mach am.Api) context.Context

func (*OtelMachTracer) NewSubmachine added in v0.5.0

func (mt *OtelMachTracer) NewSubmachine(parent, mach am.Api)

NewSubmachine links 2 machines with a parent-child relationship.

func (*OtelMachTracer) QueueEnd added in v0.6.0

func (mt *OtelMachTracer) QueueEnd(mach am.Api)

func (*OtelMachTracer) TransitionEnd added in v0.5.0

func (mt *OtelMachTracer) TransitionEnd(tx *am.Transition)

func (*OtelMachTracer) TransitionInit added in v0.5.0

func (mt *OtelMachTracer) TransitionInit(tx *am.Transition)

type OtelMachTracerOpts added in v0.5.0

type OtelMachTracerOpts struct {
	// if true, only state changes will be traced
	SkipTransitions bool
	// if true, only Healthcheck and Heartbeat will be skipped
	IncludeHealth bool
	// if true, transition traces won't include [am.Machine.GetLogArgs]
	SkipLogArgs bool
	// if true, auto transitions won't be traced
	SkipAuto bool
	// TODO
	AllowStates am.S
	// TODO
	SkipStates am.S

	Logf func(format string, args ...any)
}

type OtelMachineData added in v0.5.0

type OtelMachineData struct {
	ID string
	// Index is a unique number for this machine withing the Otel tracer.
	Index int
	Ended bool
	// contains filtered or unexported fields
}

type OtelTxHist added in v0.11.0

type OtelTxHist struct {
	Id  string
	Ctx context.Context
}

Directories

Path Synopsis
Package prometheus provides Prometheus metrics for asyncmachine.
Package prometheus provides Prometheus metrics for asyncmachine.

Jump to

Keyboard shortcuts

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