Skip to content

fix: add gentrace.in_experiment to span attributes#374

Merged
viveknair merged 3 commits intomainfrom
josh/add-in-experiment-flag
Aug 16, 2025
Merged

fix: add gentrace.in_experiment to span attributes#374
viveknair merged 3 commits intomainfrom
josh/add-in-experiment-flag

Conversation

@joshlebed
Copy link
Collaborator

@joshlebed joshlebed commented Aug 15, 2025

TL;DR

Added a new attribute ATTR_GENTRACE_IN_EXPERIMENT to track when spans are part of an experiment.

What changed?

  • Added a new constant ATTR_GENTRACE_IN_EXPERIMENT to track when spans are created within an experiment context
  • Updated the span processor to propagate this attribute from baggage to spans
  • Modified the eval and eval_dataset functions to set this attribute in the baggage context
  • Exported the new attribute in the package's __init__.py

How to test?

  1. Run an experiment using gentrace.eval() or gentrace.eval_dataset()
  2. Verify that spans created during the experiment have the gentrace.in_experiment attribute set to "true"
  3. Check that this attribute is properly propagated through the OpenTelemetry context

Why make this change?

This change enables better tracking of spans that are specifically created within experiment contexts. By adding a dedicated attribute for experiment participation, it becomes easier to filter and analyze spans that are part of experiments versus those that are not, improving observability and data analysis capabilities.

Copy link
Collaborator Author


How to use the Graphite Merge Queue

Add the label merge-queue to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@joshlebed joshlebed requested a review from viveknair August 15, 2025 17:58
@joshlebed joshlebed marked this pull request as ready for review August 15, 2025 17:58
Comment on lines 94 to 96
context_with_modified_baggage = otel_baggage.set_baggage(
ATTR_GENTRACE_IN_EXPERIMENT, "true", context=current_context
)
Copy link
Contributor

Choose a reason for hiding this comment

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

The current implementation overwrites the baggage context rather than extending it. The second call to set_baggage should use the result from the first call as its context parameter:

context_with_modified_baggage = otel_baggage.set_baggage(
    ATTR_GENTRACE_IN_EXPERIMENT, "true", context=context_with_modified_baggage
)

This ensures both baggage items are preserved in the context. The same issue appears in the eval_dataset.py file as well.

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yeah this looks bad @joshlebed - did we check if gentrace.sample still gets set?

Copy link
Contributor

Choose a reason for hiding this comment

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

Another way to validate that both are sent could be to set the OTLP trace exporter path to the local collector at 4318 and then see if it shows both span attributes

https://github.com/gentrace/gentrace/tree/main/otel-dev

"eval_dataset",
"TestInput",
"ATTR_GENTRACE_SAMPLE_KEY",
"ATTR_GENTRACE_IN_EXPERIMENT",
Copy link
Contributor

Choose a reason for hiding this comment

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

There appears to be a duplicate export of ATTR_GENTRACE_IN_EXPERIMENT in the __init__.py file. It's being exported once on line 173 and again on line 177 (after the changes). To maintain clean and consistent exports, please remove one of these duplicate entries.

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

Comment on lines 94 to 96
context_with_modified_baggage = otel_baggage.set_baggage(
ATTR_GENTRACE_IN_EXPERIMENT, "true", context=current_context
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yeah this looks bad @joshlebed - did we check if gentrace.sample still gets set?

Comment on lines 94 to 96
context_with_modified_baggage = otel_baggage.set_baggage(
ATTR_GENTRACE_IN_EXPERIMENT, "true", context=current_context
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Another way to validate that both are sent could be to set the OTLP trace exporter path to the local collector at 4318 and then see if it shows both span attributes

https://github.com/gentrace/gentrace/tree/main/otel-dev

Comment on lines 219 to 221
context_with_modified_baggage = otel_baggage.set_baggage(
ATTR_GENTRACE_IN_EXPERIMENT, "true", context=current_context
)
Copy link
Contributor

Choose a reason for hiding this comment

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

There's an issue with the context parameter in this baggage setting. The code is using current_context instead of context_with_modified_baggage, which would overwrite rather than build upon the previous baggage modification. To maintain both baggage values, please update to use context_with_modified_baggage as the context parameter, similar to the implementation in eval.py:

context_with_modified_baggage = otel_baggage.set_baggage(
    ATTR_GENTRACE_IN_EXPERIMENT, "true", context=context_with_modified_baggage
)

This ensures that both the sample key and in_experiment attributes are properly preserved in the baggage context.

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

@joshlebed joshlebed requested a review from viveknair August 15, 2025 19:10
Copy link
Contributor

@viveknair viveknair left a comment

Choose a reason for hiding this comment

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

lgtm

@viveknair viveknair merged commit f871c76 into main Aug 16, 2025
25 checks passed
@stainless-app stainless-app bot mentioned this pull request Aug 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants