Skip to content

feat: Add OTEL setup in init()#317

Merged
viveknair merged 13 commits intonextfrom
vivek/wip-setup-function
Jun 19, 2025
Merged

feat: Add OTEL setup in init()#317
viveknair merged 13 commits intonextfrom
vivek/wip-setup-function

Conversation

@viveknair
Copy link
Copy Markdown
Contributor

@viveknair viveknair commented Jun 17, 2025

TLDR

Adds a setup function to simplify OpenTelemetry configuration with Gentrace integration

Summary

This PR introduces a new setup() function that provides a streamlined way to configure OpenTelemetry with Gentrace. It reduces boilerplate code by wrapping common OpenTelemetry initialization patterns and offering optional Gentrace-specific features like custom samplers.

Key Changes

  • Added setup() function in lib/otel_setup.py for simplified OpenTelemetry initialization
  • Created comprehensive example file otel_wrapper_example.py demonstrating three usage patterns:
    • Basic OTEL setup without Gentrace samplers
    • OTEL setup with Gentrace samplers enabled
    • Full configuration with all available options
  • Exported setup function from main __init__.py module

Impact

  • New functionality: Adds a convenience wrapper for OpenTelemetry setup
  • API additions: New public setup() function available in the gentrace package
  • No breaking changes: This is an additive feature that doesn't modify existing functionality

Testing Considerations

  • Test the setup function with various configuration combinations
  • Verify that spans are correctly sent to Gentrace with different sampler configurations
  • Ensure the console exporter works when enabled for debugging
  • Test integration with the existing @interaction decorator

@viveknair viveknair marked this pull request as ready for review June 17, 2025 16:45
@viveknair viveknair requested a review from dsafreno June 17, 2025 17:57
Comment thread src/gentrace/lib/otel_setup.py Outdated
Comment on lines +88 to +96
content = pyproject_path.read_text()
for line in content.split('\n'):
if line.strip().startswith('name'):
# Extract value between quotes
parts = line.split('=', 1)
if len(parts) == 2:
name = parts[1].strip().strip('"').strip("'")
if name:
return name
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The TOML parsing implementation is vulnerable to false positives. The current approach using string splitting could incorrectly extract values from lines that happen to contain name = in other contexts (like descriptions or comments).

Consider using a proper TOML parser library instead:

try:
    import tomli  # Python 3.11+ has tomllib in stdlib
    with open(pyproject_path, "rb") as f:
        pyproject_data = tomli.load(f)
        # Extract name from project table or other relevant location
        if "project" in pyproject_data and "name" in pyproject_data["project"]:
            return pyproject_data["project"]["name"]
        # Handle other TOML structures as needed
except ImportError:
    # Fallback to simple parsing or add tomli as a dependency
    pass

This would provide more reliable parsing of the TOML structure and avoid potential edge cases.

Suggested change
content = pyproject_path.read_text()
for line in content.split('\n'):
if line.strip().startswith('name'):
# Extract value between quotes
parts = line.split('=', 1)
if len(parts) == 2:
name = parts[1].strip().strip('"').strip("'")
if name:
return name
try:
# For Python 3.11+
try:
import tomllib
with open(pyproject_path, "rb") as f:
pyproject_data = tomllib.load(f)
except ImportError:
# For Python < 3.11
import tomli
with open(pyproject_path, "rb") as f:
pyproject_data = tomli.load(f)
# Extract name from project table
if "project" in pyproject_data and "name" in pyproject_data["project"]:
return pyproject_data["project"]["name"]
# Fallback to poetry format
elif "tool" in pyproject_data and "poetry" in pyproject_data["tool"] and "name" in pyproject_data["tool"]["poetry"]:
return pyproject_data["tool"]["poetry"]["name"]
except (ImportError, FileNotFoundError, KeyError):
# Fallback to simple parsing if TOML parsing fails
content = pyproject_path.read_text()
for line in content.split('\n'):
if line.strip().startswith('name'):
# Extract value between quotes
parts = line.split('=', 1)
if len(parts) == 2:
name = parts[1].strip().strip('"').strip("'")
if name:
return name

Spotted by Diamond

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

@socket-security
Copy link
Copy Markdown

socket-security bot commented Jun 18, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedopeninference-instrumentation-openai@​0.1.3099100100100100

View full report

Copy link
Copy Markdown
Contributor

@dsafreno dsafreno left a comment

Choose a reason for hiding this comment

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

This works well and the dx is good. Flagging that I'm a little worried about how examples is becoming a mix of test code and actual examples, and seems pretty cluttered. But that's something we can address later.

Copy link
Copy Markdown
Contributor Author

Makes sense - will simplify all this.

@viveknair viveknair changed the title feat: Add setup function for OpenTelemetry with Gentrace feat: Add OTEL setup in init() Jun 19, 2025
@viveknair viveknair merged commit f6ed028 into next Jun 19, 2025
9 checks passed
@stainless-app stainless-app bot mentioned this pull request Jun 19, 2025
viveknair pushed a commit that referenced this pull request Jun 19, 2025
* feat: Add OTEL setup in `init()` (#317)

* release: 1.0.0-alpha.5

---------

Co-authored-by: Vivek Nair <vivek@gentrace.ai>
Co-authored-by: stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com>
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