diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b092bc..9da72f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## v0.0.55 Allow setting of Extensions +- Introduces `ApplyCloudEventOptions` for applying Options to CloudEvents + ## v0.0.51 Allow passing of Test Clients - Introduces `FakeSetup`, which allows for passing TestClients along. diff --git a/go.mod b/go.mod index 4cade78..5199998 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.21.0 github.com/GoogleCloudPlatform/opentelemetry-operations-go/propagator v0.45.0 github.com/cloudevents/sdk-go/protocol/pubsub/v2 v2.14.0 + github.com/cloudevents/sdk-go/v2 v2.14.0 github.com/google/go-cmp v0.6.0 go.opentelemetry.io/otel v1.22.0 go.opentelemetry.io/otel/sdk v1.22.0 @@ -37,7 +38,6 @@ require ( github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 // indirect - github.com/cloudevents/sdk-go/v2 v2.14.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/emicklei/go-restful v2.16.0+incompatible // indirect github.com/evanphx/json-patch v4.12.0+incompatible // indirect diff --git a/pkg/publisher/publisher.go b/pkg/publisher/publisher.go index 711b35d..e0563d2 100644 --- a/pkg/publisher/publisher.go +++ b/pkg/publisher/publisher.go @@ -6,15 +6,23 @@ import ( "cloud.google.com/go/errorreporting" cepubsub "github.com/cloudevents/sdk-go/protocol/pubsub/v2" + "github.com/cloudevents/sdk-go/v2/event" "github.com/otto-de/sherlock-microservice/pkg/gcp/errorreports" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" ) type Option struct { + extensions map[string]any orderingKey string } +func WithExtensions(extensions map[string]any) Option { + return Option{ + extensions: extensions, + } +} + func WithOrderingKey(orderingKey string) Option { return Option{ orderingKey: orderingKey, @@ -44,6 +52,18 @@ func ApplyCloudEventsPubSubOrderingKey(ctx context.Context, opts ...Option) cont return ctx } +func ApplyCloudEventOptions(ctx context.Context, event *event.Event, opts ...Option) context.Context { + ctx = ApplyCloudEventsPubSubOrderingKey(ctx, opts...) + for _, opt := range opts { + if opt.extensions != nil { + for k, v := range opt.extensions { + event.SetExtension(k, v) + } + } + } + return ctx +} + // PanicPublisher wraps a Publisher. // Panics if publishing fails. type PanicPublisher[EV any] struct {