github.com/blend/go-sdk@v1.20220411.3/tracing/grpctrace/metadata_reader_writer.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package grpctrace
     9  
    10  import (
    11  	"strings"
    12  
    13  	"google.golang.org/grpc/metadata"
    14  )
    15  
    16  // MetadataReaderWriter satisfies both the opentracing.TextMapReader and
    17  // opentracing.TextMapWriter interfaces.
    18  type MetadataReaderWriter struct {
    19  	metadata.MD
    20  }
    21  
    22  // Set sets a key a value.
    23  func (w MetadataReaderWriter) Set(key, val string) {
    24  	// The GRPC HPACK implementation rejects any uppercase keys here.
    25  	//
    26  	// As such, since the HTTP_HEADERS format is case-insensitive anyway, we
    27  	// blindly lowercase the key (which is guaranteed to work in the
    28  	// Inject/Extract sense per the OpenTracing spec).
    29  	key = strings.ToLower(key)
    30  	w.MD[key] = append(w.MD[key], val)
    31  }
    32  
    33  // ForeachKey does something for each key.
    34  func (w MetadataReaderWriter) ForeachKey(handler func(key, val string) error) error {
    35  	for k, vals := range w.MD {
    36  		for _, v := range vals {
    37  			if err := handler(k, v); err != nil {
    38  				return err
    39  			}
    40  		}
    41  	}
    42  	return nil
    43  }