all: add ability to set arbitrary tags on resource struct

This commit is contained in:
jonny rhea 2026-02-04 15:33:21 -06:00
parent a51692908b
commit ac01565566
4 changed files with 24 additions and 3 deletions

View file

@ -201,6 +201,7 @@ var (
utils.RPCTelemetryUserFlag,
utils.RPCTelemetryPasswordFlag,
utils.RPCTelemetryInstanceIDFlag,
utils.RPCTelemetryTagsFlag,
utils.RPCTelemetrySampleRatioFlag,
}

View file

@ -1074,6 +1074,12 @@ Please note that --` + MetricsHTTPFlag.Name + ` must be set to start the server.
Category: flags.APICategory,
}
RPCTelemetryTagsFlag = &cli.StringFlag{
Name: "rpc.telemetry.tags",
Usage: "Comma-separated tags (key/values) added as attributes to the OpenTelemetry resource struct",
Category: flags.APICategory,
}
RPCTelemetrySampleRatioFlag = &cli.Float64Flag{
Name: "rpc.telemetry.sample-ratio",
Usage: "Defines the sampling ratio for RPC telemetry (0.0 to 1.0)",
@ -1543,7 +1549,6 @@ func setOpenTelemetry(ctx *cli.Context, cfg *node.Config) {
if ctx.IsSet(RPCTelemetryFlag.Name) {
tcfg.Enabled = ctx.Bool(RPCTelemetryFlag.Name)
}
if ctx.IsSet(RPCTelemetryEndpointFlag.Name) {
tcfg.Endpoint = ctx.String(RPCTelemetryEndpointFlag.Name)
}
@ -1553,10 +1558,13 @@ func setOpenTelemetry(ctx *cli.Context, cfg *node.Config) {
if ctx.IsSet(RPCTelemetryPasswordFlag.Name) {
tcfg.AuthPassword = ctx.String(RPCTelemetryPasswordFlag.Name)
}
tcfg.SampleRatio = ctx.Float64(RPCTelemetrySampleRatioFlag.Name)
if ctx.IsSet(RPCTelemetryInstanceIDFlag.Name) {
tcfg.InstanceID = ctx.String(RPCTelemetryInstanceIDFlag.Name)
}
if ctx.IsSet(RPCTelemetryTagsFlag.Name) {
tcfg.Tags = ctx.String(RPCTelemetryTagsFlag.Name)
}
tcfg.SampleRatio = ctx.Float64(RPCTelemetrySampleRatioFlag.Name)
if tcfg.Endpoint != "" && !tcfg.Enabled {
log.Warn(fmt.Sprintf("OpenTelemetry endpoint configured but telemetry is not enabled, use --%s to enable.", RPCTelemetryFlag.Name))

View file

@ -21,6 +21,7 @@ import (
"encoding/base64"
"fmt"
"net/url"
"strings"
"time"
"github.com/ethereum/go-ethereum/internal/version"
@ -121,14 +122,24 @@ func SetupTelemetry(cfg node.OpenTelemetryConfig, stack *node.Node) error {
sdktrace.WithBatchTimeout(time.Duration(sdktrace.DefaultScheduleDelay) * time.Millisecond),
}
// Define resource with service and client information
// Define resource attributes
var attr = []attribute.KeyValue{
semconv.ServiceName("geth"),
attribute.String("client.name", version.ClientName("geth")),
}
// Add instance ID if provided
if cfg.InstanceID != "" {
attr = append(attr, semconv.ServiceInstanceID(cfg.InstanceID))
}
// Add custom tags if provided
if cfg.Tags != "" {
for tag := range strings.SplitSeq(cfg.Tags, ",") {
key, value, ok := strings.Cut(tag, "=")
if ok {
attr = append(attr, attribute.String(key, value))
}
}
}
res := resource.NewWithAttributes(semconv.SchemaURL, attr...)
// Configure TracerProvider and set it as the global tracer provider

View file

@ -221,6 +221,7 @@ type Config struct {
type OpenTelemetryConfig struct {
Enabled bool `toml:",omitempty"`
Tags string `toml:",omitempty"`
InstanceID string `toml:",omitempty"`
// Exporter endpoint.