otelcli

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: Apache-2.0 Imports: 36 Imported by: 0

Documentation

Overview

Package otelcli implements the otel-cli subcommands and argument parsing with Cobra and implements functionality using otlpclient and otlpserver.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute(version string)

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once.

func FormatVersion added in v0.1.0

func FormatVersion(version, commit, date string) string

FormatVersion pretty-prints the global version, commit, and date values into a string to enable the --version flag. Public to be called from main.

func GetExitCode

func GetExitCode() int

GetExitCode() is a helper for Cobra to retrieve the exit code, mainly used by exec to make otel-cli return the child program's exit code.

func StartClient added in v0.4.0

func StartClient(ctx context.Context, config Config) (context.Context, otlpclient.OTLPClient)

StartClient uses the Config to setup and start either a gRPC or HTTP client, and returns the OTLPClient interface to them.

Types

type BgEnd

type BgEnd struct {
	Attributes map[string]string `json:"span_attributes" env:"OTEL_CLI_ATTRIBUTES"`
	StatusCode string            `json:"status_code"`
	StatusDesc string            `json:"status_description"`
}

BgEnd is an empty struct that can be sent to call End().

type BgSpan

type BgSpan struct {
	TraceID     string `json:"trace_id"`
	SpanID      string `json:"span_id"`
	Traceparent string `json:"traceparent"`
	Error       string `json:"error"`
	// contains filtered or unexported fields
}

BgSpan is what is returned to all RPC clients and its methods are exported.

func (BgSpan) AddEvent

func (bs BgSpan) AddEvent(bse *BgSpanEvent, reply *BgSpan) error

AddEvent takes a BgSpanEvent from the client and attaches an event to the span.

func (BgSpan) End

func (bs BgSpan) End(in *BgEnd, reply *BgSpan) error

End takes a BgEnd (empty) struct, replies with the usual trace info, then ends the span end exits the background process.

func (BgSpan) Wait

func (bs BgSpan) Wait(in, reply *struct{}) error

Wait is a no-op RPC for validating the background server is up and running.

type BgSpanEvent

type BgSpanEvent struct {
	Name       string `json:"name"`
	Timestamp  string `json:"timestamp"`
	Attributes map[string]string
}

BgSpanEvent is a span event that the client will send.

type Config

type Config struct {
	Endpoint       string            `json:"endpoint" env:"OTEL_EXPORTER_OTLP_ENDPOINT"`
	TracesEndpoint string            `json:"traces_endpoint" env:"OTEL_EXPORTER_OTLP_TRACES_ENDPOINT"`
	Protocol       string            `json:"protocol" env:"OTEL_EXPORTER_OTLP_PROTOCOL,OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"`
	Timeout        string            `json:"timeout" env:"OTEL_EXPORTER_OTLP_TIMEOUT,OTEL_EXPORTER_OTLP_TRACES_TIMEOUT"`
	Headers        map[string]string `json:"otlp_headers" env:"OTEL_EXPORTER_OTLP_HEADERS"` // TODO: needs json marshaler hook to mask tokens
	Insecure       bool              `json:"insecure" env:"OTEL_EXPORTER_OTLP_INSECURE"`
	Blocking       bool              `json:"otlp_blocking" env:"OTEL_EXPORTER_OTLP_BLOCKING"`

	TlsCACert     string `json:"tls_ca_cert" env:"OTEL_EXPORTER_OTLP_CERTIFICATE,OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE"`
	TlsClientKey  string `json:"tls_client_key" env:"OTEL_EXPORTER_OTLP_CLIENT_KEY,OTEL_EXPORTER_OTLP_TRACES_CLIENT_KEY"`
	TlsClientCert string `json:"tls_client_cert" env:"OTEL_EXPORTER_OTLP_CLIENT_CERTIFICATE,OTEL_EXPORTER_OTLP_TRACES_CLIENT_CERTIFICATE"`
	// OTEL_CLI_NO_TLS_VERIFY is deprecated and will be removed for 1.0
	TlsNoVerify bool `json:"tls_no_verify" env:"OTEL_CLI_TLS_NO_VERIFY,OTEL_CLI_NO_TLS_VERIFY"`

	ServiceName       string            `json:"service_name" env:"OTEL_CLI_SERVICE_NAME,OTEL_SERVICE_NAME"`
	SpanName          string            `json:"span_name" env:"OTEL_CLI_SPAN_NAME"`
	Kind              string            `json:"span_kind" env:"OTEL_CLI_TRACE_KIND"`
	Attributes        map[string]string `json:"span_attributes" env:"OTEL_CLI_ATTRIBUTES"`
	StatusCode        string            `json:"span_status_code" env:"OTEL_CLI_STATUS_CODE"`
	StatusDescription string            `json:"span_status_description" env:"OTEL_CLI_STATUS_DESCRIPTION"`
	ForceSpanId       string            `json:"force_span_id" env:"OTEL_CLI_FORCE_SPAN_ID"`
	ForceParentSpanId string            `json:"force_parent_span_id" env:"OTEL_CLI_FORCE_PARENT_SPAN_ID"`
	ForceTraceId      string            `json:"force_trace_id" env:"OTEL_CLI_FORCE_TRACE_ID"`

	TraceparentCarrierFile string `json:"traceparent_carrier_file" env:"OTEL_CLI_CARRIER_FILE"`
	TraceparentIgnoreEnv   bool   `json:"traceparent_ignore_env" env:"OTEL_CLI_IGNORE_ENV"`
	TraceparentPrint       bool   `json:"traceparent_print" env:"OTEL_CLI_PRINT_TRACEPARENT"`
	TraceparentPrintExport bool   `json:"traceparent_print_export" env:"OTEL_CLI_EXPORT_TRACEPARENT"`
	TraceparentRequired    bool   `json:"traceparent_required" env:"OTEL_CLI_TRACEPARENT_REQUIRED"`

	BackgroundParentPollMs       int    `json:"background_parent_poll_ms" env:""`
	BackgroundSockdir            string `json:"background_socket_directory" env:""`
	BackgroundWait               bool   `json:"background_wait" env:""`
	BackgroundSkipParentPidCheck bool   `json:"background_skip_parent_pid_check"`

	ExecCommandTimeout  string `json:"exec_command_timeout" env:"OTEL_CLI_EXEC_CMD_TIMEOUT"`
	ExecTpDisableInject bool   `json:"exec_tp_disable_inject" env:"OTEL_CLI_EXEC_TP_DISABLE_INJECT"`

	StatusCanaryCount    int    `json:"status_canary_count"`
	StatusCanaryInterval string `json:"status_canary_interval"`

	SpanStartTime string `json:"span_start_time" env:""`
	SpanEndTime   string `json:"span_end_time" env:""`
	EventName     string `json:"event_name" env:""`
	EventTime     string `json:"event_time" env:""`

	CfgFile string `json:"config_file" env:"OTEL_CLI_CONFIG_FILE"`
	Verbose bool   `json:"verbose" env:"OTEL_CLI_VERBOSE"`
	Fail    bool   `json:"fail" env:"OTEL_CLI_FAIL"`

	// not exported, used to get data from cobra to otlpclient internals
	Version string `json:"-"`
}

Config stores the runtime configuration for otel-cli. Data structure is public so that it can serialize to json easily.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with all defaults set.

func (Config) GetEndpoint added in v0.4.0

func (c Config) GetEndpoint() *url.URL

func (Config) GetHeaders added in v0.4.0

func (c Config) GetHeaders() map[string]string

GetHeaders returns the stringmap of configured headers.

func (Config) GetInsecure added in v0.4.0

func (c Config) GetInsecure() bool

GetInsecure returns true if the configuration expects a non-TLS connection.

func (Config) GetIsRecording added in v0.4.0

func (c Config) GetIsRecording() bool

GetIsRecording returns true if an endpoint is set and otel-cli expects to send real spans. Returns false if unconfigured and going to run inert.

func (Config) GetServiceName added in v0.4.0

func (c Config) GetServiceName() string

GetServiceName returns the configured OTel service name.

func (Config) GetTimeout added in v0.4.0

func (c Config) GetTimeout() time.Duration

GetTimeout returns the parsed --timeout value as a time.Duration.

func (Config) GetTlsConfig added in v0.4.0

func (config Config) GetTlsConfig() *tls.Config

TlsConfig evaluates otel-cli configuration and returns a tls.Config that can be used by grpc or https.

func (Config) GetVersion added in v0.4.0

func (c Config) GetVersion() string

Version returns the program version stored in the config.

func (*Config) LoadEnv added in v0.1.0

func (c *Config) LoadEnv(getenv func(string) string) error

LoadEnv loads environment variables into the config, overwriting current values. Environment variable to config key mapping is tagged on the Config struct. Multiple names for envvars is supported, comma-separated. Takes a func(string)string that's usually os.Getenv, and is swappable to make testing easier.

func (*Config) LoadFile added in v0.1.0

func (c *Config) LoadFile() error

LoadFile reads the file specified by -c/--config and overwrites the current config values with any found in the file.

func (Config) LoadTraceparent added in v0.4.0

func (c Config) LoadTraceparent() traceparent.Traceparent

LoadTraceparent follows otel-cli's loading rules, start with envvar then file. If both are set, the file will override env. When in non-recording mode, the previous traceparent will be returned if it's available, otherwise, a zero-valued traceparent is returned.

func (Config) NewProtobufSpan added in v0.4.0

func (c Config) NewProtobufSpan() *tracepb.Span

NewProtobufSpan creates a new span and populates it with information from the config struct.

func (Config) ParseCliTimeout added in v0.4.0

func (c Config) ParseCliTimeout() time.Duration

ParseCliTimeout parses the --timeout string value to a time.Duration.

func (Config) ParseEndpoint added in v0.4.0

func (config Config) ParseEndpoint() (*url.URL, string)

ParseEndpoint takes the endpoint or signal endpoint, augments as needed (e.g. bare host:port for gRPC) and then parses as a URL. https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp

func (Config) ParseExecCommandTimeout added in v0.4.1

func (c Config) ParseExecCommandTimeout() time.Duration

ParseExecCommandTimeout parses the --command-timeout string value to a time.Duration. When timeout is unspecified or 0, otel-cli will wait forever for the command to complete.

func (Config) ParseSpanEndTime added in v0.4.0

func (c Config) ParseSpanEndTime() time.Time

ParseSpanEndTime returns config.SpanEndTime as time.Time.

func (Config) ParseSpanStartTime added in v0.4.0

func (c Config) ParseSpanStartTime() time.Time

ParseSpanStartTime returns config.SpanStartTime as time.Time.

func (Config) ParseStatusCanaryInterval added in v0.4.0

func (c Config) ParseStatusCanaryInterval() time.Duration

ParseStatusCanaryInterval parses the --canary-interval string value to a time.Duration.

func (Config) ParsedEventTime added in v0.4.0

func (c Config) ParsedEventTime() time.Time

ParsedEventTime returns config.EventTime as time.Time.

func (Config) PropagateTraceparent added in v0.4.0

func (c Config) PropagateTraceparent(span *tracepb.Span, target io.Writer)

PropagateTraceparent saves the traceparent to file if necessary, then prints span info to the console according to command-line args.

func (Config) SoftFail added in v0.4.0

func (c Config) SoftFail(format string, a ...interface{})

SoftFail calls through to softLog (which logs only if otel-cli was run with the --verbose flag), then immediately exits - with status -1 by default, or 1 if --fail was set (a la `curl --fail`)

func (Config) SoftFailIfErr added in v0.4.0

func (c Config) SoftFailIfErr(err error)

SoftFailIfErr calls SoftFail only if err != nil. Written as an interim step to pushing errors up the stack instead of calling SoftLog/SoftFail directly in methods that don't need a config handle.

func (Config) SoftLog added in v0.4.0

func (c Config) SoftLog(format string, a ...interface{})

SoftLog only calls through to log if otel-cli was run with the --verbose flag. TODO: does it make any sense to support %w? probably yes, can clean up some diagnostics.Error touch points.

func (Config) SoftLogIfErr added in v0.4.0

func (c Config) SoftLogIfErr(err error)

SoftLogIfErr calls SoftLog only if err != nil. Written as an interim step to pushing errors up the stack instead of calling SoftLog/SoftFail directly in methods that don't need a config handle.

func (Config) ToStringMap

func (c Config) ToStringMap() map[string]string

ToStringMap flattens the configuration into a stringmap that is easy to work with in tests especially with cmp.Diff. See test_main.go.

func (Config) WithAttributes

func (c Config) WithAttributes(with map[string]string) Config

WithAttributes returns the config with Attributes set to the provided value.

func (Config) WithBackgroundParentPollMs

func (c Config) WithBackgroundParentPollMs(with int) Config

WithBackgroundParentPollMs returns the config with BackgroundParentPollMs set to the provided value.

func (Config) WithBackgroundSkipParentPidCheck added in v0.2.0

func (c Config) WithBackgroundSkipParentPidCheck(with bool) Config

WithBackgroundSkipParentPidCheck returns the config with BackgroundSkipParentPidCheck set to the provided value.

func (Config) WithBackgroundSockdir

func (c Config) WithBackgroundSockdir(with string) Config

WithBackgroundSockdir returns the config with BackgroundSockdir set to the provided value.

func (Config) WithBackgroundWait

func (c Config) WithBackgroundWait(with bool) Config

WithBackgroundWait returns the config with BackgroundWait set to the provided value.

func (Config) WithBlocking

func (c Config) WithBlocking(with bool) Config

WithBlocking returns the config with Blocking set to the provided value.

func (Config) WithCfgFile

func (c Config) WithCfgFile(with string) Config

WithCfgFile returns the config with CfgFile set to the provided value.

func (Config) WithEndpoint

func (c Config) WithEndpoint(with string) Config

WithEndpoint returns the config with Endpoint set to the provided value.

func (Config) WithEventName

func (c Config) WithEventName(with string) Config

WithEventName returns the config with EventName set to the provided value.

func (Config) WithEventTime

func (c Config) WithEventTime(with string) Config

WithEventTIme returns the config with EventTIme set to the provided value.

func (Config) WithFail added in v0.1.0

func (c Config) WithFail(with bool) Config

WithFail returns the config with Fail set to the provided value.

func (Config) WithHeaders

func (c Config) WithHeaders(with map[string]string) Config

WithHeades returns the config with Heades set to the provided value.

func (Config) WithInsecure

func (c Config) WithInsecure(with bool) Config

WithInsecure returns the config with Insecure set to the provided value.

func (Config) WithKind

func (c Config) WithKind(with string) Config

WithKind returns the config with Kind set to the provided value.

func (Config) WithProtocol added in v0.1.0

func (c Config) WithProtocol(with string) Config

WithProtocol returns the config with protocol set to the provided value.

func (Config) WithServiceName

func (c Config) WithServiceName(with string) Config

WithServiceName returns the config with ServiceName set to the provided value.

func (Config) WithSpanEndTime

func (c Config) WithSpanEndTime(with string) Config

WithSpanEndTime returns the config with SpanEndTime set to the provided value.

func (Config) WithSpanName

func (c Config) WithSpanName(with string) Config

WithSpanName returns the config with SpanName set to the provided value.

func (Config) WithSpanStartTime

func (c Config) WithSpanStartTime(with string) Config

WithSpanStartTime returns the config with SpanStartTime set to the provided value.

func (Config) WithStatusCanaryCount added in v0.4.0

func (c Config) WithStatusCanaryCount(with int) Config

WithStatusCanaryCount returns the config with StatusCanaryCount set to the provided value.

func (Config) WithStatusCanaryInterval added in v0.4.0

func (c Config) WithStatusCanaryInterval(with string) Config

WithStatusCanaryInterval returns the config with StatusCanaryInterval set to the provided value.

func (Config) WithStatusCode added in v0.1.0

func (c Config) WithStatusCode(with string) Config

WithStatusCode returns the config with StatusCode set to the provided value.

func (Config) WithStatusDescription added in v0.1.0

func (c Config) WithStatusDescription(with string) Config

WithStatusDescription returns the config with StatusDescription set to the provided value.

func (Config) WithTimeout

func (c Config) WithTimeout(with string) Config

WithTimeout returns the config with Timeout set to the provided value.

func (Config) WithTlsCACert added in v0.2.0

func (c Config) WithTlsCACert(with string) Config

WithTlsCACert returns the config with TlsCACert set to the provided value.

func (Config) WithTlsClientCert added in v0.2.0

func (c Config) WithTlsClientCert(with string) Config

WithTlsClientCert returns the config with NoTlsClientCert set to the provided value.

func (Config) WithTlsClientKey added in v0.2.0

func (c Config) WithTlsClientKey(with string) Config

WithTlsClientKey returns the config with NoTlsClientKey set to the provided value.

func (Config) WithTlsNoVerify added in v0.2.0

func (c Config) WithTlsNoVerify(with bool) Config

WithTlsNoVerify returns the config with NoTlsVerify set to the provided value.

func (Config) WithTraceparentCarrierFile

func (c Config) WithTraceparentCarrierFile(with string) Config

WithTraceparentCarrierFile returns the config with TraceparentCarrierFile set to the provided value.

func (Config) WithTraceparentIgnoreEnv

func (c Config) WithTraceparentIgnoreEnv(with bool) Config

WithTraceparentIgnoreEnv returns the config with TraceparentIgnoreEnv set to the provided value.

func (Config) WithTraceparentPrint

func (c Config) WithTraceparentPrint(with bool) Config

WithTraceparentPrint returns the config with TraceparentPrint set to the provided value.

func (Config) WithTraceparentPrintExport

func (c Config) WithTraceparentPrintExport(with bool) Config

WithTraceparentPrintExport returns the config with TraceparentPrintExport set to the provided value.

func (Config) WithTraceparentRequired

func (c Config) WithTraceparentRequired(with bool) Config

WithTraceparentRequired returns the config with TraceparentRequired set to the provided value.

func (Config) WithTracesEndpoint added in v0.3.0

func (c Config) WithTracesEndpoint(with string) Config

WithTracesEndpoint returns the config with TracesEndpoint set to the provided value.

func (Config) WithVerbose

func (c Config) WithVerbose(with bool) Config

WithVerbose returns the config with Verbose set to the provided value.

func (Config) WithVersion added in v0.4.0

func (c Config) WithVersion(with string) Config

WithVersion returns the config with Version set to the provided value.

type Diagnostics

type Diagnostics struct {
	CliArgs            []string `json:"cli_args"`
	IsRecording        bool     `json:"is_recording"`
	ConfigFileLoaded   bool     `json:"config_file_loaded"`
	NumArgs            int      `json:"number_of_args"`
	DetectedLocalhost  bool     `json:"detected_localhost"`
	InsecureSkipVerify bool     `json:"insecure_skip_verify"`
	ParsedTimeoutMs    int64    `json:"parsed_timeout_ms"`
	Endpoint           string   `json:"endpoint"` // the computed endpoint, not the raw config val
	EndpointSource     string   `json:"endpoint_source"`
	Error              string   `json:"error"`
	ExecExitCode       int      `json:"exec_exit_code"`
	Retries            int      `json:"retries"`
}

Diagnostics is a place to put things that are useful for testing and diagnosing issues with otel-cli. The only user-facing feature that should be using these is otel-cli status.

var Diag Diagnostics

package global Diagnostics handle, written to from all over otel-cli and used in e.g. otel-cli status to show internal state

func (*Diagnostics) SetError added in v0.4.0

func (d *Diagnostics) SetError(err error) error

SetError sets the diagnostics Error to the error's string if it's not nil and returns the same error so it can be inlined in return.

func (*Diagnostics) ToStringMap

func (d *Diagnostics) ToStringMap() map[string]string

ToMap returns the Diag struct as a string map for testing.

type SpanEventUnion added in v0.4.0

type SpanEventUnion struct {
	Span  *tracepb.Span
	Event *tracepb.Span_Event
}

SpanEventUnion is for server_tui so it can sort spans and events together by timestamp.

func (*SpanEventUnion) IsSpan added in v0.4.0

func (seu *SpanEventUnion) IsSpan() bool

IsSpan returns true if this union is for an event. Span is always populated but Event is only populated for events.

func (*SpanEventUnion) SpanIdString added in v0.4.0

func (seu *SpanEventUnion) SpanIdString() string

func (*SpanEventUnion) TraceIdString added in v0.4.0

func (seu *SpanEventUnion) TraceIdString() string

func (*SpanEventUnion) UnixNanos added in v0.4.0

func (seu *SpanEventUnion) UnixNanos() uint64

type SpanEventUnionList added in v0.4.0

type SpanEventUnionList []SpanEventUnion

SpanEventUnionList is a sortable list of SpanEventUnion, sorted on timestamp.

func (SpanEventUnionList) Len added in v0.4.0

func (sl SpanEventUnionList) Len() int

func (SpanEventUnionList) Less added in v0.4.0

func (sl SpanEventUnionList) Less(i, j int) bool

func (SpanEventUnionList) Swap added in v0.4.0

func (sl SpanEventUnionList) Swap(i, j int)

type StatusOutput

type StatusOutput struct {
	Config      Config               `json:"config"`
	Spans       []map[string]string  `json:"spans"`
	SpanData    map[string]string    `json:"span_data"`
	Env         map[string]string    `json:"env"`
	Diagnostics Diagnostics          `json:"diagnostics"`
	Errors      otlpclient.ErrorList `json:"errors"`
}

StatusOutput captures all the data we want to print out for this subcommand and is also used in ../main_test.go for automated testing.

Jump to

Keyboard shortcuts

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