mirror of
https://github.com/hanzoai/o11y-foundry.git
synced 2026-10-30 18:26:45 +00:00
45 lines
943 B
Go
45 lines
943 B
Go
package domain
|
|
|
|
import (
|
|
"github.com/denisbrodbeck/machineid"
|
|
)
|
|
|
|
const unknownDistinctID = "unknown"
|
|
|
|
// DistinctID is an anonymous, stable identifier for telemetry attribution. It
|
|
// is at most 32 characters and never reveals the source from which it was
|
|
// derived; values longer than 32 characters are truncated by NewDistinctID.
|
|
type DistinctID struct {
|
|
value string
|
|
}
|
|
|
|
func NewDistinctID() (DistinctID, error) {
|
|
id, err := machineid.ProtectedID("foundryctl")
|
|
if err != nil {
|
|
return UnknownDistinctID(), nil
|
|
}
|
|
if len(id) > 32 {
|
|
id = id[:32]
|
|
}
|
|
|
|
return DistinctID{value: id}, nil
|
|
}
|
|
|
|
func MustNewDistinctID() DistinctID {
|
|
id, err := NewDistinctID()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return id
|
|
}
|
|
|
|
// UnknownDistinctID is the sentinel used when no stable raw identifier could be derived.
|
|
func UnknownDistinctID() DistinctID {
|
|
return DistinctID{value: unknownDistinctID}
|
|
}
|
|
|
|
func (d DistinctID) String() string {
|
|
return d.value
|
|
}
|