Skip to content

Commit

Permalink
grpc: Add external dependencies to context
Browse files Browse the repository at this point in the history
  • Loading branch information
monstermunchkin committed Aug 29, 2024
1 parent 68dbe32 commit f59bc40
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"

"github.com/pace/bricks/http/middleware"
"github.com/pace/bricks/http/security"
"github.com/pace/bricks/locale"
"github.com/pace/bricks/maintenance/log"
Expand Down Expand Up @@ -81,5 +82,10 @@ func prepareClientContext(ctx context.Context) context.Context {
ctx = metadata.AppendToOutgoingContext(ctx, "req_id", reqID)
}
ctx = EncodeContextWithUTMData(ctx)

if dep := middleware.ExternalDependencyContextFromContext(ctx); dep != nil {
ctx = metadata.AppendToOutgoingContext(ctx, MetadataKeyExternalDependencies, dep.String())
}

return ctx
}
5 changes: 5 additions & 0 deletions grpc/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package grpc

const (
MetadataKeyExternalDependencies = "external_dependencies"
)
10 changes: 10 additions & 0 deletions grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
"github.com/pace/bricks/http/middleware"
"github.com/pace/bricks/http/security"
"github.com/pace/bricks/locale"
"github.com/pace/bricks/maintenance/errors"
Expand Down Expand Up @@ -189,10 +190,19 @@ func prepareContext(ctx context.Context) (context.Context, metadata.MD) {
if bt := md.Get("bearer_token"); len(bt) > 0 {
ctx = security.ContextWithToken(ctx, security.TokenString(bt[0]))
}

// add external dependencies to context
if externalDependencies := md.Get(MetadataKeyExternalDependencies); len(externalDependencies) > 0 {
externalDependencyContext := middleware.ExternalDependencyContext{}
externalDependencyContext.Parse(externalDependencies[0])
ctx = middleware.ContextWithExternalDependency(ctx, &externalDependencyContext)
}

delete(md, "content-type")
delete(md, "locale")
delete(md, "bearer_token")
delete(md, "req_id")
delete(md, MetadataKeyExternalDependencies)

return ctx, md
}

0 comments on commit f59bc40

Please sign in to comment.