-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context propagation support (#783)
* Add context propagation support In the current implementation, weaver doesn't allow the user to propagate context information. We recommend users to define a struct that encapsulates the metadata information and add it as an argument to the method. However, more and more users are asking for an option to propagate metadata information using the context. This request comes especially from users that are using gRPC to communicate between their services, and gRPC provides a way to propagate metadata information using the context. This PR enables the users to propagate metadata information as a map[string]string. ```main.go // To attach metadata with key "foo" and value "bar" to the context, you can do: ctx := context.Background() ctx = metadata.NewContext(ctx, map[string]string{"foo": "bar"}) // To read the metadata value associated with a key "foo" in the context, you can do: meta, found := metadata.FromContext(ctx) if found { value := meta["foo"] } ``` [1] https://pkg.go.dev/google.golang.org/grpc/metadata * Header enc/dec into their own routines * Last fixes
- Loading branch information
Showing
11 changed files
with
656 additions
and
83 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
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,54 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package call | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ServiceWeaver/weaver/metadata" | ||
"github.com/ServiceWeaver/weaver/runtime/codegen" | ||
) | ||
|
||
// writeContextMetadata serializes the context metadata (if any) into enc. | ||
func writeContextMetadata(ctx context.Context, enc *codegen.Encoder) { | ||
m, found := metadata.FromContext(ctx) | ||
if !found { | ||
enc.Bool(false) | ||
return | ||
} | ||
enc.Bool(true) | ||
enc.Len(len(m)) | ||
for k, v := range m { | ||
enc.String(k) | ||
enc.String(v) | ||
} | ||
} | ||
|
||
// readContextMetadata returns the context metadata (if any) stored in dec. | ||
func readContextMetadata(ctx context.Context, dec *codegen.Decoder) context.Context { | ||
hasMeta := dec.Bool() | ||
if !hasMeta { | ||
return ctx | ||
} | ||
n := dec.Len() | ||
res := make(map[string]string, n) | ||
var k, v string | ||
for i := 0; i < n; i++ { | ||
k = dec.String() | ||
v = dec.String() | ||
res[k] = v | ||
} | ||
return metadata.NewContext(ctx, res) | ||
} |
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
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
Oops, something went wrong.