File tree Expand file tree Collapse file tree 3 files changed +34
-8
lines changed
Expand file tree Collapse file tree 3 files changed +34
-8
lines changed Original file line number Diff line number Diff line change @@ -98,3 +98,30 @@ func cloneHeaders(h map[string][]string) map[string][]string {
9898 }
9999 return c
100100}
101+
102+ // telemetryKey is a private type used to store/retrieve telemetry context values.
103+ type telemetryKey string
104+
105+ // WithTelemetryContext injects telemetry attribute values (like resource name
106+ // or client version) into the context. In accordance with standard Go context
107+ // guidelines, this should only be used for data that transits processes and APIs,
108+ // and not for passing optional parameters to functions. keyvals should have a
109+ // corresponding value for every key provided. If there is an odd number of keyvals
110+ // this method will panic.
111+ func WithTelemetryContext (ctx context.Context , keyvals ... string ) context.Context {
112+ if len (keyvals )% 2 != 0 {
113+ panic (fmt .Sprintf ("callctx: an even number of key value pairs must be provided, got %d" , len (keyvals )))
114+ }
115+
116+ for i := 0 ; i < len (keyvals ); i = i + 2 {
117+ ctx = context .WithValue (ctx , telemetryKey (keyvals [i ]), keyvals [i + 1 ])
118+ }
119+ return ctx
120+ }
121+
122+ // TelemetryFromContext extracts a telemetry attribute value from the context.
123+ // The returned bool indicates a successful typecast of the value to a string.
124+ func TelemetryFromContext (ctx context.Context , key string ) (string , bool ) {
125+ val , ok := ctx .Value (telemetryKey (key )).(string )
126+ return val , ok
127+ }
Original file line number Diff line number Diff line change @@ -36,19 +36,19 @@ import (
3636 "time"
3737
3838 "github.com/googleapis/gax-go/v2/apierror"
39- "google.golang.org/grpc/metadata "
39+ "github.com/googleapis/gax-go/v2/callctx "
4040)
4141
4242// APICall is a user defined call stub.
4343type APICall func (context.Context , CallSettings ) error
4444
4545// withRetryCount returns a new context with the retry count appended to
46- // gRPC metadata . The retry count is the number of retries that have been
46+ // the telemetry context . The retry count is the number of retries that have been
4747// attempted. On the initial request, retry count is 0.
4848// On a second request (the first retry), retry count is 1.
4949func withRetryCount (ctx context.Context , retryCount int ) context.Context {
50- // Add to gRPC metadata so it's visible to StatsHandlers
51- return metadata . AppendToOutgoingContext (ctx , "gcp.grpc. resend_count" , strconv .Itoa (retryCount ))
50+ // Add to telemetry context so it's visible to observability wrappers
51+ return callctx . WithTelemetryContext (ctx , "resend_count" , strconv .Itoa (retryCount ))
5252}
5353
5454// Invoke calls the given APICall, performing retries as specified by opts, if
Original file line number Diff line number Diff line change @@ -40,9 +40,9 @@ import (
4040 "github.com/google/go-cmp/cmp"
4141 "github.com/google/go-cmp/cmp/cmpopts"
4242 "github.com/googleapis/gax-go/v2/apierror"
43+ "github.com/googleapis/gax-go/v2/callctx"
4344 "google.golang.org/genproto/googleapis/rpc/errdetails"
4445 "google.golang.org/grpc/codes"
45- "google.golang.org/grpc/metadata"
4646 "google.golang.org/grpc/status"
4747)
4848
@@ -285,9 +285,8 @@ func TestInvokeRetryCount(t *testing.T) {
285285 calls := 0
286286 apiCall := func (ctx context.Context , _ CallSettings ) error {
287287 calls ++
288- md , _ := metadata .FromOutgoingContext (ctx )
289- if vals := md ["gcp.grpc.resend_count" ]; len (vals ) > 0 {
290- if count , err := strconv .Atoi (vals [0 ]); err == nil {
288+ if val , ok := callctx .TelemetryFromContext (ctx , "resend_count" ); ok {
289+ if count , err := strconv .Atoi (val ); err == nil {
291290 retryCounts = append (retryCounts , count )
292291 }
293292 }
You can’t perform that action at this time.
0 commit comments