package archied import ( "context" "fmt" "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats" "github.com/samcharles93/archie-core/internal/infrastructure/spanrelay" "github.com/samcharles93/archie-core/internal/infrastructure/telemetry" ) // startSpanRelay forwards the spans task containers publish to the collector. // It does nothing while telemetry is off, and the containers then export // nothing. func (b *boot) startSpanRelay(ctx context.Context, url, token string) error { if b.cfg.Telemetry.Endpoint == "" { return nil } maxAge := spanrelay.MaxAge spans, err := nats.Connect(ctx, nats.Config{ URL: url, Token: token, StreamName: spanrelay.StreamName, ConsumerName: spanrelay.ConsumerName, Subjects: []string{spanrelay.SubjectWildcard}, FilterSubject: spanrelay.SubjectWildcard, Retention: nats.FanOutRetention(), MaxAge: &maxAge, }, b.log) if err != nil { return fmt.Errorf("nats span stream connect: %w", err) } upstream, err := telemetry.NewClient(b.cfg.Telemetry) if err != nil { spans.Close() return err } relayCtx, cancel := context.WithCancel(ctx) done := make(chan struct{}) go func() { defer close(done) spanrelay.Relay(relayCtx, spans, upstream, b.log) }() b.addCleanup(func() { cancel() <-done spans.Close() }) return nil }