-
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.
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 = weavermetadata.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 := weavermetadata.FromContext(ctx) if found { value := meta["foo"] } ``` [1] https://pkg.go.dev/google.golang.org/grpc/metadata
- Loading branch information
Showing
9 changed files
with
563 additions
and
33 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,48 @@ | ||
// 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 ( | ||
"strings" | ||
|
||
"github.com/ServiceWeaver/weaver/runtime/codegen" | ||
) | ||
|
||
const metadataHeaderLen = 8 // metadata length | ||
|
||
// writeContextMetadata serializes the context metadata (if any). | ||
func writeContextMetadata(meta map[string]string) []byte { | ||
enc := codegen.NewEncoder() | ||
enc.Len(len(meta)) | ||
for k, v := range meta { | ||
enc.String(strings.ToLower(k)) | ||
enc.String(v) | ||
} | ||
return enc.Data() | ||
} | ||
|
||
// readContextMetadata returns the context metadata (if any). | ||
func readContextMetadata(meta []byte) map[string]string { | ||
dec := codegen.NewDecoder(meta) | ||
n := dec.Len() | ||
res := make(map[string]string, n) | ||
var k, v string | ||
for i := 0; i < n; i++ { | ||
k = strings.ToLower(dec.String()) | ||
v = dec.String() | ||
res[k] = v | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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 weavermetadata define the structure of the metadata supported by weaver. | ||
// | ||
// Note that we allow metadata information to propagate as a map, where the keys | ||
// and the values are strings. It the user wants to propagate values that are | ||
// more complicated types, it's their responsibility to encode/decode these types | ||
// to/from string values. | ||
// | ||
// Note that all keys are automatically lowercase. This ensures the user avoids | ||
// mistakes when creating and retrieving values for the same key. E.g., the user | ||
// can create a map with the key "Foo" but try to retrieve the value for the | ||
// key "foo". | ||
// | ||
// How to: | ||
// | ||
// To attach metadata with key "foo" and value "bar" to the context, you can do: | ||
// | ||
// ctx := context.Background() | ||
// ctx = weavermetadata.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 := weavermetadata.FromContext(ctx) | ||
// if found { | ||
// value := meta["foo"] | ||
// } | ||
package weavermetadata | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
) | ||
|
||
// metaKey is an unexported type for the key that stores the metadata. | ||
type metaKey struct{} | ||
|
||
// NewContext returns a new context that carries metadata meta. | ||
func NewContext(ctx context.Context, meta map[string]string) context.Context { | ||
return context.WithValue(ctx, metaKey{}, meta) | ||
} | ||
|
||
// FromContext returns the metadata value stored in ctx, if any. | ||
func FromContext(ctx context.Context) (map[string]string, bool) { | ||
meta, ok := ctx.Value(metaKey{}).(map[string]string) | ||
if !ok { | ||
return nil, false | ||
} | ||
out := make(map[string]string, len(meta)) | ||
for k, v := range meta { | ||
key := strings.ToLower(k) | ||
out[key] = v | ||
} | ||
return out, true | ||
} |
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,94 @@ | ||
// 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 weavermetadata | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestContextMetadata(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
meta map[string]string | ||
isMetaExpected bool | ||
want map[string]string | ||
} | ||
for _, test := range []testCase{ | ||
{ | ||
name: "no metadata", | ||
}, | ||
{ | ||
name: "with empty metadata", | ||
meta: map[string]string{}, | ||
isMetaExpected: false, | ||
}, | ||
{ | ||
name: "with valid metadata", | ||
meta: map[string]string{ | ||
"foo": "bar", | ||
"baz": "waldo", | ||
}, | ||
isMetaExpected: true, | ||
want: map[string]string{ | ||
"foo": "bar", | ||
"baz": "waldo", | ||
}, | ||
}, | ||
{ | ||
name: "with valid metadata and uppercase keys", | ||
meta: map[string]string{ | ||
"Foo": "bar", | ||
"Baz": "waldo", | ||
}, | ||
isMetaExpected: true, | ||
want: map[string]string{ | ||
"foo": "bar", | ||
"baz": "waldo", | ||
}, | ||
}, | ||
{ | ||
name: "with valid metadata and uppercase values", | ||
meta: map[string]string{ | ||
"Foo": "Bar", | ||
"Baz": "Waldo", | ||
}, | ||
isMetaExpected: true, | ||
want: map[string]string{ | ||
"foo": "Bar", | ||
"baz": "Waldo", | ||
}, | ||
}, | ||
} { | ||
t.Run(test.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
if len(test.meta) > 0 { | ||
ctx = NewContext(ctx, test.meta) | ||
} | ||
|
||
got, found := FromContext(ctx) | ||
if !reflect.DeepEqual(found, test.isMetaExpected) { | ||
t.Errorf("ExtractMetadata: expecting %v, got %v", test.isMetaExpected, found) | ||
} | ||
if !found { | ||
return | ||
} | ||
if !reflect.DeepEqual(test.want, got) { | ||
t.Errorf("ExtractMetadata: expecting %v, got %v", test.want, got) | ||
} | ||
}) | ||
} | ||
} |
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.