mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 20:26:41 +00:00
internal/telemetry: some styling changes
This commit is contained in:
parent
0897f08389
commit
6e6bd76fe3
1 changed files with 33 additions and 61 deletions
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartServerSpan creates a SpanKind=SERVER span at the JSON-RPC boundary.
|
// StartSpan creates an internal span.
|
||||||
// The span name is formatted as $rpcSystem.$rpcService/$rpcMethod
|
func StartSpan(ctx context.Context, spanName string, attributes ...Attribute) (context.Context, trace.Span, func(error)) {
|
||||||
// (e.g. "jsonrpc.engine/newPayloadV4").
|
|
||||||
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)
|
|
||||||
ctx, span := tracer.Start(
|
|
||||||
ctx,
|
|
||||||
spanName,
|
|
||||||
trace.WithSpanKind(trace.SpanKindServer),
|
|
||||||
)
|
|
||||||
|
|
||||||
// Define required attributes
|
|
||||||
attrs := []Attribute{
|
|
||||||
semconv.RPCSystemKey.String(rpc.System),
|
|
||||||
semconv.RPCServiceKey.String(rpc.Service),
|
|
||||||
semconv.RPCMethodKey.String(rpc.Method),
|
|
||||||
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...)
|
return StartSpanWithTracer(ctx, otel.Tracer(""), spanName, attributes...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartSpanWithTracer requires a tracer to be passed in and creates a SpanKind=INTERNAL span.
|
// StartSpanWithTracer requires a tracer to be passed in and creates a SpanKind=INTERNAL span.
|
||||||
func StartSpanWithTracer(
|
func StartSpanWithTracer(ctx context.Context, tracer trace.Tracer, name string, attributes ...Attribute) (context.Context, trace.Span, func(error)) {
|
||||||
ctx context.Context,
|
return startSpan(ctx, tracer, name, attributes...)
|
||||||
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.
|
// RPCInfo contains information about the RPC request.
|
||||||
func startSpan(
|
type RPCInfo struct {
|
||||||
ctx context.Context,
|
System string
|
||||||
tracer trace.Tracer,
|
Service string
|
||||||
spanName string,
|
Method string
|
||||||
attributes ...Attribute,
|
RequestID string
|
||||||
) (context.Context, trace.Span, func(error)) {
|
}
|
||||||
ctx, span := tracer.Start(
|
|
||||||
ctx,
|
// StartServerSpan creates a SpanKind=SERVER span at the JSON-RPC boundary.
|
||||||
spanName,
|
// The span name is formatted as $rpcSystem.$rpcService/$rpcMethod
|
||||||
trace.WithSpanKind(trace.SpanKindInternal),
|
// (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)) {
|
||||||
|
var (
|
||||||
|
name = fmt.Sprintf("%s.%s/%s", rpc.System, rpc.Service, rpc.Method)
|
||||||
|
attrs = []Attribute{
|
||||||
|
semconv.RPCSystemKey.String(rpc.System),
|
||||||
|
semconv.RPCServiceKey.String(rpc.Service),
|
||||||
|
semconv.RPCMethodKey.String(rpc.Method),
|
||||||
|
semconv.RPCJSONRPCRequestID(rpc.RequestID),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
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...)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue