Skip to content

Conversation

fabiomargarido
Copy link

@fabiomargarido fabiomargarido commented Sep 25, 2025

What does this PR do?

Add WithCustomConsumerSpanOptions and WithCustomProducerSpanOptions options to enable customizing spans generated by wrapped consumers and producers.

Motivation

Including extra information (e.g. Kafka message keys) as tags on spans can greatly enhance observability, enabling users to filter, group, and analyze traces based on specific message properties. This improves debugging capabilities and provides a clearer understanding of message flow within Kafka-based systems.

Reviewer's Checklist

  • Changed code has unit tests for its functionality at or near 100% coverage.
  • System-Tests covering this feature have been added and enabled with the va.b.c-dev version tag.
  • There is a benchmark for any new code, or changes to existing code.
  • If this interacts with the agent in a new way, a system test has been added.
  • New code is free of linting errors. You can check this by running ./scripts/lint.sh locally.
  • Add an appropriate team label so this PR gets put in the right place for the release notes.
  • Non-trivial go.mod changes, e.g. adding new modules, are reviewed by @DataDog/dd-trace-go-guild.

Unsure? Have a question? Request a review!

@fabiomargarido fabiomargarido requested review from a team as code owners September 25, 2025 14:17
@fabiomargarido
Copy link
Author

fabiomargarido commented Sep 30, 2025

Discussing this with colleagues we also came up with the idea of changing this to a more generic WithCustomTags which takes a func that returns a slice of tags to be added to the span. I can change it to that if you think it makes more sense.

@kakkoyun
Copy link
Member

kakkoyun commented Oct 2, 2025

Discussing this with colleagues we also came up with the idea of changing this to a more generic WithCustomTags which takes a func that returns a slice of tags to be added to the span. I can change it to that if you think it makes more sense.

This sounds more future-proof TBH.

@fabiomargarido
Copy link
Author

Cool, thanks for the feedback @kakkoyun. I'm on vacation for the next few weeks so not sure when I'll be able to update this, but I'll try to make some time to do so.

@rarguelloF rarguelloF added the apm:ecosystem contrib/* related feature requests or bugs label Oct 3, 2025
@fabiomargarido fabiomargarido changed the title feat(contrib/sarama): add option to tag spans with kafka key feat(contrib/sarama): option to add custom consumer/producer span options Oct 3, 2025
@gubtos
Copy link

gubtos commented Oct 8, 2025

@kakkoyun Could you please let me know if there are any additional changes needed, or if the implemented code looks good to you?

}

// WithCustomConsumerSpanOptions enables calling a callback func to add custom span options on wrapped consumers.
func WithCustomConsumerSpanOptions(f CustomConsumerSpanOptsFunc) OptionFn {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func WithCustomConsumerSpanOptions(f CustomConsumerSpanOptsFunc) OptionFn {
func WithConsumerCustomTag(tag string, tagFn func(msg *sarama.ConsumerMessage) any) OptionFn {

As the conversation in the PR, lets keep it scoped to custom tags.
Also let's try to follow the same format of the the WithCustomTag option in other integrations, where the first argument is the tag name and the second is the function to compute the value.

Comment on lines +16 to +17
type CustomConsumerSpanOptsFunc func(msg *sarama.ConsumerMessage) []tracer.StartSpanOption
type CustomProducerSpanOptsFunc func(msg *sarama.ProducerMessage) []tracer.StartSpanOption
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type CustomConsumerSpanOptsFunc func(msg *sarama.ConsumerMessage) []tracer.StartSpanOption
type CustomProducerSpanOptsFunc func(msg *sarama.ProducerMessage) []tracer.StartSpanOption

these aliases shouldn't be necessary

Comment on lines +27 to +28
customConsumerSpanOptionsFunc CustomConsumerSpanOptsFunc
customProducerSpanOptionsFunc CustomProducerSpanOptsFunc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
customConsumerSpanOptionsFunc CustomConsumerSpanOptsFunc
customProducerSpanOptionsFunc CustomProducerSpanOptsFunc
consumerCustomTags map[string]func(msg *sarama.ConsumerMessage) any
producerCustomTags map[string]func(msg *sarama.ProducerMessage) any

}

// WithCustomProducerSpanOptions enables calling a callback func to add custom span options on wrapped producers.
func WithCustomProducerSpanOptions(f CustomProducerSpanOptsFunc) OptionFn {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func WithCustomProducerSpanOptions(f CustomProducerSpanOptsFunc) OptionFn {
func WithProducerCustomTag(tag string, tagFn func(msg *sarama.ProducerMessage) any) OptionFn {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

apm:ecosystem contrib/* related feature requests or bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants