grpcprocotel

package module
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 18 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

grpcproc/otel

OpenTelemetry for grpcproc: a span for every send, call and handled message, chained across processes and nodes, and metrics keyed by process label. A separate module, so grpcproc itself does not depend on OpenTelemetry.

go get github.com/floatdrop/grpcproc/otel
h, err := grpcprocotel.New() // global meter/tracer providers and propagator; see Options
node, err := grpcproc.NewNode(grpcproc.Config{…, Hooks: grpcproc.JoinHooks(h, myHooks)})
reg, err := h.Observe(node) // gauges read from node snapshots at each collection
defer reg.Unregister()
lreg, err := h.ObserveLeader(node) // gauges of the node's grpcproc/leader elections
defer lreg.Unregister()

Traces

Where Span Kind Parent
Send send <message type> producer the context the sender carries
Call call <message type> client the context the sender carries
a process takes a message process <message type> consumer (server for a call) the sender's span
a process takes a Down process grpcproc.Down consumer —
a process that traps exits takes an Exited process grpcproc.Exited consumer —

A handling span lasts until the process calls Receive again, or exits (with an error status if abnormally). Everything the process sends meanwhile is its child, because grpcproc hands a process's sends the metadata of the message it is handling, and these hooks put the span there. No context has to be threaded through handlers for the chain to form.

For a handler's own spans (a database call), take the handling span from the message, in the message's context, which ends when its caller stops waiting:

ctx, cancel := m.Context(p.Context())
defer cancel()
ctx, span := tracer.Start(h.Extract(ctx, m.Metadata), "load order")

Spans carry messaging.system=grpcproc, messaging.operation.type, messaging.destination.name, grpcproc.message.type, grpcproc.label, grpcproc.pid; failed sends and calls carry error.type.

Metrics

No metric carries a PID: the process label (grpcproc.WithLabel, default the message type) is the unit, and every other attribute is bounded.

Name Type Attributes
grpcproc.messages.sent counter grpcproc.label (sender), grpcproc.call, grpcproc.remote
grpcproc.messages.received counter grpcproc.label
grpcproc.mailbox.wait histogram, s grpcproc.label
grpcproc.process.duration histogram, s grpcproc.label — time from taking a message to the next Receive
grpcproc.call.duration histogram, s grpcproc.label, grpcproc.remote, error.type on failure
grpcproc.processes.spawned counter grpcproc.label
grpcproc.processes.exited counter grpcproc.label, grpcproc.reason (normal, shutdown, killed, noproc, noconnection, type, name lost, name conflict; max restarts from actor, timeout and replaced from cron, demoted from leader; panic, error)
grpcproc.dead_letters counter grpcproc.reason, grpcproc.message.type
grpcproc.links.up, grpcproc.links.down counter grpcproc.peer — sessions with the peer begun and ended (OnLinkUp, OnLinkDown)
grpcproc.members.up, grpcproc.members.down counter grpcproc.peer — the member Members lists anew or changed, or lists no more (OnMemberUp, OnMemberDown)
grpcproc.processes gauge (Observe) grpcproc.label
grpcproc.mailbox.depth gauge (Observe) grpcproc.label, summed
grpcproc.mailbox.oldest gauge (Observe), s grpcproc.label, maximum
grpcproc.link.messages, grpcproc.link.bytes counter (Observe) grpcproc.peer, grpcproc.direction — what each link had carried at the collections that saw it, added up across links, so a broken link does not take it back

error.type is one of noproc, type, mailbox_full (the callee's mailbox was full: WithMailboxLimit), too_large (the message, or the reply, was over Config.MaxMessageSize), busy (the link to the peer was full: Config.MaxQueued), noconnection, timeout, canceled, remote (the handler returned an error), other.

Leader elections

ObserveLeader reads each grpcproc/leader elector on the node, the processes named leader.ElectorName(cluster) with its label, at each collection, from its inspect map, and reports one set of gauges per election, by grpcproc.leader.cluster. The electors are asked at once. One that the node's snapshot shows on one message for a second (busy in a slow Store, say), or that does not answer within a second of a collection that lasts that long, is reported as stalled, with no other gauge.

Name What it says
grpcproc.leader.electors 1, with grpcproc.leader.role: leader, candidate, follower or unclustered
grpcproc.leader.stalled 1 while the elector is on one message for a second or more, or does not answer within one; 0 while it answers
grpcproc.leader.term the elector's term
grpcproc.leader.view, grpcproc.leader.quorum the nodes whose majority elects, and how many that majority is
grpcproc.leader.reachable the nodes of the view it hears from, itself included
grpcproc.leader.singleton 1 while the singleton runs on this node
grpcproc.leader.checkpoints.waiting Checkpoint calls waiting for a majority to hold their state

Alerts per cluster, in PromQL, with the names as the Prometheus exporter writes them: dots turned to underscores, and a unit suffix where there is one:

# No leader: the leaders, with a 0 for every elector, so that none adds up to 0.
# A cluster below MinClusterSize has none by design: its electors are unclustered.
sum by (grpcproc_leader_cluster) (grpcproc_leader_electors{grpcproc_leader_role="leader"} or grpcproc_leader_electors * 0) < 1
# No node of a cluster reporting at all: one rule per cluster.
absent(grpcproc_leader_electors{grpcproc_leader_cluster="scheduler"})
# Two leaders: a leader cut off from the others has not noticed.
sum by (grpcproc_leader_cluster) (grpcproc_leader_electors{grpcproc_leader_role="leader"}) > 1
# The singleton runs nowhere, or twice.
sum by (grpcproc_leader_cluster) (grpcproc_leader_singleton) != 1
# One more node lost and this side has no majority; below it, none can be elected.
# A view of one or two never has a node to spare.
grpcproc_leader_reachable <= grpcproc_leader_quorum and grpcproc_leader_view >= 3
# Elections keep happening.
changes(max by (grpcproc_leader_cluster) (grpcproc_leader_term)[15m:]) > 3
# A majority is not taking the leader's state.
grpcproc_leader_checkpoints_waiting > 0
# An elector held up for a second or more, which sends no heartbeat or vote meanwhile.
grpcproc_leader_stalled > 0

Give each a for: of a few scrape intervals, a minute say: an election, and a hand-over (Resign stops the old singleton before the next leader starts its own), leave no leader or no singleton for a moment, and a leader cut off from the others leads for up to two election timeouts after they elect another. A stalled elector is in none of the sums: when the leader is the one held up, the other nodes' gauges show no leader, and with no other node reporting the sums are empty, which only "stalled" alerts on. A second is fixed: with an ElectionTimeout above it, a Store slow enough to be stalled can still be within it, so give "stalled" a for: longer than the timeout.

Documentation

Overview

Package grpcprocotel records grpcproc activity with OpenTelemetry: a span for every send, call and handled message, chained across processes and nodes through grpcproc.Metadata, and metrics keyed by process label.

h, err := grpcprocotel.New()                 // global providers and propagator
node, err := grpcproc.NewNode(grpcproc.Config{…, Hooks: h})
reg, err := h.Observe(node)                // gauges from snapshots
defer reg.Unregister()
lreg, err := h.ObserveLeader(node)         // grpcproc/leader elections, if any
defer lreg.Unregister()

Inside a handler, h.Extract(ctx, m.Metadata) is a context whose span is the one handling m, for the handler's own spans (a database call, an HTTP request).

Index

Constants

View Source
const (
	AttrLabel       = attribute.Key("grpcproc.label")
	AttrCall        = attribute.Key("grpcproc.call")
	AttrRemote      = attribute.Key("grpcproc.remote")
	AttrReason      = attribute.Key("grpcproc.reason")
	AttrMessageType = attribute.Key("grpcproc.message.type")
	AttrPeer        = attribute.Key("grpcproc.peer")
	AttrDirection   = attribute.Key("grpcproc.direction")
	AttrCluster     = attribute.Key("grpcproc.leader.cluster") // ObserveLeader
	AttrRole        = attribute.Key("grpcproc.leader.role")    // ObserveLeader's grpcproc.leader.electors
	AttrPID         = attribute.Key("grpcproc.pid")            // spans only
	AttrErrorType   = attribute.Key("error.type")
)

Attribute keys. Metrics never carry a PID: the label is the unit of aggregation, and everything else is bounded.

View Source
const ScopeName = "github.com/floatdrop/grpcproc/otel"

ScopeName is the instrumentation scope of the meter and tracer.

Variables

This section is empty.

Functions

func ErrorType

func ErrorType(err error) string

ErrorType maps a send or call error to a bounded set, for error.type.

func ReasonClass

func ReasonClass(reason string) string

ReasonClass maps an exit reason to a bounded set, for metrics: the well-known reasons, grpcproc's packages' own included, "panic", or "error" for anything else a process returned. A leader's singleton demoted, or a cron run replaced, is then no error on a dashboard.

Types

type Carrier

type Carrier grpcproc.Metadata

Carrier adapts grpcproc.Metadata to OpenTelemetry propagation, the way propagation.HeaderCarrier adapts HTTP headers.

func (Carrier) Get

func (c Carrier) Get(key string) string

func (Carrier) Keys

func (c Carrier) Keys() []string

func (Carrier) Set

func (c Carrier) Set(key, value string)

type Hooks

type Hooks struct {
	grpcproc.NopHooks
	// contains filtered or unexported fields
}

Hooks implements grpcproc.Hooks with OpenTelemetry. Combine it with other hooks through grpcproc.JoinHooks.

func New

func New(opts ...Option) (*Hooks, error)

New creates the instruments. With no options it uses the global meter and tracer providers and the global propagator, read once, now.

func (*Hooks) Extract

func (h *Hooks) Extract(ctx context.Context, md grpcproc.Metadata) context.Context

Extract returns ctx carrying the trace context in md. With a Msg's Metadata it is the span handling that message.

func (*Hooks) Observe

func (h *Hooks) Observe(node *grpcproc.Node) (metric.Registration, error)

Observe registers gauges read from node's snapshots at each collection: processes, mailbox depth and oldest message age per label, and counters of traffic per peer and direction. Those add up what each link to the peer had carried at the collections that saw it, so they never go back when a link breaks; what a link carries after the last collection before it breaks is not counted. A peer this node has neither a link to nor backs off from is forgotten, and counts from 0 if it comes back. Unregister the returned registration when the node stops.

func (*Hooks) ObserveLeader added in v0.14.0

func (h *Hooks) ObserveLeader(node *grpcproc.Node) (metric.Registration, error)

ObserveLeader registers gauges read at each collection from the electors of grpcproc/leader on node, by grpcproc.leader.cluster: one set for each election the node takes part in when the collection runs. They come from what each elector publishes through its inspect map: its role (1, with grpcproc.leader.role), its term, the nodes of its view, the quorum, how many of the view it hears from, itself included, whether the singleton runs on this node, and how many Checkpoint calls wait for a majority. The electors are asked at once. One that the node's snapshot shows on one message for a second, busy in a slow Store say, or that does not answer within a second of a collection that lasts that long, is reported as stalled, with no other gauge. Unregister the returned registration when the node stops.

func (*Hooks) OnDeadLetter

func (h *Hooks) OnDeadLetter(_, _ grpcproc.PID, body proto.Message, reason string)

func (*Hooks) OnExit

func (h *Hooks) OnExit(p grpcproc.ProcessInfo, reason string)

func (*Hooks) OnLinkDown

func (h *Hooks) OnLinkDown(peer grpcproc.NodeID, _ error)

func (*Hooks) OnLinkUp

func (h *Hooks) OnLinkUp(peer grpcproc.NodeID)

func (*Hooks) OnMemberDown added in v0.14.0

func (h *Hooks) OnMemberDown(m grpcproc.Member)

func (*Hooks) OnMemberUp added in v0.14.0

func (h *Hooks) OnMemberUp(m grpcproc.Member)

func (*Hooks) OnReceive

OnReceive starts the span that covers handling the message, a consumer span (a server span for a call) whose parent is the sender's span, and makes it what the process's own sends inherit.

func (*Hooks) OnSend

OnSend starts a producer span for a send, a client span for a call, and injects it into what leaves.

func (*Hooks) OnSpawn

func (h *Hooks) OnSpawn(p grpcproc.ProcessInfo)

type Option

type Option func(*config)

Option configures New.

func WithMeterProvider

func WithMeterProvider(mp metric.MeterProvider) Option

WithMeterProvider records metrics with mp instead of the global provider.

func WithPropagator

func WithPropagator(p propagation.TextMapPropagator) Option

WithPropagator carries trace context with p instead of the global propagator.

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider) Option

WithTracerProvider records spans with tp instead of the global provider.

Jump to

Keyboard shortcuts

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