Files
zeekay a6c7c4ec94 feat(metrics): the retired module's door and store land here, under /v1/metrics
github.com/hanzoai/metrics carries a NOTICE naming this module its successor,
and it is archived and read-only. What the NOTICE did not say is that the code
was still running: hanzoai/cloud went on importing the archive and mounting it
for /v1/metrics, /v1/logs and /v1/traces, so a repository nobody can push to
held eleven live routes and the store behind them.

A succession is only finished when the successor has the code. It arrives whole
— the per-tenant WAL store, the ZAP MetricBatch receiver, the routes and their
tests — and the tests pass here unchanged but for the addresses.

The addresses fold per HIP-1241: every route is under /v1/metrics, so the two
foreign roots the archive answered on become /v1/metrics/logs/* and
/v1/metrics/traces/*. Forward only; nothing answers at the old spelling.

This store is its own. The event.* columnar plane the rest of this module reads
is a different store of a different shape, and the succession note's claim that
o11y already held this signal store was true only of that plane — these eleven
routes had no home here until now. They share the module and nothing else.

The tenant decision still comes from the host (Deps.Org), so the boundary that
authenticates keeps naming the org and this package never reads a header.
2026-08-20 20:32:19 -07:00

51 lines
1.8 KiB
Go

package metrics
import (
"context"
"encoding/json"
luxlog "github.com/luxfi/log"
metric "github.com/luxfi/metric"
zap "github.com/luxfi/zap"
)
// MsgMetricBatch is the canonical ZAP MsgType for metric batches — it matches
// luxfi/metric.MsgMetricBatch and o11y/pkg/zapmetricreceiver so any luxfi/metric
// ZAP exporter ingests here unchanged.
const MsgMetricBatch uint16 = 2
// zapReceiver hosts a luxfi/zap node that accepts MsgMetricBatch messages and
// ingests them into the addressed tenant's metric store.
type zapReceiver struct{ node *zap.Node }
// startZAPReceiver binds a ZAP node on port that decodes a luxfi/metric.MetricBatch
// (JSON at message field 0, the o11y wire convention) from each MsgMetricBatch
// message and writes it into reg.For(org).Metrics — org is the batch's
// Resource["org"], else the deployment brand. port<=0 disables it (HTTP
// /v1/metrics/batch stays the ingest). The ZAP transport avoids the HTTP hop and
// is the push path for in-cluster luxfi/metric exporters.
func startZAPReceiver(port int, nodeID string, log luxlog.Logger) (*zapReceiver, error) {
if port <= 0 {
return nil, nil
}
node := zap.NewNode(zap.NodeConfig{NodeID: nodeID, Port: port, NoDiscovery: true})
node.Handle(MsgMetricBatch, func(ctx context.Context, peerID string, msg *zap.Message) (*zap.Message, error) {
var b metric.MetricBatch
if err := json.Unmarshal(msg.Root().Bytes(0), &b); err != nil {
log.Warn("zap metric batch decode", "peer", peerID, "err", err)
return nil, nil
}
org := b.Resource["org"]
if org == "" {
org = brand
}
reg.For(org).Metrics.IngestBatch(&b)
return nil, nil
})
if err := node.Start(); err != nil {
return nil, err
}
log.Info("zap metric receiver listening", "port", port, "nodeID", nodeID, "msgType", MsgMetricBatch)
return &zapReceiver{node: node}, nil
}