package correlation import ( "context" "encoding/hex" ) const Key string = "correlation_id" var Empty CorrelationID = CorrelationID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // Correlation ID is a byte array of length 16 type CorrelationID []byte func (ci CorrelationID) String() string { if ci == nil { return hex.EncodeToString(Empty) } return hex.EncodeToString(ci) } func FromContext(ctx context.Context) CorrelationID { if v := ctx.Value("correlation_id"); v != nil { if c, ok := v.(CorrelationID); ok { return c } } return nil } type Sequencer interface { Next() []byte } type Generator struct { seq Sequencer } func NewGenerator(seq Sequencer) *Generator { return &Generator{ seq: seq, } } func (g *Generator) Next() CorrelationID { return CorrelationID(g.seq.Next()) }