agentrun-sdk 0.1.2__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.
Potentially problematic release.
This version of agentrun-sdk might be problematic. Click here for more details.
- agentrun_operation_sdk/cli/__init__.py +1 -0
- agentrun_operation_sdk/cli/cli.py +19 -0
- agentrun_operation_sdk/cli/common.py +21 -0
- agentrun_operation_sdk/cli/runtime/__init__.py +1 -0
- agentrun_operation_sdk/cli/runtime/commands.py +203 -0
- agentrun_operation_sdk/client/client.py +75 -0
- agentrun_operation_sdk/operations/runtime/__init__.py +8 -0
- agentrun_operation_sdk/operations/runtime/configure.py +101 -0
- agentrun_operation_sdk/operations/runtime/launch.py +82 -0
- agentrun_operation_sdk/operations/runtime/models.py +31 -0
- agentrun_operation_sdk/services/runtime.py +152 -0
- agentrun_operation_sdk/utils/logging_config.py +72 -0
- agentrun_operation_sdk/utils/runtime/config.py +94 -0
- agentrun_operation_sdk/utils/runtime/container.py +280 -0
- agentrun_operation_sdk/utils/runtime/entrypoint.py +203 -0
- agentrun_operation_sdk/utils/runtime/schema.py +56 -0
- agentrun_sdk/__init__.py +7 -0
- agentrun_sdk/agent/__init__.py +25 -0
- agentrun_sdk/agent/agent.py +696 -0
- agentrun_sdk/agent/agent_result.py +46 -0
- agentrun_sdk/agent/conversation_manager/__init__.py +26 -0
- agentrun_sdk/agent/conversation_manager/conversation_manager.py +88 -0
- agentrun_sdk/agent/conversation_manager/null_conversation_manager.py +46 -0
- agentrun_sdk/agent/conversation_manager/sliding_window_conversation_manager.py +179 -0
- agentrun_sdk/agent/conversation_manager/summarizing_conversation_manager.py +252 -0
- agentrun_sdk/agent/state.py +97 -0
- agentrun_sdk/event_loop/__init__.py +9 -0
- agentrun_sdk/event_loop/event_loop.py +499 -0
- agentrun_sdk/event_loop/streaming.py +319 -0
- agentrun_sdk/experimental/__init__.py +4 -0
- agentrun_sdk/experimental/hooks/__init__.py +15 -0
- agentrun_sdk/experimental/hooks/events.py +123 -0
- agentrun_sdk/handlers/__init__.py +10 -0
- agentrun_sdk/handlers/callback_handler.py +70 -0
- agentrun_sdk/hooks/__init__.py +49 -0
- agentrun_sdk/hooks/events.py +80 -0
- agentrun_sdk/hooks/registry.py +247 -0
- agentrun_sdk/models/__init__.py +10 -0
- agentrun_sdk/models/anthropic.py +432 -0
- agentrun_sdk/models/bedrock.py +649 -0
- agentrun_sdk/models/litellm.py +225 -0
- agentrun_sdk/models/llamaapi.py +438 -0
- agentrun_sdk/models/mistral.py +539 -0
- agentrun_sdk/models/model.py +95 -0
- agentrun_sdk/models/ollama.py +357 -0
- agentrun_sdk/models/openai.py +436 -0
- agentrun_sdk/models/sagemaker.py +598 -0
- agentrun_sdk/models/writer.py +449 -0
- agentrun_sdk/multiagent/__init__.py +22 -0
- agentrun_sdk/multiagent/a2a/__init__.py +15 -0
- agentrun_sdk/multiagent/a2a/executor.py +148 -0
- agentrun_sdk/multiagent/a2a/server.py +252 -0
- agentrun_sdk/multiagent/base.py +92 -0
- agentrun_sdk/multiagent/graph.py +555 -0
- agentrun_sdk/multiagent/swarm.py +656 -0
- agentrun_sdk/py.typed +1 -0
- agentrun_sdk/session/__init__.py +18 -0
- agentrun_sdk/session/file_session_manager.py +216 -0
- agentrun_sdk/session/repository_session_manager.py +152 -0
- agentrun_sdk/session/s3_session_manager.py +272 -0
- agentrun_sdk/session/session_manager.py +73 -0
- agentrun_sdk/session/session_repository.py +51 -0
- agentrun_sdk/telemetry/__init__.py +21 -0
- agentrun_sdk/telemetry/config.py +194 -0
- agentrun_sdk/telemetry/metrics.py +476 -0
- agentrun_sdk/telemetry/metrics_constants.py +15 -0
- agentrun_sdk/telemetry/tracer.py +563 -0
- agentrun_sdk/tools/__init__.py +17 -0
- agentrun_sdk/tools/decorator.py +569 -0
- agentrun_sdk/tools/executor.py +137 -0
- agentrun_sdk/tools/loader.py +152 -0
- agentrun_sdk/tools/mcp/__init__.py +13 -0
- agentrun_sdk/tools/mcp/mcp_agent_tool.py +99 -0
- agentrun_sdk/tools/mcp/mcp_client.py +423 -0
- agentrun_sdk/tools/mcp/mcp_instrumentation.py +322 -0
- agentrun_sdk/tools/mcp/mcp_types.py +63 -0
- agentrun_sdk/tools/registry.py +607 -0
- agentrun_sdk/tools/structured_output.py +421 -0
- agentrun_sdk/tools/tools.py +217 -0
- agentrun_sdk/tools/watcher.py +136 -0
- agentrun_sdk/types/__init__.py +5 -0
- agentrun_sdk/types/collections.py +23 -0
- agentrun_sdk/types/content.py +188 -0
- agentrun_sdk/types/event_loop.py +48 -0
- agentrun_sdk/types/exceptions.py +81 -0
- agentrun_sdk/types/guardrails.py +254 -0
- agentrun_sdk/types/media.py +89 -0
- agentrun_sdk/types/session.py +152 -0
- agentrun_sdk/types/streaming.py +201 -0
- agentrun_sdk/types/tools.py +258 -0
- agentrun_sdk/types/traces.py +5 -0
- agentrun_sdk-0.1.2.dist-info/METADATA +51 -0
- agentrun_sdk-0.1.2.dist-info/RECORD +115 -0
- agentrun_sdk-0.1.2.dist-info/WHEEL +5 -0
- agentrun_sdk-0.1.2.dist-info/entry_points.txt +2 -0
- agentrun_sdk-0.1.2.dist-info/top_level.txt +3 -0
- agentrun_wrapper/__init__.py +11 -0
- agentrun_wrapper/_utils/__init__.py +6 -0
- agentrun_wrapper/_utils/endpoints.py +16 -0
- agentrun_wrapper/identity/__init__.py +5 -0
- agentrun_wrapper/identity/auth.py +211 -0
- agentrun_wrapper/memory/__init__.py +6 -0
- agentrun_wrapper/memory/client.py +1697 -0
- agentrun_wrapper/memory/constants.py +103 -0
- agentrun_wrapper/memory/controlplane.py +626 -0
- agentrun_wrapper/py.typed +1 -0
- agentrun_wrapper/runtime/__init__.py +13 -0
- agentrun_wrapper/runtime/app.py +473 -0
- agentrun_wrapper/runtime/context.py +34 -0
- agentrun_wrapper/runtime/models.py +25 -0
- agentrun_wrapper/services/__init__.py +1 -0
- agentrun_wrapper/services/identity.py +192 -0
- agentrun_wrapper/tools/__init__.py +6 -0
- agentrun_wrapper/tools/browser_client.py +325 -0
- agentrun_wrapper/tools/code_interpreter_client.py +186 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Session manager interface for agent session management."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import TYPE_CHECKING, Any
|
|
5
|
+
|
|
6
|
+
from ..hooks.events import AfterInvocationEvent, AgentInitializedEvent, MessageAddedEvent
|
|
7
|
+
from ..hooks.registry import HookProvider, HookRegistry
|
|
8
|
+
from ..types.content import Message
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from ..agent.agent import Agent
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SessionManager(HookProvider, ABC):
|
|
15
|
+
"""Abstract interface for managing sessions.
|
|
16
|
+
|
|
17
|
+
A session manager is in charge of persisting the conversation and state of an agent across its interaction.
|
|
18
|
+
Changes made to the agents conversation, state, or other attributes should be persisted immediately after
|
|
19
|
+
they are changed. The different methods introduced in this class are called at important lifecycle events
|
|
20
|
+
for an agent, and should be persisted in the session.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def register_hooks(self, registry: HookRegistry, **kwargs: Any) -> None:
|
|
24
|
+
"""Register hooks for persisting the agent to the session."""
|
|
25
|
+
# After the normal Agent initialization behavior, call the session initialize function to restore the agent
|
|
26
|
+
registry.add_callback(AgentInitializedEvent, lambda event: self.initialize(event.agent))
|
|
27
|
+
|
|
28
|
+
# For each message appended to the Agents messages, store that message in the session
|
|
29
|
+
registry.add_callback(MessageAddedEvent, lambda event: self.append_message(event.message, event.agent))
|
|
30
|
+
|
|
31
|
+
# Sync the agent into the session for each message in case the agent state was updated
|
|
32
|
+
registry.add_callback(MessageAddedEvent, lambda event: self.sync_agent(event.agent))
|
|
33
|
+
|
|
34
|
+
# After an agent was invoked, sync it with the session to capture any conversation manager state updates
|
|
35
|
+
registry.add_callback(AfterInvocationEvent, lambda event: self.sync_agent(event.agent))
|
|
36
|
+
|
|
37
|
+
@abstractmethod
|
|
38
|
+
def redact_latest_message(self, redact_message: Message, agent: "Agent", **kwargs: Any) -> None:
|
|
39
|
+
"""Redact the message most recently appended to the agent in the session.
|
|
40
|
+
|
|
41
|
+
Args:
|
|
42
|
+
redact_message: New message to use that contains the redact content
|
|
43
|
+
agent: Agent to apply the message redaction to
|
|
44
|
+
**kwargs: Additional keyword arguments for future extensibility.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
@abstractmethod
|
|
48
|
+
def append_message(self, message: Message, agent: "Agent", **kwargs: Any) -> None:
|
|
49
|
+
"""Append a message to the agent's session.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
message: Message to add to the agent in the session
|
|
53
|
+
agent: Agent to append the message to
|
|
54
|
+
**kwargs: Additional keyword arguments for future extensibility.
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
@abstractmethod
|
|
58
|
+
def sync_agent(self, agent: "Agent", **kwargs: Any) -> None:
|
|
59
|
+
"""Serialize and sync the agent with the session storage.
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
agent: Agent who should be synchronized with the session storage
|
|
63
|
+
**kwargs: Additional keyword arguments for future extensibility.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
@abstractmethod
|
|
67
|
+
def initialize(self, agent: "Agent", **kwargs: Any) -> None:
|
|
68
|
+
"""Initialize an agent with a session.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
agent: Agent to initialize
|
|
72
|
+
**kwargs: Additional keyword arguments for future extensibility.
|
|
73
|
+
"""
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Session repository interface for agent session management."""
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import Any, Optional
|
|
5
|
+
|
|
6
|
+
from ..types.session import Session, SessionAgent, SessionMessage
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SessionRepository(ABC):
|
|
10
|
+
"""Abstract repository for creating, reading, and updating Sessions, AgentSessions, and AgentMessages."""
|
|
11
|
+
|
|
12
|
+
@abstractmethod
|
|
13
|
+
def create_session(self, session: Session, **kwargs: Any) -> Session:
|
|
14
|
+
"""Create a new Session."""
|
|
15
|
+
|
|
16
|
+
@abstractmethod
|
|
17
|
+
def read_session(self, session_id: str, **kwargs: Any) -> Optional[Session]:
|
|
18
|
+
"""Read a Session."""
|
|
19
|
+
|
|
20
|
+
@abstractmethod
|
|
21
|
+
def create_agent(self, session_id: str, session_agent: SessionAgent, **kwargs: Any) -> None:
|
|
22
|
+
"""Create a new Agent in a Session."""
|
|
23
|
+
|
|
24
|
+
@abstractmethod
|
|
25
|
+
def read_agent(self, session_id: str, agent_id: str, **kwargs: Any) -> Optional[SessionAgent]:
|
|
26
|
+
"""Read an Agent."""
|
|
27
|
+
|
|
28
|
+
@abstractmethod
|
|
29
|
+
def update_agent(self, session_id: str, session_agent: SessionAgent, **kwargs: Any) -> None:
|
|
30
|
+
"""Update an Agent."""
|
|
31
|
+
|
|
32
|
+
@abstractmethod
|
|
33
|
+
def create_message(self, session_id: str, agent_id: str, session_message: SessionMessage, **kwargs: Any) -> None:
|
|
34
|
+
"""Create a new Message for the Agent."""
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def read_message(self, session_id: str, agent_id: str, message_id: int, **kwargs: Any) -> Optional[SessionMessage]:
|
|
38
|
+
"""Read a Message."""
|
|
39
|
+
|
|
40
|
+
@abstractmethod
|
|
41
|
+
def update_message(self, session_id: str, agent_id: str, session_message: SessionMessage, **kwargs: Any) -> None:
|
|
42
|
+
"""Update a Message.
|
|
43
|
+
|
|
44
|
+
A message is usually only updated when some content is redacted due to a guardrail.
|
|
45
|
+
"""
|
|
46
|
+
|
|
47
|
+
@abstractmethod
|
|
48
|
+
def list_messages(
|
|
49
|
+
self, session_id: str, agent_id: str, limit: Optional[int] = None, offset: int = 0, **kwargs: Any
|
|
50
|
+
) -> list[SessionMessage]:
|
|
51
|
+
"""List Messages from an Agent with pagination."""
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Telemetry module.
|
|
2
|
+
|
|
3
|
+
This module provides metrics and tracing functionality.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from .config import StrandsTelemetry
|
|
7
|
+
from .metrics import EventLoopMetrics, MetricsClient, Trace, metrics_to_string
|
|
8
|
+
from .tracer import Tracer, get_tracer
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
# Metrics
|
|
12
|
+
"EventLoopMetrics",
|
|
13
|
+
"Trace",
|
|
14
|
+
"metrics_to_string",
|
|
15
|
+
"MetricsClient",
|
|
16
|
+
# Tracer
|
|
17
|
+
"Tracer",
|
|
18
|
+
"get_tracer",
|
|
19
|
+
# Telemetry Setup
|
|
20
|
+
"StrandsTelemetry",
|
|
21
|
+
]
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""OpenTelemetry configuration and setup utilities for Strands agents.
|
|
2
|
+
|
|
3
|
+
This module provides centralized configuration and initialization functionality
|
|
4
|
+
for OpenTelemetry components and other telemetry infrastructure shared across Strands applications.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import logging
|
|
8
|
+
from importlib.metadata import version
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import opentelemetry.metrics as metrics_api
|
|
12
|
+
import opentelemetry.sdk.metrics as metrics_sdk
|
|
13
|
+
import opentelemetry.trace as trace_api
|
|
14
|
+
from opentelemetry import propagate
|
|
15
|
+
from opentelemetry.baggage.propagation import W3CBaggagePropagator
|
|
16
|
+
from opentelemetry.propagators.composite import CompositePropagator
|
|
17
|
+
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, PeriodicExportingMetricReader
|
|
18
|
+
from opentelemetry.sdk.resources import Resource
|
|
19
|
+
from opentelemetry.sdk.trace import TracerProvider as SDKTracerProvider
|
|
20
|
+
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter, SimpleSpanProcessor
|
|
21
|
+
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
|
|
22
|
+
|
|
23
|
+
logger = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def get_otel_resource() -> Resource:
|
|
27
|
+
"""Create a standard OpenTelemetry resource with service information.
|
|
28
|
+
|
|
29
|
+
Returns:
|
|
30
|
+
Resource object with standard service information.
|
|
31
|
+
"""
|
|
32
|
+
resource = Resource.create(
|
|
33
|
+
{
|
|
34
|
+
"service.name": "strands-agents",
|
|
35
|
+
"service.version": version("strands-agents"),
|
|
36
|
+
"telemetry.sdk.name": "opentelemetry",
|
|
37
|
+
"telemetry.sdk.language": "python",
|
|
38
|
+
}
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
return resource
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class StrandsTelemetry:
|
|
45
|
+
"""OpenTelemetry configuration and setup for Strands applications.
|
|
46
|
+
|
|
47
|
+
Automatically initializes a tracer provider with text map propagators.
|
|
48
|
+
Trace exporters (console, OTLP) can be set up individually using dedicated methods
|
|
49
|
+
that support method chaining for convenient configuration.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
tracer_provider: Optional pre-configured SDKTracerProvider. If None,
|
|
53
|
+
a new one will be created and set as the global tracer provider.
|
|
54
|
+
|
|
55
|
+
Environment Variables:
|
|
56
|
+
Environment variables are handled by the underlying OpenTelemetry SDK:
|
|
57
|
+
- OTEL_EXPORTER_OTLP_ENDPOINT: OTLP endpoint URL
|
|
58
|
+
- OTEL_EXPORTER_OTLP_HEADERS: Headers for OTLP requests
|
|
59
|
+
|
|
60
|
+
Examples:
|
|
61
|
+
Quick setup with method chaining:
|
|
62
|
+
>>> StrandsTelemetry().setup_console_exporter().setup_otlp_exporter()
|
|
63
|
+
|
|
64
|
+
Using a custom tracer provider:
|
|
65
|
+
>>> StrandsTelemetry(tracer_provider=my_provider).setup_console_exporter()
|
|
66
|
+
|
|
67
|
+
Step-by-step configuration:
|
|
68
|
+
>>> telemetry = StrandsTelemetry()
|
|
69
|
+
>>> telemetry.setup_console_exporter()
|
|
70
|
+
>>> telemetry.setup_otlp_exporter()
|
|
71
|
+
|
|
72
|
+
To setup global meter provider
|
|
73
|
+
>>> telemetry.setup_meter(enable_console_exporter=True, enable_otlp_exporter=True) # default are False
|
|
74
|
+
|
|
75
|
+
Note:
|
|
76
|
+
- The tracer provider is automatically initialized upon instantiation
|
|
77
|
+
- When no tracer_provider is provided, the instance sets itself as the global provider
|
|
78
|
+
- Exporters must be explicitly configured using the setup methods
|
|
79
|
+
- Failed exporter configurations are logged but do not raise exceptions
|
|
80
|
+
- All setup methods return self to enable method chaining
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
def __init__(
|
|
84
|
+
self,
|
|
85
|
+
tracer_provider: SDKTracerProvider | None = None,
|
|
86
|
+
) -> None:
|
|
87
|
+
"""Initialize the StrandsTelemetry instance.
|
|
88
|
+
|
|
89
|
+
Args:
|
|
90
|
+
tracer_provider: Optional pre-configured tracer provider.
|
|
91
|
+
If None, a new one will be created and set as global.
|
|
92
|
+
|
|
93
|
+
The instance is ready to use immediately after initialization, though
|
|
94
|
+
trace exporters must be configured separately using the setup methods.
|
|
95
|
+
"""
|
|
96
|
+
self.resource = get_otel_resource()
|
|
97
|
+
if tracer_provider:
|
|
98
|
+
self.tracer_provider = tracer_provider
|
|
99
|
+
else:
|
|
100
|
+
self._initialize_tracer()
|
|
101
|
+
|
|
102
|
+
def _initialize_tracer(self) -> None:
|
|
103
|
+
"""Initialize the OpenTelemetry tracer."""
|
|
104
|
+
logger.info("Initializing tracer")
|
|
105
|
+
|
|
106
|
+
# Create tracer provider
|
|
107
|
+
self.tracer_provider = SDKTracerProvider(resource=self.resource)
|
|
108
|
+
|
|
109
|
+
# Set as global tracer provider
|
|
110
|
+
trace_api.set_tracer_provider(self.tracer_provider)
|
|
111
|
+
|
|
112
|
+
# Set up propagators
|
|
113
|
+
propagate.set_global_textmap(
|
|
114
|
+
CompositePropagator(
|
|
115
|
+
[
|
|
116
|
+
W3CBaggagePropagator(),
|
|
117
|
+
TraceContextTextMapPropagator(),
|
|
118
|
+
]
|
|
119
|
+
)
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
def setup_console_exporter(self, **kwargs: Any) -> "StrandsTelemetry":
|
|
123
|
+
"""Set up console exporter for the tracer provider.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
**kwargs: Optional keyword arguments passed directly to
|
|
127
|
+
OpenTelemetry's ConsoleSpanExporter initializer.
|
|
128
|
+
|
|
129
|
+
Returns:
|
|
130
|
+
self: Enables method chaining.
|
|
131
|
+
|
|
132
|
+
This method configures a SimpleSpanProcessor with a ConsoleSpanExporter,
|
|
133
|
+
allowing trace data to be output to the console. Any additional keyword
|
|
134
|
+
arguments provided will be forwarded to the ConsoleSpanExporter.
|
|
135
|
+
"""
|
|
136
|
+
try:
|
|
137
|
+
logger.info("Enabling console export")
|
|
138
|
+
console_processor = SimpleSpanProcessor(ConsoleSpanExporter(**kwargs))
|
|
139
|
+
self.tracer_provider.add_span_processor(console_processor)
|
|
140
|
+
except Exception as e:
|
|
141
|
+
logger.exception("error=<%s> | Failed to configure console exporter", e)
|
|
142
|
+
return self
|
|
143
|
+
|
|
144
|
+
def setup_otlp_exporter(self, **kwargs: Any) -> "StrandsTelemetry":
|
|
145
|
+
"""Set up OTLP exporter for the tracer provider.
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
**kwargs: Optional keyword arguments passed directly to
|
|
149
|
+
OpenTelemetry's OTLPSpanExporter initializer.
|
|
150
|
+
|
|
151
|
+
Returns:
|
|
152
|
+
self: Enables method chaining.
|
|
153
|
+
|
|
154
|
+
This method configures a BatchSpanProcessor with an OTLPSpanExporter,
|
|
155
|
+
allowing trace data to be exported to an OTLP endpoint. Any additional
|
|
156
|
+
keyword arguments provided will be forwarded to the OTLPSpanExporter.
|
|
157
|
+
"""
|
|
158
|
+
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
|
|
159
|
+
|
|
160
|
+
try:
|
|
161
|
+
otlp_exporter = OTLPSpanExporter(**kwargs)
|
|
162
|
+
batch_processor = BatchSpanProcessor(otlp_exporter)
|
|
163
|
+
self.tracer_provider.add_span_processor(batch_processor)
|
|
164
|
+
logger.info("OTLP exporter configured")
|
|
165
|
+
except Exception as e:
|
|
166
|
+
logger.exception("error=<%s> | Failed to configure OTLP exporter", e)
|
|
167
|
+
return self
|
|
168
|
+
|
|
169
|
+
def setup_meter(
|
|
170
|
+
self, enable_console_exporter: bool = False, enable_otlp_exporter: bool = False
|
|
171
|
+
) -> "StrandsTelemetry":
|
|
172
|
+
"""Initialize the OpenTelemetry Meter."""
|
|
173
|
+
logger.info("Initializing meter")
|
|
174
|
+
metrics_readers = []
|
|
175
|
+
try:
|
|
176
|
+
if enable_console_exporter:
|
|
177
|
+
logger.info("Enabling console metrics exporter")
|
|
178
|
+
console_reader = PeriodicExportingMetricReader(ConsoleMetricExporter())
|
|
179
|
+
metrics_readers.append(console_reader)
|
|
180
|
+
if enable_otlp_exporter:
|
|
181
|
+
logger.info("Enabling OTLP metrics exporter")
|
|
182
|
+
from opentelemetry.exporter.otlp.proto.http.metric_exporter import OTLPMetricExporter
|
|
183
|
+
|
|
184
|
+
otlp_reader = PeriodicExportingMetricReader(OTLPMetricExporter())
|
|
185
|
+
metrics_readers.append(otlp_reader)
|
|
186
|
+
except Exception as e:
|
|
187
|
+
logger.exception("error=<%s> | Failed to configure OTLP metrics exporter", e)
|
|
188
|
+
|
|
189
|
+
self.meter_provider = metrics_sdk.MeterProvider(resource=self.resource, metric_readers=metrics_readers)
|
|
190
|
+
|
|
191
|
+
# Set as global tracer provider
|
|
192
|
+
metrics_api.set_meter_provider(self.meter_provider)
|
|
193
|
+
logger.info("Strands Meter configured")
|
|
194
|
+
return self
|