internal/telemetry: some styling changes

This commit is contained in:
lightclient 2026-01-08 08:29:45 -07:00
parent 0897f08389
commit 6e6bd76fe3
No known key found for this signature in database
GPG key ID: 657913021EF45A6A

View file

@ -27,14 +27,6 @@ import (
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
) )
// RPCInfo contains information about the RPC request.
type RPCInfo struct {
System string
Service string
Method string
RequestID string
}
// Attribute is an alias for attribute.KeyValue. // Attribute is an alias for attribute.KeyValue.
type Attribute = attribute.KeyValue type Attribute = attribute.KeyValue
@ -48,66 +40,46 @@ func Int64Attribute(key string, val int64) Attribute {
return attribute.Int64(key, val) return attribute.Int64(key, val)
} }
// StartSpan creates an internal span.
func StartSpan(ctx context.Context, spanName string, attributes ...Attribute) (context.Context, trace.Span, func(error)) {
return StartSpanWithTracer(ctx, otel.Tracer(""), spanName, attributes...)
}
// StartSpanWithTracer requires a tracer to be passed in and creates a SpanKind=INTERNAL span.
func StartSpanWithTracer(ctx context.Context, tracer trace.Tracer, name string, attributes ...Attribute) (context.Context, trace.Span, func(error)) {
return startSpan(ctx, tracer, name, attributes...)
}
// RPCInfo contains information about the RPC request.
type RPCInfo struct {
System string
Service string
Method string
RequestID string
}
// StartServerSpan creates a SpanKind=SERVER span at the JSON-RPC boundary. // StartServerSpan creates a SpanKind=SERVER span at the JSON-RPC boundary.
// The span name is formatted as $rpcSystem.$rpcService/$rpcMethod // The span name is formatted as $rpcSystem.$rpcService/$rpcMethod
// (e.g. "jsonrpc.engine/newPayloadV4"). // (e.g. "jsonrpc.engine/newPayloadV4") which follows the Open Telemetry
// semantic convensions: https://opentelemetry.io/docs/specs/semconv/rpc/rpc-spans/#span-name.
func StartServerSpan(ctx context.Context, tracer trace.Tracer, rpc RPCInfo, others ...Attribute) (context.Context, func(error)) { func StartServerSpan(ctx context.Context, tracer trace.Tracer, rpc RPCInfo, others ...Attribute) (context.Context, func(error)) {
spanName := fmt.Sprintf("%s.%s/%s", rpc.System, rpc.Service, rpc.Method) var (
ctx, span := tracer.Start( name = fmt.Sprintf("%s.%s/%s", rpc.System, rpc.Service, rpc.Method)
ctx, attrs = []Attribute{
spanName,
trace.WithSpanKind(trace.SpanKindServer),
)
// Define required attributes
attrs := []Attribute{
semconv.RPCSystemKey.String(rpc.System), semconv.RPCSystemKey.String(rpc.System),
semconv.RPCServiceKey.String(rpc.Service), semconv.RPCServiceKey.String(rpc.Service),
semconv.RPCMethodKey.String(rpc.Method), semconv.RPCMethodKey.String(rpc.Method),
semconv.RPCJSONRPCRequestID(rpc.RequestID), semconv.RPCJSONRPCRequestID(rpc.RequestID),
} }
// Add any additional attributes provided
if len(others) > 0 {
attrs = append(attrs, others...)
}
span.SetAttributes(attrs...)
return ctx, endSpan(span)
}
// StartSpan creates a SpanKind=INTERNAL span.
func StartSpan(
ctx context.Context,
spanName string,
attributes ...Attribute,
) (context.Context, trace.Span, func(error)) {
return StartSpanWithTracer(ctx, otel.Tracer(""), spanName, attributes...)
}
// StartSpanWithTracer requires a tracer to be passed in and creates a SpanKind=INTERNAL span.
func StartSpanWithTracer(
ctx context.Context,
tracer trace.Tracer,
spanName string,
attributes ...Attribute,
) (context.Context, trace.Span, func(error)) {
return startSpan(ctx, tracer, spanName, attributes...)
}
// startSpan creates a SpanKind=INTERNAL span.
func startSpan(
ctx context.Context,
tracer trace.Tracer,
spanName string,
attributes ...Attribute,
) (context.Context, trace.Span, func(error)) {
ctx, span := tracer.Start(
ctx,
spanName,
trace.WithSpanKind(trace.SpanKindInternal),
) )
ctx, _, end := startSpan(ctx, tracer, name, append(attrs, others...)...)
return ctx, end
}
// startSpan creates an internal span.
func startSpan(ctx context.Context, tracer trace.Tracer, spanName string, attributes ...Attribute) (context.Context, trace.Span, func(error)) {
ctx, span := tracer.Start(ctx, spanName, trace.WithSpanKind(trace.SpanKindInternal))
// Set attributes if any are provided
if len(attributes) > 0 { if len(attributes) > 0 {
span.SetAttributes(attributes...) span.SetAttributes(attributes...)
} }