zanzibar

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 48 Imported by: 1,056

Documentation

Index

Constants

View Source
const (

	// MetricEndpointPanics is endpoint level panic counter
	MetricEndpointPanics = "endpoint.panic"

	// MetricEndpointAppErrors is the metric name for endpoint level application error for TChannel
	MetricEndpointAppErrors = "endpoint.app-errors"

	// TraceIDKey is the log field key containing the associated trace id
	TraceIDKey = "trace.traceId"
	// TraceSpanKey is the log field key containing the associated span id
	TraceSpanKey = "trace.span"
	// TraceSampledKey is the log field key for whether a trace was sampled or not
	TraceSampledKey = "trace.sampled"

	// ClientResponseDurationKey is the key denoting a downstream response duration
	ClientResponseDurationKey = "client.response.duration"
	// ClientTypeKey denotes the type of the client, usually http / tchannel / client-less / custom
	ClientTypeKey = "client.type"
)

Variables

This section is empty.

Functions

func EnsureEmpty

func EnsureEmpty(r io.Reader, stage string) error

EnsureEmpty ensures that the specified reader is empty. If the reader is not empty, it returns an error with the specified stage in the message.

func GetAPIEnvironment added in v1.1.0

func GetAPIEnvironment(endpoint *RouterEndpoint, r *http.Request) string

GetAPIEnvironment returns the api environment for a given request. By default, the api environment is set to production. However, there may be use cases where a different environment may be required for monitoring purposes. This may be overridden by a non-empty environment value in the request header.

func GetBuffer

func GetBuffer() *bytes.Buffer

GetBuffer returns a new Byte Buffer from the buffer pool that has been reset

func GetCtxLogCounterFromCtx added in v1.1.0

func GetCtxLogCounterFromCtx(ctx context.Context) int

GetCtxLogCounterFromCtx returns ctxLogCounter value from ctx

func GetCtxLogLevelOrDebugLevelFromCtx added in v1.1.0

func GetCtxLogLevelOrDebugLevelFromCtx(ctx context.Context) zapcore.Level

GetCtxLogLevelOrDebugLevelFromCtx returns ctxLogLevel value from ctx

func GetDirnameFromRuntimeCaller

func GetDirnameFromRuntimeCaller(file string) string

GetDirnameFromRuntimeCaller will compute the current dirname if passed a filename from runtime.Caller(0). This is useful for doing __dirname/__FILE__ for golang.

func GetEndpointRequestHeadersFromCtx added in v0.2.0

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

GetEndpointRequestHeadersFromCtx returns the endpoint request headers, if it exists on context

func GetHostname

func GetHostname() string

GetHostname returns hostname

func GetLogFieldsFromCtx added in v0.3.0

func GetLogFieldsFromCtx(ctx context.Context) []zap.Field

GetLogFieldsFromCtx returns the log fields attached to the context.Context

func GetRequestEndpointFromCtx

func GetRequestEndpointFromCtx(ctx context.Context) string

GetRequestEndpointFromCtx returns the endpoint, if it exists on context

func GetRoutingDelegateFromCtx

func GetRoutingDelegateFromCtx(ctx context.Context) string

GetRoutingDelegateFromCtx returns the tchannel routing delegate info extracted from context.

func GetScopeTagsFromCtx added in v0.2.0

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

GetScopeTagsFromCtx returns the tag info extracted from context.

func GetShardKeyFromCtx added in v0.4.3

func GetShardKeyFromCtx(ctx context.Context) string

GetShardKeyFromCtx returns the tchannel shardkey info extracted from context.

func LogErrorWarnTimeout deprecated

func LogErrorWarnTimeout(logger *zap.Logger, err error, msg string)

LogErrorWarnTimeout logs warnings for timeout errors, otherwise logs errors TODO: We want to improve the classification of errors, similar to: https://github.com/uber/tchannel-node/blob/master/errors.js#L907-L930

Deprecated: use proper level to log instead

func NewTChannelLogger

func NewTChannelLogger(logger *zap.Logger) tchannel.Logger

NewTChannelLogger creates a TChannel logger given a zap logger

func NewTChannelStatsReporter

func NewTChannelStatsReporter(scope tally.Scope) tchannel.StatsReporter

NewTChannelStatsReporter returns a StatsReporter using the given tally.Scope.

func ParamsFromContext added in v0.2.0

func ParamsFromContext(ctx context.Context) url.Values

ParamsFromContext extracts the URL parameters that are embedded in the context by the Zanzibar HTTP router implementation.

func PutBuffer

func PutBuffer(buf *bytes.Buffer)

PutBuffer returns byte buffer to the buffer pool

func ReadHeaders

func ReadHeaders(r io.Reader) (map[string]string, error)

ReadHeaders reads key-value pairs encoded using WriteHeaders.

func ReadStruct

func ReadStruct(reader io.Reader, s RWTStruct) error

ReadStruct reads the given Thriftrw struct.

func RequestUUIDFromCtx added in v0.3.0

func RequestUUIDFromCtx(ctx context.Context) string

RequestUUIDFromCtx returns the RequestUUID, if it exists on context

func WithEndpointField

func WithEndpointField(ctx context.Context, endpoint string) context.Context

WithEndpointField adds the endpoint information in the request context.

func WithEndpointRequestHeadersField added in v0.2.0

func WithEndpointRequestHeadersField(ctx context.Context, requestHeaders map[string]string) context.Context

WithEndpointRequestHeadersField adds the endpoint request header information in the request context.

func WithLogFields added in v0.1.2

func WithLogFields(ctx context.Context, newFields ...zap.Field) context.Context

WithLogFields returns a new context with the given log fields attached to context.Context

func WithRoutingDelegate

func WithRoutingDelegate(ctx context.Context, rd string) context.Context

WithRoutingDelegate adds the tchannel routing delegate information in the request context.

func WithScopeTags added in v0.2.0

func WithScopeTags(ctx context.Context, newFields map[string]string) context.Context

WithScopeTags returns a new context with the given scope tags attached to context.Context

func WithShardKey added in v0.4.3

func WithShardKey(ctx context.Context, sk string) context.Context

WithShardKey adds the tchannel shard key information in the request context.

func WriteHeaders

func WriteHeaders(w io.Writer, headers map[string]string) error

WriteHeaders writes the given key-value pairs using the following encoding: len~2 (k~4 v~4)~len

Types

type ClientHTTPRequest

type ClientHTTPRequest struct {
	ClientID             string
	ClientTargetEndpoint string
	MethodName           string
	Metrics              ContextMetrics

	Logger        *zap.Logger
	ContextLogger ContextLogger
	// contains filtered or unexported fields
}

ClientHTTPRequest is the struct for making a single client request using an outbound http client.

func NewClientHTTPRequest

func NewClientHTTPRequest(
	ctx context.Context,
	clientID string,
	clientMethod string,
	clientTargetEndpoint string,
	client *HTTPClient,
) *ClientHTTPRequest

NewClientHTTPRequest allocates a ClientHTTPRequest. The ctx parameter is the context associated with the outbound requests.

func (*ClientHTTPRequest) CheckHeaders

func (req *ClientHTTPRequest) CheckHeaders(expected []string) error

CheckHeaders verifies that the outbound request contains required headers

func (*ClientHTTPRequest) Do

Do will send the request out.

func (*ClientHTTPRequest) InjectSpanToHeader

func (req *ClientHTTPRequest) InjectSpanToHeader(span opentracing.Span, format interface{}) error

InjectSpanToHeader will inject span to request header This method is current used for unit tests TODO: we need to set source and test code as same pkg name which would makes UTs easier

func (*ClientHTTPRequest) WriteBytes added in v0.4.5

func (req *ClientHTTPRequest) WriteBytes(
	method, url string,
	headers map[string]string,
	rawBody []byte,
) error

WriteBytes materialize the HTTP request with given method, url, headers and body. Body is assumed to be a byte array.s

func (*ClientHTTPRequest) WriteJSON

func (req *ClientHTTPRequest) WriteJSON(
	method, url string,
	headers map[string]string,
	body interface{},
) error

WriteJSON materialize the HTTP request with given method, url, headers and body.

type ClientHTTPResponse

type ClientHTTPResponse struct {
	StatusCode int
	Duration   time.Duration
	Header     http.Header
	// contains filtered or unexported fields
}

ClientHTTPResponse is the struct managing the client response when making outbound http calls.

func NewClientHTTPResponse

func NewClientHTTPResponse(
	req *ClientHTTPRequest,
) *ClientHTTPResponse

NewClientHTTPResponse allocates a client http response object to track http response.

func (*ClientHTTPResponse) CheckOKResponse

func (res *ClientHTTPResponse) CheckOKResponse(okResponses []int)

CheckOKResponse checks if the status code is OK.

func (*ClientHTTPResponse) GetRawBody

func (res *ClientHTTPResponse) GetRawBody() []byte

GetRawBody returns the body as byte array if it has been read.

func (*ClientHTTPResponse) ReadAll

func (res *ClientHTTPResponse) ReadAll() ([]byte, error)

ReadAll reads bytes from response.

func (*ClientHTTPResponse) ReadAndUnmarshalBody

func (res *ClientHTTPResponse) ReadAndUnmarshalBody(v interface{}) error

ReadAndUnmarshalBody will try to unmarshal non pointer value or fail

func (*ClientHTTPResponse) ReadAndUnmarshalBodyMultipleOptions added in v0.3.0

func (res *ClientHTTPResponse) ReadAndUnmarshalBodyMultipleOptions(vs []interface{}) (interface{}, error)

ReadAndUnmarshalBodyMultipleOptions will try to unmarshal non pointer value to one of the provided types or fail It will return the deserialized struct (if any) that succeeded

func (*ClientHTTPResponse) UnmarshalBody added in v0.4.5

func (res *ClientHTTPResponse) UnmarshalBody(v interface{}, rawBody []byte) error

UnmarshalBody helper to unmarshal body into struct

type ConfigOption

type ConfigOption struct {
	// contains filtered or unexported fields
}

ConfigOption points to a collection of bytes representing a file. This is either the full contents of the file as bytes, or a string poiting to a file

func ConfigFileContents

func ConfigFileContents(fileBytes []byte) *ConfigOption

ConfigFileContents creates a ConfigFile representing the contents of the file

func ConfigFilePath

func ConfigFilePath(path string) *ConfigOption

ConfigFilePath creates a ConfigFile represented as a path

type ContextExtractor added in v0.2.0

type ContextExtractor interface {
	ExtractScopeTags(ctx context.Context) map[string]string
	ExtractLogFields(ctx context.Context) []zap.Field
}

ContextExtractor is a extractor that extracts some log fields from the context

type ContextExtractors added in v0.2.0

type ContextExtractors struct {
	ScopeTagsExtractors []ContextScopeTagsExtractor
	LogFieldsExtractors []ContextLogFieldsExtractor
}

ContextExtractors warps extractors for context, implements ContextExtractor interface

func (*ContextExtractors) ExtractLogFields added in v0.2.0

func (c *ContextExtractors) ExtractLogFields(ctx context.Context) []zap.Field

ExtractLogFields extracts log fields from a context into a field.

func (*ContextExtractors) ExtractScopeTags added in v0.2.0

func (c *ContextExtractors) ExtractScopeTags(ctx context.Context) map[string]string

ExtractScopeTags extracts scope fields from a context into a tag.

type ContextLogFieldsExtractor added in v0.2.0

type ContextLogFieldsExtractor func(context.Context) []zap.Field

ContextLogFieldsExtractor defines func where extracts log fields from context

type ContextLogger added in v0.1.2

type ContextLogger interface {
	Debug(ctx context.Context, msg string, fields ...zap.Field) context.Context
	Error(ctx context.Context, msg string, fields ...zap.Field) context.Context
	Info(ctx context.Context, msg string, fields ...zap.Field) context.Context
	Panic(ctx context.Context, msg string, fields ...zap.Field) context.Context
	Warn(ctx context.Context, msg string, fields ...zap.Field) context.Context

	// DebugZ skips logs, and adds to context if skipZanzibarLogs is set to true otherwise behaves as normal Debug, similarly for other XxxxZ's
	DebugZ(ctx context.Context, msg string, fields ...zap.Field) context.Context
	ErrorZ(ctx context.Context, msg string, fields ...zap.Field) context.Context
	InfoZ(ctx context.Context, msg string, fields ...zap.Field) context.Context
	PanicZ(ctx context.Context, msg string, fields ...zap.Field) context.Context
	WarnZ(ctx context.Context, msg string, fields ...zap.Field) context.Context

	// Other utility methods on the logger
	Check(lvl zapcore.Level, msg string) *zapcore.CheckedEntry

	SetSkipZanzibarLogs(bool)
}

ContextLogger is a logger that extracts some log fields from the context before passing through to underlying zap logger. In cases it also updates the context instead of logging

func NewContextLogger added in v0.1.2

func NewContextLogger(log *zap.Logger) ContextLogger

NewContextLogger returns a logger that extracts log fields a context before passing through to underlying zap logger.

type ContextMetrics added in v0.2.0

type ContextMetrics interface {
	IncCounter(ctx context.Context, name string, value int64)
	RecordTimer(ctx context.Context, name string, d time.Duration)
	RecordHistogramDuration(ctx context.Context, name string, d time.Duration)
}

ContextMetrics emit metrics with tags extracted from context.

func NewContextMetrics added in v0.2.0

func NewContextMetrics(scope tally.Scope) ContextMetrics

NewContextMetrics create ContextMetrics to emit metrics with tags extracted from context.

type ContextScopeTagsExtractor added in v0.2.0

type ContextScopeTagsExtractor func(context.Context) map[string]string

ContextScopeTagsExtractor defines func where extracts tags from context

type DefaultDependencies

type DefaultDependencies struct {
	// ContextExtractor extracts context for scope and logs field
	ContextExtractor ContextExtractor
	// ContextLogger is a logger with request-scoped log fields
	ContextLogger ContextLogger
	// ContextMetrics emit metrics from context
	ContextMetrics ContextMetrics

	Logger         *zap.Logger
	Scope          tally.Scope
	Tracer         opentracing.Tracer
	Config         *StaticConfig
	ServerTChannel *tchannel.Channel
	Gateway        *Gateway

	// dispatcher for managing gRPC clients
	GRPCClientDispatcher *yarpc.Dispatcher

	// JSONWrapper provides json marshaling and unmarshaling functionality
	JSONWrapper jsonwrapper.JSONWrapper
}

DefaultDependencies are the common dependencies for all modules

type GRPCClientCallHelper added in v0.4.3

type GRPCClientCallHelper interface {
	// Start method should be used just before calling the actual gRPC client method call.
	Start()
	// Finish method should be used right after the actual call to gRPC client method.
	Finish(ctx context.Context, err error) context.Context
}

GRPCClientCallHelper is used to track internal state of logging and metrics.

func NewGRPCClientCallHelper added in v0.4.3

func NewGRPCClientCallHelper(ctx context.Context, serviceMethod string, opts *GRPCClientOpts) (context.Context, GRPCClientCallHelper)

NewGRPCClientCallHelper used to initialize a helper that will be used to track logging and metric for a gRPC Client call.

type GRPCClientOpts added in v0.4.3

type GRPCClientOpts struct {
	ContextLogger          ContextLogger
	Metrics                ContextMetrics
	ContextExtractor       ContextExtractor
	RoutingKey             string
	RequestUUIDHeaderKey   string
	CircuitBreakerDisabled bool
	Timeout                time.Duration
	ScopeTags              map[string]map[string]string
}

GRPCClientOpts used to configure various client options.

func NewGRPCClientOpts added in v0.4.3

func NewGRPCClientOpts(
	contextLogger ContextLogger,
	metrics ContextMetrics,
	contextExtractor ContextExtractor,
	methodNames map[string]string,
	clientID, routingKey, requestUUIDHeaderKey string,
	circuitBreakerDisabled bool,
	timeoutInMS int,
) *GRPCClientOpts

NewGRPCClientOpts creates a new instance of GRPCClientOpts.

type Gateway

type Gateway struct {
	HTTPPort               int32
	TChannelPort           int32
	RealHTTPPort           int32
	RealHTTPAddr           string
	RealTChannelPort       int32
	RealTChannelAddr       string
	WaitGroup              *sync.WaitGroup
	ServerTChannel         *tchannel.Channel
	ClientTChannels        map[string]*tchannel.Channel
	ContextLogger          ContextLogger
	ContextMetrics         ContextMetrics
	ContextExtractor       ContextExtractor
	RootScope              tally.Scope
	Logger                 *zap.Logger
	ServiceName            string
	Config                 *StaticConfig
	HTTPRouter             HTTPRouter
	ServerTChannelRouter   *TChannelRouter
	TChannelSubLoggerLevel zapcore.Level
	Tracer                 opentracing.Tracer
	JSONWrapper            jsonwrapper.JSONWrapper

	// gRPC client dispatcher for gRPC client lifecycle management
	GRPCClientDispatcher *yarpc.Dispatcher
	// contains filtered or unexported fields
}

Gateway type

func CreateGateway

func CreateGateway(
	config *StaticConfig, opts *Options,
) (*Gateway, error)

CreateGateway func

func (*Gateway)