segment

package
v10.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package segment reports analytics events to Segment.

Choosing it commits a deployment to a Segment write key and to Segment's own hosted endpoint: Config carries the token and nothing else, so unlike the posthog sibling there is no host to point at a different region, and no option reaches the underlying segment.Config to add one.

What a caller gets in exchange is fan-out. Every Track and Identify goes out with all integrations enabled, so an event published here reaches every destination the Segment workspace has turned on, and adding a destination is a change made in Segment rather than in this deployment.

A nil error means buffered, not delivered

AddUser, EventOccurred, and EventOccurredAnonymous append to the client's in-memory buffer and return. A background flush delivers later, and the only report of a delivery that failed is the error counter and the log line this package records from the client's Failure callback — which names the message and its ID, and omits its properties and traits, since those are the caller's data rather than this service's.

Close is therefore the final flush, and its error is the last chance anyone has to learn that buffered events did not arrive. A process that exits without it drops whatever was still buffered.

The circuit breaker is a required constructor argument and is driven from those delivery callbacks rather than from the enqueue, so it reflects whether Segment is accepting events rather than whether the buffer accepted them. While it is open, every call returns circuitbreaking.ErrCircuitBroken without enqueueing. Nothing here retries.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyAPIToken indicates an empty API token was provided.
	ErrEmptyAPIToken = platformerrors.New("empty Segment API token")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	APIToken string `env:"API_TOKEN" json:"apiToken,omitempty" yaml:"apiToken,omitempty"`
}

func (*Config) ValidateWithContext

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

ValidateWithContext validates a Config struct.

type EventReporter

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

EventReporter is the Segment analytics.EventReporter implementation. It is exported, and returned by NewEventReporter, so a caller who has chosen Segment can depend on that choice rather than on the interface every reporter shares.

func NewEventReporter

func NewEventReporter(apiKey string, circuitBreaker circuitbreaking.CircuitBreaker, opts ...Option) (*EventReporter, error)

NewEventReporter returns a new Segment-backed EventReporter.

func (*EventReporter) AddUser

func (c *EventReporter) AddUser(ctx context.Context, userID string, properties map[string]any) error

AddUser upsert's a user's identity.

func (*EventReporter) Close

func (c *EventReporter) Close(ctx context.Context) error

Close wraps the internal client's Close method.

func (*EventReporter) EventOccurred

func (c *EventReporter) EventOccurred(ctx context.Context, event, userID string, properties map[string]any) error

EventOccurred associates events with a user.

func (*EventReporter) EventOccurredAnonymous

func (c *EventReporter) EventOccurredAnonymous(ctx context.Context, event, anonymousID string, properties map[string]any) error

EventOccurredAnonymous records an event for an anonymous user.

type Option

type Option func(*options)

Option configures the EventReporter this package constructs. The zero configuration works: an absent logger logs nowhere, an absent tracer provider traces nowhere, and an absent metrics provider records nothing.

func WithLogger

func WithLogger(logger logging.Logger) Option

WithLogger attaches a logger.

func WithMetricsProvider

func WithMetricsProvider(metricsProvider metrics.Provider) Option

WithMetricsProvider attaches a metrics provider.

func WithTracerProvider

func WithTracerProvider(tracerProvider tracing.Provider) Option

WithTracerProvider attaches a tracer provider, enabling spans on every event.

Jump to

Keyboard shortcuts

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