-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jet/tracing): add otel protocol based tracing middleware (#339)
* feat(jet/tracing): add otel protocol based tracing middleware * feat(jet/tracing): added an otel protocol based tracing middleware * feat(jet/tracing): added an otel protocol based tracing middleware
- Loading branch information
Showing
7 changed files
with
189 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package jet | ||
|
||
import "context" | ||
|
||
type contextClientKey struct{} | ||
|
||
// ContextWithClient returns a new Context that carries value. | ||
func ContextWithClient(ctx context.Context, client *Client) context.Context { | ||
return context.WithValue(ctx, contextClientKey{}, client) | ||
} | ||
|
||
// ClientFromContext returns the Client value stored in ctx, if any. | ||
func ClientFromContext(ctx context.Context) (*Client, bool) { | ||
client, ok := ctx.Value(contextClientKey{}).(*Client) | ||
return client, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package jet | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestContext_Client(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
got1, ok1 := ClientFromContext(ctx) | ||
assert.False(t, ok1) | ||
assert.Nil(t, got1) | ||
|
||
client := &Client{} | ||
ctx = ContextWithClient(context.Background(), client) | ||
|
||
got2, ok2 := ClientFromContext(ctx) | ||
assert.True(t, ok2) | ||
assert.Equal(t, client, got2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package tracing | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/otel" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/codes" | ||
"go.opentelemetry.io/otel/propagation" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.26.0" | ||
"go.opentelemetry.io/otel/trace" | ||
|
||
"github.com/go-kratos-ecosystem/components/v2/hyperf/jet" | ||
) | ||
|
||
const instrumentation = "github.com/go-kratos-ecosystem/components/v2/hyperf/jet/middleware/tracing" | ||
|
||
type options struct { | ||
mp propagation.TextMapPropagator | ||
tp trace.TracerProvider | ||
attrs []attribute.KeyValue | ||
} | ||
|
||
type Option func(*options) | ||
|
||
func WithPropagator(mp propagation.TextMapPropagator) Option { | ||
return func(o *options) { | ||
o.mp = mp | ||
} | ||
} | ||
|
||
func WithTracerProvider(tp trace.TracerProvider) Option { | ||
return func(o *options) { | ||
o.tp = tp | ||
} | ||
} | ||
|
||
func WithAttributes(attrs ...attribute.KeyValue) Option { | ||
return func(o *options) { | ||
o.attrs = append(o.attrs, attrs...) | ||
} | ||
} | ||
|
||
func newOptions(opts ...Option) options { | ||
o := options{ | ||
mp: otel.GetTextMapPropagator(), | ||
tp: otel.GetTracerProvider(), | ||
} | ||
for _, opt := range opts { | ||
opt(&o) | ||
} | ||
return o | ||
} | ||
|
||
func New(opts ...Option) jet.Middleware { | ||
o := newOptions(opts...) | ||
|
||
tracer := o.tp.Tracer(instrumentation) | ||
return func(next jet.Handler) jet.Handler { | ||
return func(ctx context.Context, service, method string, request any) (response any, err error) { | ||
ctx, span := tracer.Start(ctx, service+"/"+method, | ||
trace.WithSpanKind(trace.SpanKindClient), | ||
) | ||
defer span.End() | ||
|
||
attrs := []attribute.KeyValue{ | ||
semconv.RPCService(service), | ||
semconv.RPCMethod(method), | ||
// semconv.RPCJsonrpcErrorCode(0), // todo | ||
// semconv.RPCJsonrpcErrorMessage(""), // todo | ||
// semconv.RPCJsonrpcRequestID(""), // todo | ||
// semconv.ServerAddress(""), // todo | ||
// semconv.ServerPort(0), // todo | ||
// semconv.NetworkPeerAddress(""), // todo | ||
// semconv.NetworkPeerPort(0), // todo | ||
} | ||
attrs = append(attrs, formatterAttributes(ctx)...) | ||
attrs = append(attrs, transportAttributes(ctx)...) | ||
attrs = append(attrs, o.attrs...) | ||
|
||
span.SetAttributes(attrs...) | ||
|
||
response, err = next(ctx, service, method, request) | ||
if err != nil { | ||
span.SetStatus(codes.Error, err.Error()) | ||
span.RecordError(err) | ||
} | ||
|
||
return | ||
} | ||
} | ||
} | ||
|
||
func formatterAttributes(ctx context.Context) []attribute.KeyValue { | ||
client, ok := jet.ClientFromContext(ctx) | ||
if !ok { | ||
return []attribute.KeyValue{} | ||
} | ||
|
||
switch formatter := client.GetFormatter(); formatter.Kind() { | ||
case jet.FormatterKindJSONRPC: | ||
return []attribute.KeyValue{ | ||
semconv.RPCSystemKey.String("jsonrpc"), | ||
semconv.RPCJsonrpcVersion(jet.JSONRPCVersion), | ||
} | ||
default: | ||
return []attribute.KeyValue{} | ||
} | ||
} | ||
|
||
func transportAttributes(ctx context.Context) []attribute.KeyValue { | ||
client, ok := jet.ClientFromContext(ctx) | ||
if !ok { | ||
return []attribute.KeyValue{} | ||
} | ||
|
||
switch transporter := client.GetTransporter().(type) { | ||
case *jet.HTTPTransporter: | ||
return []attribute.KeyValue{ | ||
semconv.ServerAddress(transporter.Addr), // todo: split host and port | ||
} | ||
default: | ||
return []attribute.KeyValue{} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters