Documentation
¶
Overview ¶
Package telemetry provides a telemetry client that is thread-safe burden-less telemetry client following the specification of the instrumentation telemetry from Datadog. Specification here: https://github.com/DataDog/instrumentation-telemetry-api-docs/tree/main
The telemetry package has 6 main capabilities:
- Metrics: Support for Count, Rate, Gauge, Distribution metrics.
- Logs: Support Debug, Warn, Error logs with tags and stack traces via the subpackage log or the Log function.
- Product: Start, Stop and Startup errors reporting to the backend
- App Config: Register and change the configuration of the application and declare its origin
- Integration: Loading and errors
- Dependencies: Sending all the dependencies of the application to the backend (for SCA purposes for example)
Each of these capabilities is exposed through the Client interface but mainly through the package level functions. that mirror and call the global client that is started through the StartApp function.
Before the StartApp function is called, all called to the global client will be recorded and replay when the StartApp function is called synchronously. The telemetry client is allowed to record at most 512 calls.
At the end of the app lifetime. If [tracer.Stop] is called, the client should be stopped with the StopApp function. For all data to be flushed to the backend appropriately.
Note: No public API is available for the dependencies payloads as this is does in-house with the `ClientConfig.DependencyLoader` function output.
Index ¶
- func AddFlushTicker(ticker func(Client))
- func Disabled() bool
- func LoadIntegration(integration string)
- func Log(level LogLevel, text string, options ...LogOption)
- func MarkIntegrationAsLoaded(integration Integration)
- func MockClient(client Client) func()
- func ProductStartError(product Namespace, err error)
- func ProductStarted(product Namespace)
- func ProductStopped(product Namespace)
- func RegisterAppConfig(key string, value any, origin Origin)
- func RegisterAppConfigs(kvs ...Configuration)
- func SanitizeConfigValue(value any) any
- func StartApp(client Client)
- func StopApp()
- type Client
- type ClientConfig
- type Configuration
- type Integration
- type LogLevel
- type LogOption
- type MetricHandle
- type Namespace
- type Origin
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFlushTicker ¶
func AddFlushTicker(ticker func(Client))
AddFlushTicker adds a function that is called at each telemetry Flush. By default, every minute
func Disabled ¶
func Disabled() bool
Disabled returns whether instrumentation telemetry is disabled according to the DD_INSTRUMENTATION_TELEMETRY_ENABLED env var
func LoadIntegration ¶
func LoadIntegration(integration string)
LoadIntegration marks an integration as loaded in the telemetry client. If telemetry is disabled, it will do nothing. If the telemetry client has not started yet, it will record the action and replay it once the client is started.
func MarkIntegrationAsLoaded ¶
func MarkIntegrationAsLoaded(integration Integration)
MarkIntegrationAsLoaded marks an integration as loaded in the telemetry. If telemetry is disabled or the client has not started yet it will record the action and replay it once the client is started.
func MockClient ¶
func MockClient(client Client) func()
MockClient swaps the global client with the given client and clears the recorder to make sure external calls are not replayed. It returns a function that can be used to swap back the global client
func ProductStartError ¶
ProductStartError declares that a product could not start because of the following error. If telemetry is disabled, it will do nothing. If the telemetry client has not started yet, it will record the action and replay it once the client is started.
func ProductStarted ¶
func ProductStarted(product Namespace)
ProductStarted declares a product to have started at the customer’s request. If telemetry is disabled, it will do nothing. If the telemetry client has not started yet, it will record the action and replay it once the client is started.
func ProductStopped ¶
func ProductStopped(product Namespace)
ProductStopped declares a product to have being stopped by the customer. If telemetry is disabled, it will do nothing. If the telemetry client has not started yet, it will record the action and replay it once the client is started.
func RegisterAppConfig ¶
RegisterAppConfig adds a key value pair to the app configuration and send the change to telemetry value has to be json serializable and the origin is the source of the change. If telemetry is disabled, it will do nothing. If the telemetry client has not started yet, it will record the action and replay it once the client is started.
func RegisterAppConfigs ¶
func RegisterAppConfigs(kvs ...Configuration)
RegisterAppConfigs adds a list of key value pairs to the app configuration and sends the change to telemetry. Same as AddAppConfig but for multiple values. If telemetry is disabled, it will do nothing. If the telemetry client has not started yet, it will record the action and replay it once the client is started.
func SanitizeConfigValue ¶
SanitizeConfigValue sanitizes the value of a configuration key to ensure it can be marshalled.
Types ¶
type Client ¶
type Client interface { io.Closer // Count obtains the metric handle for the given parameters, or creates a new one if none was created just yet. // Tags cannot contain commas. Count(namespace Namespace, name string, tags []string) MetricHandle // Rate obtains the metric handle for the given parameters, or creates a new one if none was created just yet. // Tags cannot contain commas. Rate(namespace Namespace, name string, tags []string) MetricHandle // Gauge obtains the metric handle for the given parameters, or creates a new one if none was created just yet. // Tags cannot contain commas. Gauge(namespace Namespace, name string, tags []string) MetricHandle // Distribution obtains the metric handle for the given parameters, or creates a new one if none was created just yet. // Tags cannot contain commas. Distribution(namespace Namespace, name string, tags []string) MetricHandle // Log sends a telemetry log at the desired level with the given text and options. // Options include sending key-value pairs as tags, and a stack trace frozen from inside the Log function. Log(level LogLevel, text string, options ...LogOption) // ProductStarted declares a product to have started at the customer’s request ProductStarted(product Namespace) // ProductStopped declares a product to have being stopped by the customer ProductStopped(product Namespace) // ProductStartError declares that a product could not start because of the following error ProductStartError(product Namespace, err error) // RegisterAppConfig adds a key value pair to the app configuration and send the change to telemetry // value has to be json serializable and the origin is the source of the change. RegisterAppConfig(key string, value any, origin Origin) // RegisterAppConfigs adds a list of key value pairs to the app configuration and sends the change to telemetry. // Same as AddAppConfig but for multiple values. RegisterAppConfigs(kvs ...Configuration) // MarkIntegrationAsLoaded marks an integration as loaded in the telemetry MarkIntegrationAsLoaded(integration Integration) // Flush closes the client and flushes any remaining data. Flush() // AppStart sends the telemetry necessary to signal that the app is starting. // Preferred use via [StartApp] package level function AppStart() // AppStop sends the telemetry necessary to signal that the app is stopping. // Preferred use via [StopApp] package level function AppStop() // AddFlushTicker adds a function that is called at each telemetry Flush. By default, every minute AddFlushTicker(ticker func(Client)) }
Client constitutes all the functions available concurrently for the telemetry users. All methods are thread-safe This is an interface for easier testing but all functions will be mirrored at the package level to call the global client.
func NewClient ¶
func NewClient(service, env, version string, config ClientConfig) (Client, error)
NewClient creates a new telemetry client with the given service, environment, and version and config.
func SwapClient ¶
SwapClient swaps the global client with the given client and Flush the old (*client).
type ClientConfig ¶
type ClientConfig struct { // DependencyLoader determines how dependency data is sent via telemetry. // The default value is [debug.ReadBuildInfo] since Application Security Monitoring uses this data to detect vulnerabilities in the ASM-SCA product // To disable this feature, please implement a function that returns nil, false. // This can only be controlled via the env var DD_TELEMETRY_DEPENDENCY_COLLECTION_ENABLED DependencyLoader func() (*debug.BuildInfo, bool) // MetricsEnabled determines whether metrics are sent via telemetry. // If false, libraries should not send the generate-metrics or distributions events. // This can only be controlled via the env var DD_TELEMETRY_METRICS_ENABLED MetricsEnabled bool // LogsEnabled determines whether logs are sent via telemetry. // This can only be controlled via the env var DD_TELEMETRY_LOG_COLLECTION_ENABLED LogsEnabled bool // AgentlessURL is the full URL to the agentless telemetry endpoint. (optional) // Defaults to https://instrumentation-telemetry-intake.datadoghq.com/api/v2/apmtelemetry AgentlessURL string // AgentURL is the url of the agent to send telemetry to. (optional) // If the AgentURL is not set, the telemetry client will not attempt to connect to the agent before sending to the agentless endpoint. AgentURL string // HTTPClient is the http client to use for sending telemetry, defaults to a http.DefaultClient copy. HTTPClient *http.Client // HeartbeatInterval is the interval at which to send a heartbeat payload, defaults to 60s. // The maximum value is 60s. HeartbeatInterval time.Duration // ExtendedHeartbeatInterval is the interval at which to send an extended heartbeat payload, defaults to 24h. ExtendedHeartbeatInterval time.Duration // FlushInterval is the interval at which the client flushes the data. // By default, the client will start to Flush at 60s intervals and will reduce the interval based on the load till it hit 15s // Both values cannot be higher than 60s because the heartbeat need to be sent at least every 60s. Values will be clamped otherwise. FlushInterval internal.Range[time.Duration] // PayloadQueueSize is the size of the payload queue. Default range is [4, 32]. PayloadQueueSize internal.Range[int] // DistributionsSize is the size of the distribution queue. Default range is [2^8, 2^14]. DistributionsSize internal.Range[int] // Debug enables debug mode for the telemetry client and sent it to the backend so it logs the request. The // DD_TELEMETRY_DEBUG environment variable, when set to a truthy value, overrides this setting. Debug bool // APIKey is the API key to use for sending telemetry to the agentless endpoint. (using DD_API_KEY env var by default) APIKey string // EarlyFlushPayloadSize is the size of the payload that will trigger an early flush. // This is necessary because backend won't allow bodies larger than 5MB. // The default value here will be 2MB to take into account the large inaccuracy in estimating the size of bodies EarlyFlushPayloadSize int // MaxDistributionsSize is the maximum number of logs with distinct message, level and tags that can be stored per flush window. // If the limit is reached, logs will be dropped and a log will be sent to the backend about it // The default value is 1024. MaxDistinctLogs int32 // contains filtered or unexported fields }
type Configuration ¶
type Configuration struct { // Key is the key of the configuration. Name string // Value is the value of the configuration. Need to be json serializable. Value any // Origin is the source of the configuration change. Origin Origin }
Configuration is a key-value pair that is used to configure the application.
type Integration ¶
type Integration struct { // Name is an arbitrary string that must stay constant for the integration. Name string // Version is the version of the integration/dependency that is being loaded. Version string // Error is the error that occurred while loading the integration. If this field is specified, the integration is // considered to be having been forcefully disabled because of the error. Error string }
Integration is an integration that is configured to be traced.
type LogLevel ¶
const ( LogDebug LogLevel = transport.LogLevelDebug LogWarn LogLevel = transport.LogLevelWarn LogError LogLevel = transport.LogLevelError )
type LogOption ¶
type LogOption func(key *loggerKey, value *loggerValue)
LogOption is a function that modifies the log message that is sent to the telemetry.
func WithStacktrace ¶
func WithStacktrace() LogOption
WithStacktrace returns a LogOption that sets the stacktrace for the telemetry log message. The stacktrace is a string that is generated inside the WithStacktrace function. Logs demultiplication does not take the stacktrace into account. This means that a log that has been demultiplicated will only show of the first log.
type MetricHandle ¶
type MetricHandle interface { // Submit submits a value to the metric handle. Submit(value float64) // Get returns the last value submitted to the metric handle. Get() float64 }
MetricHandle can be used to submit different values for the same metric. MetricHandle is used to reduce lock contention when submitting metrics. This can also be used ephemerally to submit a single metric value like this:
telemetry.metric(telemetry.Appsec, "my-count", map[string]string{"tag1": "true", "tag2": "1.0"}).Submit(1.0)
func Count ¶
func Count(namespace Namespace, name string, tags []string) MetricHandle
Count creates a new metric handle for the given parameters that can be used to submit values. Count will always return a MetricHandle, even if telemetry is disabled or the client has yet to start. The MetricHandle is then swapped with the actual MetricHandle once the client is started.
func Distribution ¶
func Distribution(namespace Namespace, name string, tags []string) MetricHandle
Distribution creates a new metric handle for the given parameters that can be used to submit values. Distribution will always return a MetricHandle, even if telemetry is disabled or the client has yet to start. The MetricHandle is then swapped with the actual MetricHandle once the client is started. The Get() method of the MetricHandle will return the last value submitted. Distribution MetricHandle is advised to be held in a variable more than the rest of the metric types to avoid too many useless allocations.
func Gauge ¶
func Gauge(namespace Namespace, name string, tags []string) MetricHandle
Gauge creates a new metric handle for the given parameters that can be used to submit values. Gauge will always return a MetricHandle, even if telemetry is disabled or the client has yet to start. The MetricHandle is then swapped with the actual MetricHandle once the client is started.
func Rate ¶
func Rate(namespace Namespace, name string, tags []string) MetricHandle
Rate creates a new metric handle for the given parameters that can be used to submit values. Rate will always return a MetricHandle, even if telemetry is disabled or the client has yet to start. The MetricHandle is then swapped with the actual MetricHandle once the client is started.
type Namespace ¶
Namespace describes a product to distinguish telemetry coming from different products used by the same application
const ( NamespaceGeneral Namespace = transport.NamespaceGeneral NamespaceTracers Namespace = transport.NamespaceTracers NamespaceProfilers Namespace = transport.NamespaceProfilers NamespaceAppSec Namespace = transport.NamespaceAppSec NamespaceIAST Namespace = transport.NamespaceIAST NamespaceCIVisibility Namespace = transport.NamespaceCIVisibility NamespaceMLOps Namespace = transport.NamespaceMLOps NamespaceRUM Namespace = transport.NamespaceRUM )
type Origin ¶
const ( OriginDefault Origin = transport.OriginDefault OriginCode Origin = transport.OriginCode OriginDDConfig Origin = transport.OriginDDConfig OriginEnvVar Origin = transport.OriginEnvVar OriginRemoteConfig Origin = transport.OriginRemoteConfig OriginLocalStableConfig Origin = transport.OriginLocalStableConfig OriginManagedStableConfig Origin = transport.OriginManagedStableConfig )
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package telemetrytest provides a mock implementation of the telemetry client for testing purposes
|
Package telemetrytest provides a mock implementation of the telemetry client for testing purposes |