-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add OTLP export and W3C trace context propagation to tracing #9414
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mscolnick
merged 3 commits into
marimo-team:main
from
tigretigre:feature/otel-otlp-propagation
May 2, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |||||||||||||||||
|
|
||||||||||||||||||
| import os | ||||||||||||||||||
| from contextlib import contextmanager | ||||||||||||||||||
| from typing import TYPE_CHECKING, Any, cast | ||||||||||||||||||
| from typing import TYPE_CHECKING, Any, Literal, cast | ||||||||||||||||||
|
|
||||||||||||||||||
| from marimo import _loggers | ||||||||||||||||||
| from marimo._config.settings import GLOBAL_SETTINGS | ||||||||||||||||||
|
|
@@ -81,6 +81,37 @@ def start_as_current_span(self, *args: Any, **kwargs: Any) -> Any: | |||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| TRACE_FILENAME = os.path.join("traces", "spans.jsonl") | ||||||||||||||||||
| OTLPProtocol = Literal["grpc", "http/protobuf"] | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _otlp_endpoint_configured() -> bool: | ||||||||||||||||||
| return bool( | ||||||||||||||||||
| os.environ.get("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT") | ||||||||||||||||||
| or os.environ.get("OTEL_EXPORTER_OTLP_ENDPOINT") | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _otlp_protocol() -> OTLPProtocol | None: | ||||||||||||||||||
| protocol = ( | ||||||||||||||||||
| ( | ||||||||||||||||||
| os.environ.get("OTEL_EXPORTER_OTLP_TRACES_PROTOCOL") | ||||||||||||||||||
| or os.environ.get("OTEL_EXPORTER_OTLP_PROTOCOL") | ||||||||||||||||||
| or "http/protobuf" | ||||||||||||||||||
| ) | ||||||||||||||||||
| .strip() | ||||||||||||||||||
| .lower() | ||||||||||||||||||
| ) | ||||||||||||||||||
| protocol = protocol or "http/protobuf" | ||||||||||||||||||
|
|
||||||||||||||||||
| if protocol in ("grpc", "http/protobuf"): | ||||||||||||||||||
| return cast(OTLPProtocol, protocol) | ||||||||||||||||||
|
|
||||||||||||||||||
| LOGGER.warning( | ||||||||||||||||||
| "Unsupported OTLP protocol %r; expected 'grpc' or " | ||||||||||||||||||
| "'http/protobuf'. Falling back to file export.", | ||||||||||||||||||
| protocol, | ||||||||||||||||||
| ) | ||||||||||||||||||
| return None | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| def _set_tracer_provider() -> None: | ||||||||||||||||||
|
|
@@ -90,6 +121,7 @@ def _set_tracer_provider() -> None: | |||||||||||||||||
| DependencyManager.opentelemetry.require("for tracing.") | ||||||||||||||||||
|
|
||||||||||||||||||
| from opentelemetry import trace | ||||||||||||||||||
| from opentelemetry.sdk.resources import Resource | ||||||||||||||||||
| from opentelemetry.sdk.trace import ReadableSpan, TracerProvider | ||||||||||||||||||
| from opentelemetry.sdk.trace.export import ( | ||||||||||||||||||
| BatchSpanProcessor, | ||||||||||||||||||
|
|
@@ -103,37 +135,80 @@ def _set_tracer_provider() -> None: | |||||||||||||||||
| except Exception: | ||||||||||||||||||
| return | ||||||||||||||||||
|
|
||||||||||||||||||
| class FileExporter(SpanExporter): | ||||||||||||||||||
| def __init__(self, file_path: Path) -> None: | ||||||||||||||||||
| self.file_path = file_path | ||||||||||||||||||
| # Clear file | ||||||||||||||||||
| self.file_path.write_bytes(b"") | ||||||||||||||||||
|
|
||||||||||||||||||
| def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult: | ||||||||||||||||||
| try: | ||||||||||||||||||
| with self.file_path.open("a", encoding="utf-8") as f: | ||||||||||||||||||
| for span in spans: | ||||||||||||||||||
| f.write(span.to_json(cast(Any, None))) | ||||||||||||||||||
| f.write("\n") | ||||||||||||||||||
| return SpanExportResult.SUCCESS | ||||||||||||||||||
| except Exception as e: | ||||||||||||||||||
| LOGGER.exception(e) | ||||||||||||||||||
| return SpanExportResult.FAILURE | ||||||||||||||||||
|
|
||||||||||||||||||
| def shutdown(self) -> None: | ||||||||||||||||||
| pass | ||||||||||||||||||
|
|
||||||||||||||||||
| # Create a directory for logs if it doesn't exist | ||||||||||||||||||
| config_ready = ConfigReader.for_filename(TRACE_FILENAME) | ||||||||||||||||||
| filepath = config_ready.filepath | ||||||||||||||||||
| filepath.parent.mkdir(parents=True, exist_ok=True) | ||||||||||||||||||
|
|
||||||||||||||||||
| # Create a file exporter | ||||||||||||||||||
| file_exporter: FileExporter = FileExporter(filepath) | ||||||||||||||||||
|
|
||||||||||||||||||
| provider = TracerProvider() | ||||||||||||||||||
| processor = BatchSpanProcessor(file_exporter) | ||||||||||||||||||
| provider.add_span_processor(processor) | ||||||||||||||||||
| otlp_protocol = _otlp_protocol() if _otlp_endpoint_configured() else None | ||||||||||||||||||
| OTLPSpanExporter: Any | None = None | ||||||||||||||||||
| if otlp_protocol == "grpc": | ||||||||||||||||||
| try: | ||||||||||||||||||
| from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( | ||||||||||||||||||
| OTLPSpanExporter as GrpcOTLPSpanExporter, | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| OTLPSpanExporter = GrpcOTLPSpanExporter | ||||||||||||||||||
| except ImportError: | ||||||||||||||||||
| LOGGER.warning( | ||||||||||||||||||
| "opentelemetry-exporter-otlp-proto-grpc not installed; " | ||||||||||||||||||
| "install marimo[otel] for OTLP export. Falling back to file export.", | ||||||||||||||||||
| ) | ||||||||||||||||||
| elif otlp_protocol == "http/protobuf": | ||||||||||||||||||
| try: | ||||||||||||||||||
| from opentelemetry.exporter.otlp.proto.http.trace_exporter import ( | ||||||||||||||||||
| OTLPSpanExporter as HttpOTLPSpanExporter, | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| OTLPSpanExporter = HttpOTLPSpanExporter | ||||||||||||||||||
| except ImportError: | ||||||||||||||||||
| LOGGER.warning( | ||||||||||||||||||
| "opentelemetry-exporter-otlp-proto-http not installed; " | ||||||||||||||||||
| "install marimo[otel] for OTLP export. Falling back to file export.", | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| if OTLPSpanExporter is not None: | ||||||||||||||||||
| resource = Resource.create( | ||||||||||||||||||
| { | ||||||||||||||||||
| "service.name": "marimo", | ||||||||||||||||||
| }, | ||||||||||||||||||
| ) | ||||||||||||||||||
|
Comment on lines
+166
to
+170
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Explicitly passing Prompt for AI agents
Suggested change
|
||||||||||||||||||
| provider = TracerProvider(resource=resource) | ||||||||||||||||||
| provider.add_span_processor( | ||||||||||||||||||
| BatchSpanProcessor( | ||||||||||||||||||
| OTLPSpanExporter(), | ||||||||||||||||||
| ), | ||||||||||||||||||
| ) | ||||||||||||||||||
| LOGGER.debug( | ||||||||||||||||||
| "OTel tracer: OTLP export via %s", | ||||||||||||||||||
| otlp_protocol, | ||||||||||||||||||
| ) | ||||||||||||||||||
| else: | ||||||||||||||||||
|
|
||||||||||||||||||
| class FileExporter(SpanExporter): | ||||||||||||||||||
| def __init__(self, file_path: Path) -> None: | ||||||||||||||||||
| self.file_path = file_path | ||||||||||||||||||
| self.file_path.write_bytes(b"") | ||||||||||||||||||
|
|
||||||||||||||||||
| def export( | ||||||||||||||||||
| self, | ||||||||||||||||||
| spans: Sequence[ReadableSpan], | ||||||||||||||||||
| ) -> SpanExportResult: | ||||||||||||||||||
| try: | ||||||||||||||||||
| with self.file_path.open("a", encoding="utf-8") as f: | ||||||||||||||||||
| for span in spans: | ||||||||||||||||||
| f.write(span.to_json(cast(Any, None))) | ||||||||||||||||||
| f.write("\n") | ||||||||||||||||||
| return SpanExportResult.SUCCESS | ||||||||||||||||||
| except Exception as e: | ||||||||||||||||||
| LOGGER.exception(e) | ||||||||||||||||||
| return SpanExportResult.FAILURE | ||||||||||||||||||
|
|
||||||||||||||||||
| def shutdown(self) -> None: | ||||||||||||||||||
| pass | ||||||||||||||||||
|
|
||||||||||||||||||
| config_ready = ConfigReader.for_filename(TRACE_FILENAME) | ||||||||||||||||||
| filepath = config_ready.filepath | ||||||||||||||||||
| filepath.parent.mkdir(parents=True, exist_ok=True) | ||||||||||||||||||
|
|
||||||||||||||||||
| provider = TracerProvider() | ||||||||||||||||||
| provider.add_span_processor(BatchSpanProcessor(FileExporter(filepath))) | ||||||||||||||||||
| LOGGER.debug("OTel tracer: file export to %s", filepath) | ||||||||||||||||||
|
|
||||||||||||||||||
| # Sets the global default tracer provider | ||||||||||||||||||
| trace.set_tracer_provider(provider) | ||||||||||||||||||
|
|
||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.