elephantine

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 28 Imported by: 5

README

Elephantine

Shared functionality for Elephant systems. It's most likely not something anyone outside of Elephant would be interested in.

Documentation

Index

Constants

View Source
const (
	// LogKeyLogLevel is the log level that an application was configured
	// with.
	LogKeyLogLevel = "log_level"
	// LogKeyError is an error message.
	LogKeyError = "err"
	// LogKeyErrorCode is an error code.
	LogKeyErrorCode = "err_code"
	// LogKeyErrorMeta is a JSON object with error metadata.
	LogKeyErrorMeta = "err_meta"
	// LogKeyCountMetric was planned to be used to increment a given metric
	// when used. TODO: not implemented yet, should it be removed?
	LogKeyCountMetric = "count_metric"
	// LogKeyDocumentUUID is the UUID of a document.
	LogKeyDocumentUUID = "document_uuid"
	// LogKeyTransaction is the name of a transaction, usually used to
	// identify a transaction that has failed.
	LogKeyTransaction = "transaction"
	// LogKeyOCSource is used to identify a source document from OC by UUID.
	LogKeyOCSource = "oc_source"
	// LogKeyOCVersion is the version of the OC document.
	LogKeyOCVersion = "oc_version"
	// LogKeyOCEvent is the type of an OC event- or content-log event.
	LogKeyOCEvent = "oc_event"
	// LogKeyChannel identifies a notification channel.
	LogKeyChannel = "channel"
	// LogKeyMessage can be used to log a unexpected message.
	LogKeyMessage = "message"
	// LogKeyDelay can be used to communicate the delay when logging
	// information about retry attempts and backoff delays.
	LogKeyDelay = "delay"
	// LogKeyBucket is used to log a S3 bucket name.
	LogKeyBucket = "bucket"
	// LogKeyObjectKey is used to log a S3 object key.
	LogKeyObjectKey = "object_key"
	// LogKeyComponent is used to communicate what application subcomponent
	// the log entry is from.
	LogKeyComponent = "component"
	// LogKeyCount is used to communicate a count.
	LogKeyCount = "count"
	// LogKeyEventID is the ID of an event.
	LogKeyEventID = "event_id"
	// LogKeyEventType is the type of an event.
	LogKeyEventType = "event_type"
	// LogKeyJobLock is the name of a job lock.
	LogKeyJobLock = "job_lock"
	// LogKeyJobLockID is the ID of a job lock.
	LogKeyJobLockID = "job_lock_id"
	// LogKeyState is the name of a state, like "held", "lost" or "accepted".
	LogKeyState = "state"
	// LogKeyIndex is the name of a search index, like an Open Search index.
	LogKeyIndex = "index"
	// LogKeyRoute is used to name a route or path.
	LogKeyRoute = "route"
	// LogKeyService is used to specify an RPC service.
	LogKeyService = "service"
	// LogKeyMethod is used to specify an RPC method.
	LogKeyMethod = "method"
	// LogKeySubject is the sub of an authenticated client.
	LogKeySubject = "sub"
	// LogKeyScopes are the scopes of the authenticated client.
	LogKeyScopes = "scopes"
	// LogKeyStatusCode is the HTTP status code used for a response.
	LogKeyStatusCode = "status_code"
)

Log attribute keys used throughout the application.

Variables

View Source
var ErrNoAuthorization = errors.New("no authorization provided")

Functions

func GetLogMetadata added in v0.5.0

func GetLogMetadata(ctx context.Context) map[string]any

func IsHTTPErrorWithStatus

func IsHTTPErrorWithStatus(err error, status int) bool

func IsTwirpErrorCode added in v0.4.0

func IsTwirpErrorCode(err error, code twirp.ErrorCode) bool

func ListenAndServeContext

func ListenAndServeContext(ctx context.Context, server *http.Server) error

func LoggingHooks added in v0.5.0

func LoggingHooks(
	logger *slog.Logger, scopesFunc func(context.Context) string,
) *twirp.ServerHooks

func NewTwirpMetricsHooks added in v0.4.0

func NewTwirpMetricsHooks(opts ...TwirpMetricOptionFunc) (*twirp.ServerHooks, error)

NewTwirpMetricsHooks creates new twirp hooks enabling prometheus metrics.

func ResolveParameter

func ResolveParameter(
	ctx context.Context, c *cli.Context, src ParameterSource, name string,
) (string, error)

func SetAuthInfo added in v0.6.0

func SetAuthInfo(ctx context.Context, info *AuthInfo) context.Context

func SetLogMetadata added in v0.5.0

func SetLogMetadata(ctx context.Context, key string, value any)

func SetUpLogger

func SetUpLogger(logLevel string, w io.Writer) *slog.Logger

SetUpLogger creates a default JSON logger and sets it as the global logger.

func UnmarshalFile

func UnmarshalFile(path string, o interface{}) error

func UnmarshalHTTPResource

func UnmarshalHTTPResource(resURL string, o interface{}) error

func WithLogMetadata added in v0.5.0

func WithLogMetadata(ctx context.Context) context.Context

Types

type AuthInfo added in v0.6.0

type AuthInfo struct {
	Claims JWTClaims
}

func AuthInfoFromHeader added in v0.6.0

func AuthInfoFromHeader(key *ecdsa.PublicKey, authorization string) (*AuthInfo, error)

func GetAuthInfo added in v0.6.0

func GetAuthInfo(ctx context.Context) (*AuthInfo, bool)

type GracefulShutdown added in v0.4.0

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

func NewGracefulShutdown added in v0.4.0

func NewGracefulShutdown(logger *slog.Logger, timeout time.Duration) *GracefulShutdown

func (*GracefulShutdown) CancelOnQuit added in v0.4.0

func (gs *GracefulShutdown) CancelOnQuit(ctx context.Context) context.Context

func (*GracefulShutdown) CancelOnStop added in v0.4.0

func (gs *GracefulShutdown) CancelOnStop(ctx context.Context) context.Context

func (*GracefulShutdown) ShouldQuit added in v0.4.0

func (gs *GracefulShutdown) ShouldQuit() <-chan struct{}

func (*GracefulShutdown) ShouldStop added in v0.4.0

func (gs *GracefulShutdown) ShouldStop() <-chan struct{}

func (*GracefulShutdown) Stop added in v0.4.0

func (gs *GracefulShutdown) Stop()

type HTTPClientInstrumentation added in v0.4.0

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

func NewHTTPClientIntrumentation added in v0.4.0

func NewHTTPClientIntrumentation(
	registerer prometheus.Registerer,
) (*HTTPClientInstrumentation, error)

func (*HTTPClientInstrumentation) Client added in v0.4.0

func (ci *HTTPClientInstrumentation) Client(name string, client *http.Client) error

type HTTPError

type HTTPError struct {
	Status     string
	StatusCode int
	Header     http.Header
	Body       io.Reader
}

func HTTPErrorFromResponse

func HTTPErrorFromResponse(res *http.Response) *HTTPError

func HTTPErrorf

func HTTPErrorf(statusCode int, format string, a ...any) *HTTPError

func NewHTTPError

func NewHTTPError(statusCode int, message string) *HTTPError

func (*HTTPError) Error

func (e *HTTPError) Error() string

type HealthServer

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

HealthServer exposes health endpoints, metrics, and PPROF endpoints.

func NewHealthServer

func NewHealthServer(addr string) *HealthServer

func (*HealthServer) AddReadyFunction

func (s *HealthServer) AddReadyFunction(name string, fn ReadyFunc)

func (*HealthServer) Close

func (s *HealthServer) Close() error

func (*HealthServer) ListenAndServe

func (s *HealthServer) ListenAndServe(ctx context.Context) error

type JWTClaims added in v0.6.0

type JWTClaims struct {
	jwt.RegisteredClaims

	Name  string   `json:"sub_name"`
	Scope string   `json:"scope"`
	Units []string `json:"units,omitempty"`
}

func (JWTClaims) HasAnyScope added in v0.6.0

func (c JWTClaims) HasAnyScope(names ...string) bool

func (JWTClaims) HasScope added in v0.6.0

func (c JWTClaims) HasScope(name string) bool

func (JWTClaims) Valid added in v0.6.0

func (c JWTClaims) Valid() error

type LazySSM

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

func NewLazySSM

func NewLazySSM() *LazySSM

func (*LazySSM) GetParameterValue

func (l *LazySSM) GetParameterValue(ctx context.Context, name string) (string, error)

type ParameterSource

type ParameterSource interface {
	GetParameterValue(ctx context.Context, name string) (string, error)
}

func GetParameterSource

func GetParameterSource(name string) (ParameterSource, error)

type ReadyFunc

type ReadyFunc func(ctx context.Context) error

type ReadyResult

type ReadyResult struct {
	Ok    bool   `json:"ok"`
	Error string `json:"error,omitempty"`
}

type TwirpMetricOptionFunc added in v0.4.0

type TwirpMetricOptionFunc func(opts *TwirpMetricsOptions)

func WithTwirpMetricsRegisterer added in v0.4.0

func WithTwirpMetricsRegisterer(reg prometheus.Registerer) TwirpMetricOptionFunc

WithTwirpMetricsRegisterer uses a custom registerer for Twirp metrics.

func WithTwirpMetricsStaticTestLatency added in v0.4.0

func WithTwirpMetricsStaticTestLatency(latency time.Duration) TwirpMetricOptionFunc

WithTwirpMetricsStaticTestLatency configures the RPC metrics to report a static duration.

type TwirpMetricsOptions added in v0.4.0

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

Directories

Path Synopsis
pg

Jump to

Keyboard shortcuts

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