callctx

package
v2.22.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2026 License: BSD-3-Clause Imports: 3 Imported by: 16

Documentation

Overview

Package callctx provides helpers for storing and retrieving values out of context.Context. These values are used by our client libraries in various ways across the stack.

Index

Examples

Constants

View Source
const (
	// XGoogFieldMaskHeader is the canonical header key for the [System Parameter]
	// that specifies the response read mask. The value(s) for this header
	// must adhere to format described in [fieldmaskpb].
	//
	// [System Parameter]: https://cloud.google.com/apis/docs/system-parameters
	// [fieldmaskpb]: https://google.golang.org/protobuf/types/known/fieldmaskpb
	XGoogFieldMaskHeader = "x-goog-fieldmask"
)

Variables

This section is empty.

Functions

func HeadersFromContext

func HeadersFromContext(ctx context.Context) map[string][]string

HeadersFromContext retrieves headers set from SetHeaders. These headers can then be cast to http.Header or metadata.MD to send along on requests.

func LoggerFromContext added in v2.19.0

func LoggerFromContext(ctx context.Context) (*slog.Logger, bool)

LoggerFromContext extracts a slog.Logger from the context. The returned bool indicates whether a logger was found.

func SetHeaders

func SetHeaders(ctx context.Context, keyvals ...string) context.Context

SetHeaders stores key value pairs in the returned context that can later be retrieved by HeadersFromContext. Values stored in this manner will automatically be retrieved by client libraries and sent as outgoing headers on all requests. keyvals should have a corresponding value for every key provided. If there is an odd number of keyvals this method will panic.

Example
package main

import (
	"context"
	"fmt"

	"github.com/googleapis/gax-go/v2/callctx"
)

func main() {
	ctx := context.Background()
	ctx = callctx.SetHeaders(ctx, "key", "value")

	// Send the returned context to the request you are making. Later on these
	// values will be retrieved and set on outgoing requests.

	headers := callctx.HeadersFromContext(ctx)
	fmt.Println(headers["key"][0])
}
Output:
value

func TelemetryFromContext added in v2.18.0

func TelemetryFromContext(ctx context.Context, key string) (string, bool)

TelemetryFromContext extracts a telemetry attribute value from the context. The returned bool indicates a successful typecast of the value to a string.

func WithLoggerContext added in v2.19.0

func WithLoggerContext(ctx context.Context, logger *slog.Logger) context.Context

WithLoggerContext injects a slog.Logger into the context. This logger will be extracted by the client library or transport wrappers to emit logs.

func WithTelemetryContext added in v2.18.0

func WithTelemetryContext(ctx context.Context, keyvals ...string) context.Context

WithTelemetryContext injects telemetry attribute values (like resource name or client version) into the context. In accordance with standard Go context guidelines, this should only be used for data that transits processes and APIs, and not for passing optional parameters to functions. keyvals should have a corresponding value for every key provided. If there is an odd number of keyvals this method will panic.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL