// defaultKSUIDs sets the ID to a new KSUID if non is set (recommended)
func defaultKSUIDs() client.EventDefaulter {
return func(ctx context.Context, evt event.Event) event.Event {
if evt.ID() == "" {
evt.Context = evt.Context.Clone()
kID := ksuid.New().String()
evt.SetID(kID)
}
return evt
}
}
// withKSUIDs allows the defaultKSUIDs to be used as a client.Option for
func withKSUIDs() client.Option {
return client.WithEventDefaulter(defaultKSUIDs())
}
// elsewhere
c, err := cloudevents.NewClient(sender,
cloudevents.WithTimeNow(),
withKSUIDs(),
)
Problem:
in my case the ID is empty, because it should be set by the defaulter
Is there a way I can either manually apply the defaulters to an event, or to make that partition key be lazy evaluated?
kcat -b localhost:9092 -C -t test-topic -f '%t %p @ %o: %s\n'
test-topic 0 @ 0: {"internal-id":0,"message":"Hello, World!"}
test-topic 0 @ 1: {"internal-id":4,"message":"Hello, World!"}
test-topic 0 @ 2: {"internal-id":5,"message":"Hello, World!"}
test-topic 0 @ 3: {"internal-id":7,"message":"Hello, World!"}
test-topic 0 @ 4: {"internal-id":8,"message":"Hello, World!"}
% Reached end of topic test-topic [0] at offset 5
% Reached end of topic test-topic [1] at offset 0
test-topic 2 @ 0: {"internal-id":9,"message":"Hello, World!"}
test-topic 3 @ 0: {"internal-id":1,"message":"Hello, World!"}
test-topic 3 @ 1: {"internal-id":2,"message":"Hello, World!"}
test-topic 3 @ 2: {"internal-id":3,"message":"Hello, World!"}
test-topic 3 @ 3: {"internal-id":6,"message":"Hello, World!"}
% Reached end of topic test-topic [2] at offset 1
% Reached end of topic test-topic [3] at offset 4
Also aside/nit : I'd recommend we change the sample message to not have an "id" field set because that can be confusing to new folks. Which ID is used?
Symptom: All my events are being produced to a single topic partition
Context:
Problem:
Is there a way I can either manually apply the defaulters to an event, or to make that partition key be lazy evaluated?
(also note how the sender example has to explicitly set the ID, contradictory with the
cloudevents.WithUUIDs()
defaulter set)Helper commands i used to debug:
Observe with
kafka-console-consumer
orkcat
Also aside/nit : I'd recommend we change the sample message to not have an
"id"
field set because that can be confusing to new folks. Which ID is used?