-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
226446d
commit fc9f402
Showing
8 changed files
with
183 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
70 changes: 70 additions & 0 deletions
70
src/core/trulens/experimental/otel_tracing/core/exporter.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from datetime import datetime | ||
import logging | ||
from typing import Optional, Sequence | ||
|
||
from opentelemetry.sdk.trace import ReadableSpan | ||
from opentelemetry.sdk.trace.export import SpanExporter | ||
from opentelemetry.sdk.trace.export import SpanExportResult | ||
from opentelemetry.trace import StatusCode | ||
from trulens.core.database import connector as core_connector | ||
from trulens.core.schema import event as event_schema | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def to_timestamp(timestamp: Optional[int]) -> datetime: | ||
if timestamp: | ||
return datetime.fromtimestamp(timestamp * 1e-9) | ||
|
||
return datetime.now() | ||
|
||
|
||
class TruLensDBSpanExporter(SpanExporter): | ||
""" | ||
Implementation of `SpanExporter` that flushes the spans to the database in the TruLens session. | ||
""" | ||
|
||
connector: core_connector.DBConnector | ||
|
||
def __init__(self, connector: core_connector.DBConnector): | ||
self.connector = connector | ||
|
||
def _construct_event(self, span: ReadableSpan) -> event_schema.Event: | ||
context = span.get_span_context() | ||
parent = span.parent | ||
|
||
if context is None: | ||
raise ValueError("Span context is None") | ||
|
||
return event_schema.Event( | ||
event_id=str(context.span_id), | ||
record={ | ||
"name": span.name, | ||
"kind": "SPAN_KIND_TRULENS", | ||
"parent_span_id": str(parent.span_id if parent else ""), | ||
"status": "STATUS_CODE_ERROR" | ||
if span.status.status_code == StatusCode.ERROR | ||
else "STATUS_CODE_UNSET", | ||
}, | ||
record_attributes=span.attributes, | ||
record_type=event_schema.EventRecordType.SPAN, | ||
resource_attributes=span.resource.attributes, | ||
start_timestamp=to_timestamp(span.start_time), | ||
timestamp=to_timestamp(span.end_time), | ||
trace={ | ||
"span_id": str(context.span_id), | ||
"trace_id": str(context.trace_id), | ||
"parent_id": str(parent.span_id if parent else ""), | ||
}, | ||
) | ||
|
||
def export(self, spans: Sequence[ReadableSpan]) -> SpanExportResult: | ||
try: | ||
events = list(map(self._construct_event, spans)) | ||
self.connector.add_events(events) | ||
|
||
except Exception as e: | ||
logger.error("Error exporting spans to the database: %s", e) | ||
return SpanExportResult.FAILURE | ||
|
||
return SpanExportResult.SUCCESS |
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import logging | ||
|
||
from opentelemetry import trace | ||
from opentelemetry.sdk.resources import Resource | ||
from opentelemetry.sdk.trace import TracerProvider | ||
from opentelemetry.sdk.trace.export import ConsoleSpanExporter | ||
from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
from trulens.core.session import TruSession | ||
from trulens.experimental.otel_tracing.core.exporter import ( | ||
TruLensDBSpanExporter, | ||
) | ||
|
||
TRULENS_SERVICE_NAME = "trulens" | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def init(session: TruSession, debug: bool = False): | ||
"""Initialize the OpenTelemetry SDK with TruLens configuration.""" | ||
resource = Resource.create({"service.name": TRULENS_SERVICE_NAME}) | ||
provider = TracerProvider(resource=resource) | ||
trace.set_tracer_provider(provider) | ||
|
||
if debug: | ||
logging.debug( | ||
"Initializing OpenTelemetry with TruLens configuration for console debugging" | ||
) | ||
# Add a console exporter for debugging purposes | ||
console_exporter = ConsoleSpanExporter() | ||
console_processor = SimpleSpanProcessor(console_exporter) | ||
provider.add_span_processor(console_processor) | ||
|
||
if session.connector: | ||
logging.debug("Exporting traces to the TruLens database") | ||
|
||
# Check the database revision | ||
try: | ||
db_revision = session.connector.db.get_db_revision() | ||
if db_revision is None: | ||
raise ValueError( | ||
"Database revision is not set. Please run the migrations." | ||
) | ||
if int(db_revision) < 10: | ||
raise ValueError( | ||
"Database revision is too low. Please run the migrations." | ||
) | ||
except Exception: | ||
raise ValueError("Error checking the database revision.") | ||
|
||
# Add the TruLens database exporter | ||
db_exporter = TruLensDBSpanExporter(session.connector) | ||
db_processor = SimpleSpanProcessor(db_exporter) | ||
provider.add_span_processor(db_processor) |
This file contains 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 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