mirror of
https://github.com/hanzoai/o11y-foundry.git
synced 2026-10-30 18:26:45 +00:00
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package segmentledger
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"runtime"
|
|
|
|
"github.com/hanzoai/o11y-foundry/internal/domain"
|
|
"github.com/hanzoai/o11y-foundry/internal/ledger"
|
|
"github.com/hanzoai/o11y-foundry/internal/ledger/noopledger"
|
|
"github.com/hanzoai/o11y-foundry/internal/version"
|
|
segment "github.com/segmentio/analytics-go/v3"
|
|
)
|
|
|
|
// provider implements ledger.Ledger using Segment.
|
|
type provider struct {
|
|
client segment.Client
|
|
}
|
|
|
|
// New creates a new Segment ledger provider.
|
|
// Returns a noop provider if the write key is not set.
|
|
func New(config ledger.Config) ledger.Ledger {
|
|
if config.Segment.Key == "" || config.Segment.Key == "<unset>" {
|
|
return noopledger.New()
|
|
}
|
|
|
|
client, err := segment.NewWithConfig(config.Segment.Key, segment.Config{})
|
|
if err != nil {
|
|
return noopledger.New()
|
|
}
|
|
|
|
return &provider{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
func (p *provider) Track(_ context.Context, event domain.Event, properties domain.Properties) {
|
|
properties = properties.
|
|
Set("os", runtime.GOOS).
|
|
Set("arch", runtime.GOARCH).
|
|
Set("foundry_version", version.Info.Version())
|
|
|
|
props := segment.NewProperties()
|
|
for k, v := range properties.Map() {
|
|
props.Set(k, v)
|
|
}
|
|
|
|
_ = p.client.Enqueue(segment.Track{
|
|
AnonymousId: domain.MustNewDistinctID().String(),
|
|
Event: fmt.Sprintf("foundryctl: %s", event.String()),
|
|
Properties: props,
|
|
})
|
|
}
|
|
|
|
func (p *provider) Close() error {
|
|
return p.client.Close()
|
|
}
|