analyticscfg

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package analyticscfg selects and builds an analytics.EventReporter from configuration — Segment, PostHog, or the noop reporter — handing the vendor implementations a circuit breaker built from the same config.

It also carries ProxySources, per-source configuration keyed by a free-form source name, which is what the multisource reporter fans out over. The set of sources belongs to the application rather than to this module, so it is a map rather than a field per source: adding one is a deployment's business and not a change to an exported struct here.

Index

Constants

View Source
const (
	// ProviderSegment represents Segment.
	ProviderSegment = "segment"
	// ProviderPostHog represents PostHog.
	ProviderPostHog = "posthog"
	// ProviderNoop discards every event. It must be selected deliberately — an
	// unset or typo'd provider is an error, because analytics that silently stop
	// being recorded are only noticed when someone asks a question of the data
	// months later.
	ProviderNoop = "noop"
)

Variables

This section is empty.

Functions

func NewEventReporter

func NewEventReporter(ctx context.Context, cfg *Config, opts ...Option) (analytics.EventReporter, error)

NewEventReporter provides an analytics.EventReporter from a config.

func RegisterEventReporter

func RegisterEventReporter(i do.Injector)

RegisterEventReporter registers an analytics.EventReporter with the injector.

Types

type Config

type Config struct {
	ProxySources ProxySourcesConfig `envPrefix:"PROXY_SOURCES_" json:"proxySources,omitempty" yaml:"proxySources,omitempty"`
	SourceConfig
}

Config is the configuration structure.

func (*Config) EnsureDefaults

func (cfg *Config) EnsureDefaults()

EnsureDefaults sets sensible defaults for zero-valued fields.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates a Config struct.

type Option

type Option func(*options)

Option configures how NewEventReporter assembles its reporter.

The observability dependencies are options rather than parameters because every one of them is genuinely optional: an absent logger logs nowhere, an absent tracer provider traces nowhere, and an absent metrics provider records nothing. Requiring them positionally made a caller that wanted none of the three name all three anyway, usually as noops.

func WithLogger

func WithLogger(logger logging.Logger) Option

WithLogger attaches a logger. An absent logger logs nowhere.

func WithMetricsProvider

func WithMetricsProvider(metricsProvider metrics.Provider) Option

WithMetricsProvider attaches a metrics provider. An absent provider records nothing.

func WithPillars

func WithPillars(p *observability.Pillars) Option

WithPillars attaches a logger, tracer provider, and metrics provider in one go, for the common case where a caller has already built them together. A nil Pillars attaches nothing.

It is applied in order with the individual options, so a caller can hand over its pillars and then override one of them.

func WithTracerProvider

func WithTracerProvider(tracerProvider tracing.Provider) Option

WithTracerProvider attaches a tracer provider, enabling spans on the instrumented operations. An absent tracer provider traces nowhere.

type ProxySourcesConfig

type ProxySourcesConfig map[string]*SourceConfig

ProxySourcesConfig holds per-source analytics config for the analytics proxy gRPC service, keyed by source name.

It is a map rather than a struct with a field per source: the set of sources belongs to the application, not to this module, and every source an application adds would otherwise be a breaking change to an exported struct here. Source names are free-form; "ios" and "web" are conventional, not special.

Environment parsing populates it from PROXY_SOURCES_<NAME>_* keys.

func (ProxySourcesConfig) ToMap

func (p ProxySourcesConfig) ToMap() map[string]*SourceConfig

ToMap returns the configured sources keyed by name, skipping nil entries. It is what the multisource reporter consumes.

type SourceConfig

type SourceConfig struct {
	Segment        *segment.Config           `env:",init"                  envPrefix:"SEGMENT_"           json:"segment,omitempty"        yaml:"segment,omitempty"`
	Posthog        *posthog.Config           `env:",init"                  envPrefix:"POSTHOG_"           json:"posthog,omitempty"        yaml:"posthog,omitempty"`
	Provider       string                    `env:"PROVIDER"               json:"provider,omitempty"      yaml:"provider,omitempty"`
	CircuitBreaker circuitbreakingcfg.Config `envPrefix:"CIRCUIT_BREAKER_" json:"circuitBreaker,omitzero" yaml:"circuitBreaker,omitempty"`
}

SourceConfig is the per-source analytics config (provider + credentials). Used for proxy sources; no ProxySources to avoid recursion.

func (*SourceConfig) EnsureDefaults

func (cfg *SourceConfig) EnsureDefaults()

EnsureDefaults sets sensible defaults for zero-valued fields.

func (*SourceConfig) NewCollector

func (cfg *SourceConfig) NewCollector(
	ctx context.Context,
	opts ...Option,
) (analytics.EventReporter, error)

NewCollector provides a collector.

Each provider is built into a variable and returned only once its error is known to be nil. The provider constructors return their own concrete types, so returning one straight through would convert a nil *segment.EventReporter into a non-nil analytics.EventReporter on the error path, and a caller testing the result against nil would find a reporter that panics on first use.

func (*SourceConfig) ValidateWithContext

func (cfg *SourceConfig) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates a SourceConfig: the provider must be known and the matching credentials block present, so a proxy source with no provider/key can't pass validation and silently degrade to a noop at runtime.

The sub-config for a provider that was not selected is skipped rather than merely unguarded: ozzo validates any non-nil pointer to a Validatable once a field's rules have run, and `env:",init"` leaves every sub-config non-nil. A validation.When guard alone stops the Required rule and nothing else, so both providers' credentials were required at once and no source could load.

Jump to

Keyboard shortcuts

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