Skip to content

Respect OTEL_RESOURCE_ATTRIBUTES#125

Merged
tdyas merged 4 commits intoshoalsoft:mainfrom
wisechengyi:respectOTEL_RESOURCE_ATTRIBUTES
Feb 3, 2026
Merged

Respect OTEL_RESOURCE_ATTRIBUTES#125
tdyas merged 4 commits intoshoalsoft:mainfrom
wisechengyi:respectOTEL_RESOURCE_ATTRIBUTES

Conversation

@wisechengyi
Copy link
Contributor

@wisechengyi wisechengyi commented Jan 23, 2026

Partially address #104

Problem

The Pants OpenTelemetry plugin did not respect the standard OpenTelemetry environment variable OTEL_RESOURCE_ATTRIBUTES. This meant users could not add custom resource attributes (like team name, environment, user info, etc.) to their telemetry data
through the standard OpenTelemetry configuration mechanism.

Resource attributes are key-value pairs that describe the entity producing telemetry data and are essential for filtering, grouping, and analyzing traces in observability platforms.

Solution

Modified the plugin to read and respect the OTEL_RESOURCE_ATTRIBUTES environment variable:

  1. Read the environment variable: Updated register.py to fetch OTEL_RESOURCE_ATTRIBUTES alongside TRACEPARENT when the plugin is enabled.

  2. Leverage OpenTelemetry's built-in parsing: Changed opentelemetry_processor.py to use Resource.create() instead of manually constructing the Resource. The Resource.create() method automatically parses and merges attributes from the
    OTEL_RESOURCE_ATTRIBUTES environment variable.

  3. Safe environment handling: Implemented a context manager to temporarily set the environment variable during resource creation, ensuring no side effects on the global environment.

  4. Comprehensive testing: Added integration test do_test_of_resource_attributes() that verifies custom resource attributes are properly included in exported telemetry spans.

Example

Before (main branch):

# Custom attributes were ignored                                                                                                                                                                                                                           
export OTEL_RESOURCE_ATTRIBUTES="team=ml-platform,env=production,user.name=alice"                                                                                                                                                                          
pants --shoalsoft-opentelemetry-enabled list ::                                                                                                                                                                                                            
# Exported spans only contained: service.name=pantsbuild, telemetry.sdk.name, etc.                                                                                                                                                                         

After (this branch):

  # Custom attributes are now included in all telemetry spans                                                                                                                                                                                                
  export OTEL_RESOURCE_ATTRIBUTES="team=ml-platform,env=production,user.name=alice"                                                                                                                                                                          
  pants --shoalsoft-opentelemetry-enabled list ::                                                                                                                                                                                                            
  # Exported spans contain: service.name=pantsbuild, team=ml-platform,                                                                                                                                                                                       
  #                         env=production, user.name=alice, etc.                                                                                                                                                                                            

This allows teams to:

  • Filter traces by team, environment, or user in their observability platform
  • Add custom metadata for cost attribution or debugging
  • Follow standard OpenTelemetry conventions for resource attributes

@wisechengyi wisechengyi marked this pull request as ready for review January 23, 2026 01:13
old_value = os.environ.get(key)
try:
if value is not None:
os.environ[key] = value
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this helper do del os.environ[key] if value is None?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll make an update in a separate PR.

@tdyas tdyas merged commit d3fb175 into shoalsoft:main Feb 3, 2026
1 check passed
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