nvidia-nat-opentelemetry 1.1.0a20251020__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nat/meta/pypi.md +23 -0
- nat/plugins/opentelemetry/__init__.py +24 -0
- nat/plugins/opentelemetry/mixin/__init__.py +14 -0
- nat/plugins/opentelemetry/mixin/otlp_span_exporter_mixin.py +69 -0
- nat/plugins/opentelemetry/otel_span.py +525 -0
- nat/plugins/opentelemetry/otel_span_exporter.py +165 -0
- nat/plugins/opentelemetry/otlp_span_adapter_exporter.py +94 -0
- nat/plugins/opentelemetry/otlp_span_redaction_adapter_exporter.py +144 -0
- nat/plugins/opentelemetry/register.py +194 -0
- nat/plugins/opentelemetry/span_converter.py +228 -0
- nvidia_nat_opentelemetry-1.1.0a20251020.dist-info/METADATA +47 -0
- nvidia_nat_opentelemetry-1.1.0a20251020.dist-info/RECORD +17 -0
- nvidia_nat_opentelemetry-1.1.0a20251020.dist-info/WHEEL +5 -0
- nvidia_nat_opentelemetry-1.1.0a20251020.dist-info/entry_points.txt +2 -0
- nvidia_nat_opentelemetry-1.1.0a20251020.dist-info/licenses/LICENSE-3rd-party.txt +5478 -0
- nvidia_nat_opentelemetry-1.1.0a20251020.dist-info/licenses/LICENSE.md +201 -0
- nvidia_nat_opentelemetry-1.1.0a20251020.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
from abc import abstractmethod
|
|
18
|
+
from importlib.metadata import PackageNotFoundError
|
|
19
|
+
from importlib.metadata import version
|
|
20
|
+
|
|
21
|
+
from nat.builder.context import ContextState
|
|
22
|
+
from nat.data_models.span import Span
|
|
23
|
+
from nat.observability.exporter.span_exporter import SpanExporter
|
|
24
|
+
from nat.observability.processor.batching_processor import BatchingProcessor
|
|
25
|
+
from nat.observability.processor.processor import Processor
|
|
26
|
+
from nat.plugins.opentelemetry.otel_span import OtelSpan
|
|
27
|
+
from nat.plugins.opentelemetry.span_converter import convert_span_to_otel
|
|
28
|
+
from opentelemetry.sdk.resources import Resource
|
|
29
|
+
|
|
30
|
+
logger = logging.getLogger(__name__)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_opentelemetry_sdk_version() -> str:
|
|
34
|
+
"""Get the OpenTelemetry SDK version dynamically.
|
|
35
|
+
|
|
36
|
+
Returns:
|
|
37
|
+
The version of the opentelemetry-sdk package, or 'unknown' if not found.
|
|
38
|
+
"""
|
|
39
|
+
try:
|
|
40
|
+
return version("opentelemetry-sdk")
|
|
41
|
+
except PackageNotFoundError:
|
|
42
|
+
logger.warning("Could not determine opentelemetry-sdk version")
|
|
43
|
+
return "unknown"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class SpanToOtelProcessor(Processor[Span, OtelSpan]):
|
|
47
|
+
"""Processor that converts a Span to an OtelSpan."""
|
|
48
|
+
|
|
49
|
+
async def process(self, item: Span) -> OtelSpan:
|
|
50
|
+
return convert_span_to_otel(item) # type: ignore
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class OtelSpanBatchProcessor(BatchingProcessor[OtelSpan]):
|
|
54
|
+
"""Processor that batches OtelSpans with explicit type information.
|
|
55
|
+
|
|
56
|
+
This class provides explicit type information for the TypeIntrospectionMixin
|
|
57
|
+
by overriding the type properties directly.
|
|
58
|
+
"""
|
|
59
|
+
pass
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class OtelSpanExporter(SpanExporter[Span, OtelSpan]):
|
|
63
|
+
"""Abstract base class for OpenTelemetry exporters.
|
|
64
|
+
|
|
65
|
+
This class provides a specialized implementation for OpenTelemetry exporters.
|
|
66
|
+
It builds upon SpanExporter's span construction logic and automatically adds
|
|
67
|
+
a SpanToOtelProcessor to transform Span objects into OtelSpan objects.
|
|
68
|
+
|
|
69
|
+
The processing flow is:
|
|
70
|
+
IntermediateStep → Span → OtelSpan → Export
|
|
71
|
+
|
|
72
|
+
Key Features:
|
|
73
|
+
- Automatic span construction from IntermediateStep events (via SpanExporter)
|
|
74
|
+
- Built-in Span to OtelSpan conversion (via SpanToOtelProcessor)
|
|
75
|
+
- Support for additional processing steps if needed
|
|
76
|
+
- Type-safe processing pipeline with enhanced TypeVar compatibility
|
|
77
|
+
- Batching support for efficient export
|
|
78
|
+
|
|
79
|
+
Inheritance Hierarchy:
|
|
80
|
+
- BaseExporter: Core functionality + TypeIntrospectionMixin
|
|
81
|
+
- ProcessingExporter: Processor pipeline support
|
|
82
|
+
- SpanExporter: Span creation and lifecycle management
|
|
83
|
+
- OtelExporter: OpenTelemetry-specific span transformation
|
|
84
|
+
|
|
85
|
+
Generic Types:
|
|
86
|
+
- InputSpanT: Always Span (from IntermediateStep conversion)
|
|
87
|
+
- OutputSpanT: Always OtelSpan (for OpenTelemetry compatibility)
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
def __init__(self,
|
|
91
|
+
context_state: ContextState | None = None,
|
|
92
|
+
batch_size: int = 100,
|
|
93
|
+
flush_interval: float = 5.0,
|
|
94
|
+
max_queue_size: int = 1000,
|
|
95
|
+
drop_on_overflow: bool = False,
|
|
96
|
+
shutdown_timeout: float = 10.0,
|
|
97
|
+
resource_attributes: dict[str, str] | None = None):
|
|
98
|
+
"""Initialize the OpenTelemetry exporter.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
context_state: The context state to use for the exporter.
|
|
102
|
+
batch_size: The batch size for exporting spans.
|
|
103
|
+
flush_interval: The flush interval in seconds for exporting spans.
|
|
104
|
+
max_queue_size: The maximum queue size for exporting spans.
|
|
105
|
+
drop_on_overflow: Whether to drop spans on overflow.
|
|
106
|
+
shutdown_timeout: The shutdown timeout in seconds.
|
|
107
|
+
resource_attributes: Additional resource attributes for spans.
|
|
108
|
+
"""
|
|
109
|
+
super().__init__(context_state)
|
|
110
|
+
|
|
111
|
+
# Initialize resource for span attribution
|
|
112
|
+
if resource_attributes is None:
|
|
113
|
+
resource_attributes = {}
|
|
114
|
+
self._resource = Resource(attributes=resource_attributes)
|
|
115
|
+
|
|
116
|
+
self.add_processor(SpanToOtelProcessor())
|
|
117
|
+
self.add_processor(
|
|
118
|
+
OtelSpanBatchProcessor(batch_size=batch_size,
|
|
119
|
+
flush_interval=flush_interval,
|
|
120
|
+
max_queue_size=max_queue_size,
|
|
121
|
+
drop_on_overflow=drop_on_overflow,
|
|
122
|
+
shutdown_timeout=shutdown_timeout))
|
|
123
|
+
|
|
124
|
+
async def export_processed(self, item: OtelSpan | list[OtelSpan]) -> None:
|
|
125
|
+
"""Export the processed span(s).
|
|
126
|
+
|
|
127
|
+
This method handles the common logic for all OTEL exporters:
|
|
128
|
+
- Normalizes single spans vs. batches
|
|
129
|
+
- Sets resource attributes on spans
|
|
130
|
+
- Delegates to the abstract export_otel_spans method
|
|
131
|
+
|
|
132
|
+
Args:
|
|
133
|
+
item (OtelSpan | list[OtelSpan]): The processed span(s) to export.
|
|
134
|
+
Can be a single span or a batch of spans from BatchingProcessor.
|
|
135
|
+
"""
|
|
136
|
+
try:
|
|
137
|
+
if isinstance(item, OtelSpan):
|
|
138
|
+
spans = [item]
|
|
139
|
+
elif isinstance(item, list):
|
|
140
|
+
spans = item
|
|
141
|
+
else:
|
|
142
|
+
logger.warning("Unexpected item type: %s", type(item))
|
|
143
|
+
return
|
|
144
|
+
|
|
145
|
+
# Set resource attributes on all spans
|
|
146
|
+
for span in spans:
|
|
147
|
+
span.set_resource(self._resource)
|
|
148
|
+
|
|
149
|
+
# Delegate to concrete implementation
|
|
150
|
+
await self.export_otel_spans(spans)
|
|
151
|
+
|
|
152
|
+
except Exception as e:
|
|
153
|
+
logger.error("Error exporting spans: %s", e, exc_info=True)
|
|
154
|
+
|
|
155
|
+
@abstractmethod
|
|
156
|
+
async def export_otel_spans(self, spans: list[OtelSpan]) -> None:
|
|
157
|
+
"""Export a list of OpenTelemetry spans.
|
|
158
|
+
|
|
159
|
+
This method must be implemented by concrete exporters to handle
|
|
160
|
+
the actual export logic (e.g., HTTP requests, file writes, etc.).
|
|
161
|
+
|
|
162
|
+
Args:
|
|
163
|
+
spans (list[OtelSpan]): The list of spans to export.
|
|
164
|
+
"""
|
|
165
|
+
pass
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
|
|
18
|
+
from nat.builder.context import ContextState
|
|
19
|
+
from nat.plugins.opentelemetry.mixin.otlp_span_exporter_mixin import OTLPSpanExporterMixin
|
|
20
|
+
from nat.plugins.opentelemetry.otel_span_exporter import OtelSpanExporter
|
|
21
|
+
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class OTLPSpanAdapterExporter(OTLPSpanExporterMixin, OtelSpanExporter):
|
|
26
|
+
"""An OpenTelemetry OTLP span exporter for sending traces to OTLP-compatible services.
|
|
27
|
+
|
|
28
|
+
This class combines the OtelSpanExporter base functionality with OTLP-specific
|
|
29
|
+
export capabilities to provide a complete solution for sending telemetry traces
|
|
30
|
+
to any OTLP-compatible collector or service via HTTP.
|
|
31
|
+
|
|
32
|
+
Key Features:
|
|
33
|
+
- Complete span processing pipeline (IntermediateStep → Span → OtelSpan → Export)
|
|
34
|
+
- Batching support for efficient transmission
|
|
35
|
+
- OTLP HTTP protocol for maximum compatibility
|
|
36
|
+
- Configurable authentication via headers
|
|
37
|
+
- Resource attribute management
|
|
38
|
+
- Error handling and retry logic
|
|
39
|
+
|
|
40
|
+
This exporter is commonly used with services like:
|
|
41
|
+
- OpenTelemetry Collector
|
|
42
|
+
- Jaeger (OTLP endpoint)
|
|
43
|
+
- Grafana Tempo
|
|
44
|
+
- Custom OTLP-compatible backends
|
|
45
|
+
|
|
46
|
+
Example::
|
|
47
|
+
|
|
48
|
+
exporter = OTLPSpanAdapterExporter(
|
|
49
|
+
endpoint="https://api.service.com/v1/traces",
|
|
50
|
+
headers={"Authorization": "Bearer your-token"},
|
|
51
|
+
batch_size=50,
|
|
52
|
+
flush_interval=10.0
|
|
53
|
+
)
|
|
54
|
+
"""
|
|
55
|
+
|
|
56
|
+
def __init__(
|
|
57
|
+
self,
|
|
58
|
+
*,
|
|
59
|
+
# OtelSpanExporter args
|
|
60
|
+
context_state: ContextState | None = None,
|
|
61
|
+
batch_size: int = 100,
|
|
62
|
+
flush_interval: float = 5.0,
|
|
63
|
+
max_queue_size: int = 1000,
|
|
64
|
+
drop_on_overflow: bool = False,
|
|
65
|
+
shutdown_timeout: float = 10.0,
|
|
66
|
+
resource_attributes: dict[str, str] | None = None,
|
|
67
|
+
# OTLPSpanExporterMixin args
|
|
68
|
+
endpoint: str,
|
|
69
|
+
headers: dict[str, str] | None = None,
|
|
70
|
+
**otlp_kwargs):
|
|
71
|
+
"""Initialize the OTLP span exporter.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
context_state: The context state for the exporter.
|
|
75
|
+
batch_size: Number of spans to batch before exporting.
|
|
76
|
+
flush_interval: Time in seconds between automatic batch flushes.
|
|
77
|
+
max_queue_size: Maximum number of spans to queue.
|
|
78
|
+
drop_on_overflow: Whether to drop spans when queue is full.
|
|
79
|
+
shutdown_timeout: Maximum time to wait for export completion during shutdown.
|
|
80
|
+
resource_attributes: Additional resource attributes for spans.
|
|
81
|
+
endpoint: The endpoint for the OTLP service.
|
|
82
|
+
headers: The headers for the OTLP service.
|
|
83
|
+
otlp_kwargs: Additional keyword arguments for the OTLP service.
|
|
84
|
+
"""
|
|
85
|
+
super().__init__(context_state=context_state,
|
|
86
|
+
batch_size=batch_size,
|
|
87
|
+
flush_interval=flush_interval,
|
|
88
|
+
max_queue_size=max_queue_size,
|
|
89
|
+
drop_on_overflow=drop_on_overflow,
|
|
90
|
+
shutdown_timeout=shutdown_timeout,
|
|
91
|
+
resource_attributes=resource_attributes,
|
|
92
|
+
endpoint=endpoint,
|
|
93
|
+
headers=headers,
|
|
94
|
+
**otlp_kwargs)
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
from collections.abc import Callable
|
|
18
|
+
from collections.abc import Mapping
|
|
19
|
+
from enum import Enum
|
|
20
|
+
from typing import Any
|
|
21
|
+
|
|
22
|
+
from nat.builder.context import ContextState
|
|
23
|
+
from nat.observability.processor.redaction import SpanHeaderRedactionProcessor
|
|
24
|
+
from nat.observability.processor.span_tagging_processor import SpanTaggingProcessor
|
|
25
|
+
from nat.plugins.opentelemetry.otlp_span_adapter_exporter import OTLPSpanAdapterExporter
|
|
26
|
+
|
|
27
|
+
logger = logging.getLogger(__name__)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class OTLPSpanHeaderRedactionAdapterExporter(OTLPSpanAdapterExporter):
|
|
31
|
+
"""An OpenTelemetry OTLP span exporter with built-in redaction and privacy tagging.
|
|
32
|
+
|
|
33
|
+
This class extends OTLPSpanAdapterExporter to provide automatic span redaction
|
|
34
|
+
and privacy tagging capabilities. It automatically adds header-based redaction
|
|
35
|
+
and span tagging processors to the processing pipeline.
|
|
36
|
+
|
|
37
|
+
Key Features:
|
|
38
|
+
- Header-based span redaction with configurable callback logic
|
|
39
|
+
- Privacy level tagging for compliance and governance
|
|
40
|
+
- Complete span processing pipeline (IntermediateStep → Span → Redaction → Tagging → OtelSpan → Batching → Export)
|
|
41
|
+
- Batching support for efficient transmission
|
|
42
|
+
- OTLP HTTP protocol for maximum compatibility
|
|
43
|
+
- Configurable authentication via headers
|
|
44
|
+
- Resource attribute management
|
|
45
|
+
- Error handling and retry logic
|
|
46
|
+
|
|
47
|
+
The redaction processor allows conditional redaction based on authentication headers,
|
|
48
|
+
while the tagging processor adds privacy-level metadata to spans for downstream
|
|
49
|
+
processing and compliance tracking.
|
|
50
|
+
|
|
51
|
+
This exporter is commonly used with services like:
|
|
52
|
+
- OpenTelemetry Collector
|
|
53
|
+
- DataDog (OTLP endpoint)
|
|
54
|
+
- Jaeger (OTLP endpoint)
|
|
55
|
+
- Grafana Tempo
|
|
56
|
+
- Custom OTLP-compatible backends
|
|
57
|
+
|
|
58
|
+
Example::
|
|
59
|
+
|
|
60
|
+
def should_redact(auth_key: str) -> bool:
|
|
61
|
+
return auth_key in ["sensitive_user", "test_user"]
|
|
62
|
+
|
|
63
|
+
exporter = OTLPSpanRedactionAdapterExporter(
|
|
64
|
+
endpoint="https://api.service.com/v1/traces",
|
|
65
|
+
headers={"Authorization": "Bearer your-token"},
|
|
66
|
+
redaction_attributes=["user.email", "request.body"],
|
|
67
|
+
redaction_headers=["x-user-id"],
|
|
68
|
+
redaction_callback=should_redact,
|
|
69
|
+
redaction_value="REDACTED",
|
|
70
|
+
tags={"privacy.level": PrivacyLevel.HIGH, "service.type": "sensitive"},
|
|
71
|
+
batch_size=50,
|
|
72
|
+
flush_interval=10.0
|
|
73
|
+
)
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def __init__(
|
|
77
|
+
self,
|
|
78
|
+
*,
|
|
79
|
+
# OtelSpanExporter args
|
|
80
|
+
context_state: ContextState | None = None,
|
|
81
|
+
batch_size: int = 100,
|
|
82
|
+
flush_interval: float = 5.0,
|
|
83
|
+
max_queue_size: int = 1000,
|
|
84
|
+
drop_on_overflow: bool = False,
|
|
85
|
+
shutdown_timeout: float = 10.0,
|
|
86
|
+
resource_attributes: dict[str, str] | None = None,
|
|
87
|
+
# Redaction args
|
|
88
|
+
redaction_attributes: list[str] | None = None,
|
|
89
|
+
redaction_headers: list[str] | None = None,
|
|
90
|
+
redaction_callback: Callable[..., Any] | None = None,
|
|
91
|
+
redaction_enabled: bool = False,
|
|
92
|
+
force_redaction: bool = False,
|
|
93
|
+
redaction_value: str = "[REDACTED]",
|
|
94
|
+
redaction_tag: str | None = None,
|
|
95
|
+
tags: Mapping[str, Enum | str] | None = None,
|
|
96
|
+
# OTLPSpanExporterMixin args
|
|
97
|
+
endpoint: str,
|
|
98
|
+
headers: dict[str, str] | None = None,
|
|
99
|
+
**otlp_kwargs):
|
|
100
|
+
"""Initialize the OTLP span exporter with redaction and tagging capabilities.
|
|
101
|
+
|
|
102
|
+
Args:
|
|
103
|
+
context_state: The context state for the exporter.
|
|
104
|
+
batch_size: Number of spans to batch before exporting, default is 100.
|
|
105
|
+
flush_interval: Time in seconds between automatic batch flushes, default is 5.0.
|
|
106
|
+
max_queue_size: Maximum number of spans to queue, default is 1000.
|
|
107
|
+
drop_on_overflow: Whether to drop spans when queue is full, default is False.
|
|
108
|
+
shutdown_timeout: Maximum time to wait for export completion during shutdown, default is 10.0.
|
|
109
|
+
resource_attributes: Additional resource attributes for spans.
|
|
110
|
+
redaction_attributes: List of span attribute keys to redact when conditions are met.
|
|
111
|
+
redaction_headers: List of header keys to check for authentication/user identification.
|
|
112
|
+
redaction_callback: Function that returns true to redact spans based on header value, false otherwise.
|
|
113
|
+
redaction_enabled: Whether the redaction processor is enabled, default is False.
|
|
114
|
+
force_redaction: If True, always redact regardless of header checks, default is False.
|
|
115
|
+
redaction_value: Value to replace redacted attributes with, default is "[REDACTED]".
|
|
116
|
+
tags: Mapping of tag keys to their values (enums or strings) to add to spans.
|
|
117
|
+
redaction_tag: Tag to add to spans when redaction occurs.
|
|
118
|
+
endpoint: The endpoint for the OTLP service.
|
|
119
|
+
headers: The headers for the OTLP service.
|
|
120
|
+
otlp_kwargs: Additional keyword arguments for the OTLP service.
|
|
121
|
+
"""
|
|
122
|
+
super().__init__(context_state=context_state,
|
|
123
|
+
batch_size=batch_size,
|
|
124
|
+
flush_interval=flush_interval,
|
|
125
|
+
max_queue_size=max_queue_size,
|
|
126
|
+
drop_on_overflow=drop_on_overflow,
|
|
127
|
+
shutdown_timeout=shutdown_timeout,
|
|
128
|
+
resource_attributes=resource_attributes,
|
|
129
|
+
endpoint=endpoint,
|
|
130
|
+
headers=headers,
|
|
131
|
+
**otlp_kwargs)
|
|
132
|
+
|
|
133
|
+
# Insert redaction and tagging processors to the front of the processing pipeline
|
|
134
|
+
self.add_processor(SpanHeaderRedactionProcessor(attributes=redaction_attributes or [],
|
|
135
|
+
headers=redaction_headers or [],
|
|
136
|
+
callback=redaction_callback or (lambda _: False),
|
|
137
|
+
enabled=redaction_enabled,
|
|
138
|
+
force_redact=force_redaction,
|
|
139
|
+
redaction_value=redaction_value,
|
|
140
|
+
redaction_tag=redaction_tag),
|
|
141
|
+
name="header_redaction",
|
|
142
|
+
position=0)
|
|
143
|
+
|
|
144
|
+
self.add_processor(SpanTaggingProcessor(tags=tags), name="span_sensitivity_tagging", position=1)
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
#
|
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
# you may not use this file except in compliance with the License.
|
|
6
|
+
# You may obtain a copy of the License at
|
|
7
|
+
#
|
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
#
|
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
# See the License for the specific language governing permissions and
|
|
14
|
+
# limitations under the License.
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
import os
|
|
18
|
+
|
|
19
|
+
from pydantic import Field
|
|
20
|
+
|
|
21
|
+
from nat.builder.builder import Builder
|
|
22
|
+
from nat.cli.register_workflow import register_telemetry_exporter
|
|
23
|
+
from nat.data_models.telemetry_exporter import TelemetryExporterBaseConfig
|
|
24
|
+
from nat.observability.mixin.batch_config_mixin import BatchConfigMixin
|
|
25
|
+
from nat.observability.mixin.collector_config_mixin import CollectorConfigMixin
|
|
26
|
+
|
|
27
|
+
logger = logging.getLogger(__name__)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class LangfuseTelemetryExporter(BatchConfigMixin, TelemetryExporterBaseConfig, name="langfuse"):
|
|
31
|
+
"""A telemetry exporter to transmit traces to externally hosted langfuse service."""
|
|
32
|
+
|
|
33
|
+
endpoint: str = Field(description="The langfuse OTEL endpoint (/api/public/otel/v1/traces)")
|
|
34
|
+
public_key: str = Field(description="The Langfuse public key", default="")
|
|
35
|
+
secret_key: str = Field(description="The Langfuse secret key", default="")
|
|
36
|
+
resource_attributes: dict[str, str] = Field(default_factory=dict,
|
|
37
|
+
description="The resource attributes to add to the span")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@register_telemetry_exporter(config_type=LangfuseTelemetryExporter)
|
|
41
|
+
async def langfuse_telemetry_exporter(config: LangfuseTelemetryExporter, builder: Builder):
|
|
42
|
+
|
|
43
|
+
import base64
|
|
44
|
+
|
|
45
|
+
from nat.plugins.opentelemetry import OTLPSpanAdapterExporter
|
|
46
|
+
|
|
47
|
+
secret_key = config.secret_key or os.environ.get("LANGFUSE_SECRET_KEY")
|
|
48
|
+
public_key = config.public_key or os.environ.get("LANGFUSE_PUBLIC_KEY")
|
|
49
|
+
if not secret_key or not public_key:
|
|
50
|
+
raise ValueError("secret and public keys are required for langfuse")
|
|
51
|
+
|
|
52
|
+
credentials = f"{public_key}:{secret_key}".encode()
|
|
53
|
+
auth_header = base64.b64encode(credentials).decode("utf-8")
|
|
54
|
+
headers = {"Authorization": f"Basic {auth_header}"}
|
|
55
|
+
|
|
56
|
+
yield OTLPSpanAdapterExporter(endpoint=config.endpoint,
|
|
57
|
+
headers=headers,
|
|
58
|
+
batch_size=config.batch_size,
|
|
59
|
+
flush_interval=config.flush_interval,
|
|
60
|
+
max_queue_size=config.max_queue_size,
|
|
61
|
+
drop_on_overflow=config.drop_on_overflow,
|
|
62
|
+
shutdown_timeout=config.shutdown_timeout)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class LangsmithTelemetryExporter(BatchConfigMixin, CollectorConfigMixin, TelemetryExporterBaseConfig, name="langsmith"):
|
|
66
|
+
"""A telemetry exporter to transmit traces to externally hosted langsmith service."""
|
|
67
|
+
|
|
68
|
+
endpoint: str = Field(
|
|
69
|
+
description="The langsmith OTEL endpoint",
|
|
70
|
+
default="https://api.smith.langchain.com/otel/v1/traces",
|
|
71
|
+
)
|
|
72
|
+
api_key: str = Field(description="The Langsmith API key", default="")
|
|
73
|
+
resource_attributes: dict[str, str] = Field(default_factory=dict,
|
|
74
|
+
description="The resource attributes to add to the span")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@register_telemetry_exporter(config_type=LangsmithTelemetryExporter)
|
|
78
|
+
async def langsmith_telemetry_exporter(config: LangsmithTelemetryExporter, builder: Builder):
|
|
79
|
+
"""Create a Langsmith telemetry exporter."""
|
|
80
|
+
|
|
81
|
+
from nat.plugins.opentelemetry import OTLPSpanAdapterExporter
|
|
82
|
+
|
|
83
|
+
api_key = config.api_key or os.environ.get("LANGSMITH_API_KEY")
|
|
84
|
+
if not api_key:
|
|
85
|
+
raise ValueError("API key is required for langsmith")
|
|
86
|
+
|
|
87
|
+
headers = {"x-api-key": api_key, "Langsmith-Project": config.project}
|
|
88
|
+
yield OTLPSpanAdapterExporter(endpoint=config.endpoint,
|
|
89
|
+
headers=headers,
|
|
90
|
+
batch_size=config.batch_size,
|
|
91
|
+
flush_interval=config.flush_interval,
|
|
92
|
+
max_queue_size=config.max_queue_size,
|
|
93
|
+
drop_on_overflow=config.drop_on_overflow,
|
|
94
|
+
shutdown_timeout=config.shutdown_timeout)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class OtelCollectorTelemetryExporter(BatchConfigMixin,
|
|
98
|
+
CollectorConfigMixin,
|
|
99
|
+
TelemetryExporterBaseConfig,
|
|
100
|
+
name="otelcollector"):
|
|
101
|
+
"""A telemetry exporter to transmit traces to externally hosted otel collector service."""
|
|
102
|
+
|
|
103
|
+
resource_attributes: dict[str, str] = Field(default_factory=dict,
|
|
104
|
+
description="The resource attributes to add to the span")
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
@register_telemetry_exporter(config_type=OtelCollectorTelemetryExporter)
|
|
108
|
+
async def otel_telemetry_exporter(config: OtelCollectorTelemetryExporter, builder: Builder):
|
|
109
|
+
"""Create an OpenTelemetry telemetry exporter."""
|
|
110
|
+
|
|
111
|
+
from nat.plugins.opentelemetry import OTLPSpanAdapterExporter
|
|
112
|
+
from nat.plugins.opentelemetry.otel_span_exporter import get_opentelemetry_sdk_version
|
|
113
|
+
|
|
114
|
+
# Default resource attributes
|
|
115
|
+
default_resource_attributes = {
|
|
116
|
+
"telemetry.sdk.language": "python",
|
|
117
|
+
"telemetry.sdk.name": "opentelemetry",
|
|
118
|
+
"telemetry.sdk.version": get_opentelemetry_sdk_version(),
|
|
119
|
+
"service.name": config.project,
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
# Merge defaults with config, giving precedence to config
|
|
123
|
+
merged_resource_attributes = {**default_resource_attributes, **config.resource_attributes}
|
|
124
|
+
|
|
125
|
+
yield OTLPSpanAdapterExporter(endpoint=config.endpoint,
|
|
126
|
+
resource_attributes=merged_resource_attributes,
|
|
127
|
+
batch_size=config.batch_size,
|
|
128
|
+
flush_interval=config.flush_interval,
|
|
129
|
+
max_queue_size=config.max_queue_size,
|
|
130
|
+
drop_on_overflow=config.drop_on_overflow,
|
|
131
|
+
shutdown_timeout=config.shutdown_timeout)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class PatronusTelemetryExporter(BatchConfigMixin, CollectorConfigMixin, TelemetryExporterBaseConfig, name="patronus"):
|
|
135
|
+
"""A telemetry exporter to transmit traces to Patronus service."""
|
|
136
|
+
|
|
137
|
+
api_key: str = Field(description="The Patronus API key", default="")
|
|
138
|
+
resource_attributes: dict[str, str] = Field(default_factory=dict,
|
|
139
|
+
description="The resource attributes to add to the span")
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@register_telemetry_exporter(config_type=PatronusTelemetryExporter)
|
|
143
|
+
async def patronus_telemetry_exporter(config: PatronusTelemetryExporter, builder: Builder):
|
|
144
|
+
"""Create a Patronus telemetry exporter."""
|
|
145
|
+
|
|
146
|
+
from nat.plugins.opentelemetry import OTLPSpanAdapterExporter
|
|
147
|
+
|
|
148
|
+
api_key = config.api_key or os.environ.get("PATRONUS_API_KEY")
|
|
149
|
+
if not api_key:
|
|
150
|
+
raise ValueError("API key is required for Patronus")
|
|
151
|
+
|
|
152
|
+
headers = {
|
|
153
|
+
"x-api-key": api_key,
|
|
154
|
+
"pat-project-name": config.project,
|
|
155
|
+
}
|
|
156
|
+
yield OTLPSpanAdapterExporter(endpoint=config.endpoint,
|
|
157
|
+
headers=headers,
|
|
158
|
+
batch_size=config.batch_size,
|
|
159
|
+
flush_interval=config.flush_interval,
|
|
160
|
+
max_queue_size=config.max_queue_size,
|
|
161
|
+
drop_on_overflow=config.drop_on_overflow,
|
|
162
|
+
shutdown_timeout=config.shutdown_timeout)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
class GalileoTelemetryExporter(BatchConfigMixin, CollectorConfigMixin, TelemetryExporterBaseConfig, name="galileo"):
|
|
166
|
+
"""A telemetry exporter to transmit traces to externally hosted galileo service."""
|
|
167
|
+
|
|
168
|
+
endpoint: str = Field(description="The galileo endpoint to export telemetry traces.",
|
|
169
|
+
default="https://app.galileo.ai/api/galileo/otel/traces")
|
|
170
|
+
logstream: str = Field(description="The logstream name to group the telemetry traces.")
|
|
171
|
+
api_key: str = Field(description="The api key to authenticate with the galileo service.")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@register_telemetry_exporter(config_type=GalileoTelemetryExporter)
|
|
175
|
+
async def galileo_telemetry_exporter(config: GalileoTelemetryExporter, builder: Builder):
|
|
176
|
+
"""Create a Galileo telemetry exporter."""
|
|
177
|
+
|
|
178
|
+
from nat.plugins.opentelemetry import OTLPSpanAdapterExporter
|
|
179
|
+
|
|
180
|
+
headers = {
|
|
181
|
+
"Galileo-API-Key": config.api_key,
|
|
182
|
+
"logstream": config.logstream,
|
|
183
|
+
"project": config.project,
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
yield OTLPSpanAdapterExporter(
|
|
187
|
+
endpoint=config.endpoint,
|
|
188
|
+
headers=headers,
|
|
189
|
+
batch_size=config.batch_size,
|
|
190
|
+
flush_interval=config.flush_interval,
|
|
191
|
+
max_queue_size=config.max_queue_size,
|
|
192
|
+
drop_on_overflow=config.drop_on_overflow,
|
|
193
|
+
shutdown_timeout=config.shutdown_timeout,
|
|
194
|
+
)
|