Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use official otelconnect interceptor #49

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/saga/start/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Module = fx.Options(
// Register our Connect-Go server
path, handler := temporalConnect.NewTemporalServiceHandler(
svc,
connect.WithInterceptors(pkgConnect.UnaryInterceptors()...),
connect.WithInterceptors(pkgConnect.Interceptors()...),
)

return modConnect.HandlerOutput{
Expand Down
2 changes: 1 addition & 1 deletion cmd/svc/license/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Module = fx.Options(
// Register our Connect-Go server
path, handler := licenseConnect.NewLicenseServiceHandler(
svc,
connect.WithInterceptors(pkgConnect.UnaryInterceptors()...),
connect.WithInterceptors(pkgConnect.Interceptors()...),
)

return modConnect.HandlerOutput{
Expand Down
2 changes: 1 addition & 1 deletion cmd/svc/org/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Module = fx.Options(
// Register our Connect-Go server
path, handler := orgv1beta1connect.NewOrgServiceHandler(
svc,
connect.WithInterceptors(pkgConnect.UnaryInterceptors()...),
connect.WithInterceptors(pkgConnect.Interceptors()...),
)

return modConnect.HandlerOutput{
Expand Down
2 changes: 1 addition & 1 deletion cmd/svc/profile/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var Module = fx.Options(
// Register our Connect-Go server
path, handler := profilev1beta1connect.NewProfileServiceHandler(
svc,
connect.WithInterceptors(pkgConnect.UnaryInterceptors()...),
connect.WithInterceptors(pkgConnect.Interceptors()...),
)

return modConnect.HandlerOutput{
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
buf.build/gen/go/kevinmichaelchen/temporalapis/connectrpc/go v1.11.1-20230620011625-99cb60d4ce70.1
buf.build/gen/go/kevinmichaelchen/temporalapis/protocolbuffers/go v1.31.0-20230620011625-99cb60d4ce70.1
connectrpc.com/connect v1.12.0
connectrpc.com/otelconnect v0.6.0
github.com/bufbuild/connect-grpchealth-go v1.1.1
github.com/bufbuild/protovalidate-go v0.3.4
github.com/google/uuid v1.3.1
Expand All @@ -26,7 +27,6 @@ require (
go.opentelemetry.io/otel v1.19.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.19.0
go.opentelemetry.io/otel/sdk v1.19.0
go.opentelemetry.io/otel/trace v1.19.0
go.temporal.io/sdk v1.25.1
go.temporal.io/sdk/contrib/opentelemetry v0.3.0
go.uber.org/fx v1.20.1
Expand Down Expand Up @@ -59,6 +59,7 @@ require (
github.com/stretchr/objx v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
go.temporal.io/api v1.24.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,8 @@ cloud.google.com/go/workflows v1.10.0/go.mod h1:fZ8LmRmZQWacon9UCX1r/g/DfAXx5VcP
cloud.google.com/go/workflows v1.11.1/go.mod h1:Z+t10G1wF7h8LgdY/EmRcQY8ptBD/nvofaL6FqlET6g=
connectrpc.com/connect v1.12.0 h1:HwKdOY0lGhhoHdsza+hW55aqHEC64pYpObRNoAgn70g=
connectrpc.com/connect v1.12.0/go.mod h1:3AGaO6RRGMx5IKFfqbe3hvK1NqLosFNP2BxDYTPmNPo=
connectrpc.com/otelconnect v0.6.0 h1:VJAdQL9+sgdUw9+7+J+jq8pQo/h1S7tSFv2+vDcR7bU=
connectrpc.com/otelconnect v0.6.0/go.mod h1:jdcs0uiwXQVmSMgTJ2dAaWR5VbpNd7QKNkuoH7n86RA=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
gioui.org v0.0.0-20210308172011-57750fc8a0a6/go.mod h1:RSH6KIUZ0p2xy5zHDxgAM4zumjgTw83q2ge/PI+yyw8=
git.sr.ht/~sbinet/gg v0.3.1/go.mod h1:KGYtlADtqsqANL9ueOFkWymvzUvLMQllU5Ixo+8v3pc=
Expand Down Expand Up @@ -1158,6 +1160,8 @@ go.opentelemetry.io/otel/metric v1.19.0/go.mod h1:L5rUsV9kM1IxCj1MmSdS+JQAcVm319
go.opentelemetry.io/otel/sdk v1.2.0/go.mod h1:jNN8QtpvbsKhgaC6V5lHiejMoKD+V8uadoSafgHPx1U=
go.opentelemetry.io/otel/sdk v1.19.0 h1:6USY6zH+L8uMH8L3t1enZPR3WFEmSTADlqldyHtJi3o=
go.opentelemetry.io/otel/sdk v1.19.0/go.mod h1:NedEbbS4w3C6zElbLdPJKOpJQOrGUJ+GfzpjUvI0v1A=
go.opentelemetry.io/otel/sdk/metric v1.19.0 h1:EJoTO5qysMsYCa+w4UghwFV/ptQgqSL/8Ni+hx+8i1k=
go.opentelemetry.io/otel/sdk/metric v1.19.0/go.mod h1:XjG0jQyFJrv2PbMvwND7LwCEhsJzCzV5210euduKcKY=
go.opentelemetry.io/otel/trace v1.2.0/go.mod h1:N5FLswTubnxKxOJHM7XZC074qpeEdLy3CgAVsdMucK0=
go.opentelemetry.io/otel/trace v1.19.0 h1:DFVQmlVbfVeOuBRrwdtaehRrWiL1JoVs9CPIQ1Dzxpg=
go.opentelemetry.io/otel/trace v1.19.0/go.mod h1:mfaSyvGyEJEI0nyV2I4qhNQnbBOUUmYZpYojqMnX2vo=
Expand Down
113 changes: 0 additions & 113 deletions pkg/connect/extract.go

This file was deleted.

60 changes: 11 additions & 49 deletions pkg/connect/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,20 @@
package connect

import (
"context"

"connectrpc.com/connect"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
grpccodes "google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"connectrpc.com/otelconnect"
)

// UnaryInterceptors - Unary interceptors for Connect Go servers.
func UnaryInterceptors() []connect.Interceptor {
// Interceptors - Interceptors for Connect Go servers.
func Interceptors() []connect.Interceptor {
return []connect.Interceptor{
connectInterceptorForSpan(),
otelconnect.NewInterceptor(
// Trust the client's tracing information. With this option, servers
// will create child spans for each request.
otelconnect.WithTrustRemote(),
// Reduce metrics and tracing cardinality. Drop high-cardinality
// attributes like the server IP address and the remote port number.
otelconnect.WithoutServerPeerAttributes(),
),
}
}

func connectInterceptorForSpan() connect.UnaryInterceptorFunc {
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc {
return connect.UnaryFunc(func(
ctx context.Context,
req connect.AnyRequest,
) (connect.AnyResponse, error) {
fullMethod := req.Spec().Procedure // e.g., "/acme.foo.v1.FooService/Bar"

tracer := otel.Tracer("")

ctx = extract(ctx, otel.GetTextMapPropagator(), req)

name, attr := spanInfo(fullMethod, peerFromCtx(ctx))

// Create a new span
ctx, span := tracer.Start(
trace.ContextWithRemoteSpanContext(ctx, trace.SpanContextFromContext(ctx)),
name,
trace.WithSpanKind(trace.SpanKindServer),
trace.WithAttributes(attr...),
)
defer span.End()

resp, err := next(ctx, req)
if err != nil {
s, _ := status.FromError(err)
span.SetStatus(codes.Error, s.Message())
span.SetAttributes(statusCodeAttr(s.Code()))
} else {
span.SetAttributes(statusCodeAttr(grpccodes.OK))
}

return resp, err
})
}

return interceptor
}