Conversation
| 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 |
There was a problem hiding this comment.
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
passThis would provide more reliable parsing of the TOML structure and avoid potential edge cases.
| 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.
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
dsafreno
left a comment
There was a problem hiding this comment.
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.
|
Makes sense - will simplify all this. |
init()
* 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>
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
setup()function inlib/otel_setup.pyfor simplified OpenTelemetry initializationotel_wrapper_example.pydemonstrating three usage patterns:setupfunction from main__init__.pymoduleImpact
setup()function available in the gentrace packageTesting Considerations
@interactiondecorator