-
Notifications
You must be signed in to change notification settings - Fork 30
Feat/semconv #224
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
base: main
Are you sure you want to change the base?
Feat/semconv #224
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -469,6 +469,8 @@ def _create_invocation_from_generation( | |
|
|
||
| invocation = LLMInvocation(request_model=request_model) | ||
| invocation.provider = "dashscope" | ||
|
Collaborator
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. [Info] |
||
| invocation.server_address = "dashscope.aliyuncs.com" | ||
| invocation.server_port = 443 | ||
| invocation.input_messages = _extract_input_messages(kwargs) | ||
|
|
||
| # Extract tool definitions and convert to FunctionToolDefinition objects | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -571,6 +571,7 @@ def create_agent_invocation( | |
| invocation = InvokeAgentInvocation( | ||
| provider=_HERMES_AGENT_SYSTEM, | ||
| agent_name="Hermes", | ||
| agent_id="hermes-agent", | ||
|
Collaborator
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. [Warning] |
||
| conversation_id=getattr(instance, "session_id", None), | ||
| request_model=getattr(instance, "model", None), | ||
| input_messages=[ | ||
|
|
@@ -609,6 +610,25 @@ def should_create_entry_for_agent(instance: Any) -> bool: | |
| return bool(_entry_platform(instance)) | ||
|
|
||
|
|
||
| def _extract_server_info(instance: Any) -> tuple[str | None, int | None]: | ||
| """Extract server address and port from instance base_url.""" | ||
| base_url = getattr(instance, "base_url", None) | ||
| if not base_url: | ||
| return None, None | ||
| base_url_str = str(base_url).rstrip("/") | ||
| try: | ||
| from urllib.parse import urlparse | ||
|
|
||
| parsed = urlparse(base_url_str) | ||
| address = parsed.hostname | ||
| port = parsed.port | ||
| if port is None: | ||
| port = 443 if parsed.scheme == "https" else 80 | ||
| return address, port | ||
| except Exception: | ||
| return None, None | ||
|
|
||
|
Comment on lines
+613
to
+630
|
||
|
|
||
| def create_llm_invocation(instance: Any, api_kwargs: Any) -> LLMInvocation: | ||
| if not isinstance(api_kwargs, dict): | ||
| api_kwargs = {} | ||
|
|
@@ -620,6 +640,8 @@ def create_llm_invocation(instance: Any, api_kwargs: Any) -> LLMInvocation: | |
| if max_tokens is None: | ||
| max_tokens = api_kwargs.get("max_output_tokens") | ||
|
|
||
| server_address, server_port = _extract_server_info(instance) | ||
|
|
||
| invocation = LLMInvocation( | ||
| provider=provider_name(instance), | ||
| request_model=request_model or None, | ||
|
|
@@ -633,6 +655,8 @@ def create_llm_invocation(instance: Any, api_kwargs: Any) -> LLMInvocation: | |
| presence_penalty=api_kwargs.get("presence_penalty"), | ||
| seed=api_kwargs.get("seed"), | ||
| stop_sequences=api_kwargs.get("stop"), | ||
| server_address=server_address, | ||
| server_port=server_port, | ||
| ) | ||
| return invocation | ||
|
|
||
|
|
@@ -683,6 +707,7 @@ def create_tool_invocation( | |
| tool_name=tool_name, | ||
| provider=provider or _HERMES_AGENT_SYSTEM, | ||
| ) | ||
| invocation.tool_type = "function" | ||
| if invocation.provider: | ||
| invocation.attributes["gen_ai.provider.name"] = invocation.provider | ||
| if arguments is not None: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,12 +24,23 @@ | |
| from opentelemetry.sdk._configuration import _OTelSDKConfigurator | ||
| from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_PROTOCOL | ||
|
|
||
| from loongsuite.distro.resource import LoongSuiteResourceDetector | ||
|
|
||
|
|
||
| class LoongSuiteConfigurator(_OTelSDKConfigurator): | ||
| """ | ||
| LoongSuite configurator, inherits from OpenTelemetry SDK configurator. | ||
|
|
||
| Augments the resource with LoongSuite specific attributes (``host.ip`` and | ||
| ``gen_ai.instrumentation.sdk.name``) before delegating to the OpenTelemetry | ||
| SDK configurator. | ||
| """ | ||
|
|
||
| def _configure(self, **kwargs: Any) -> None: | ||
| resource_attributes = dict(kwargs.pop("resource_attributes", None) or {}) | ||
|
Collaborator
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. [Warning] The resource_attributes = dict(kwargs.pop("resource_attributes", None) or {})
# User values set first, detector only fills gaps:
detected = LoongSuiteResourceDetector().detect().attributes
for k, v in detected.items():
resource_attributes.setdefault(k, v) |
||
| resource_attributes.update(LoongSuiteResourceDetector().detect().attributes) | ||
| super()._configure(resource_attributes=resource_attributes, **kwargs) | ||
|
|
||
|
|
||
| class LoongSuiteDistro(BaseDistro): | ||
| """ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import socket | ||
| from logging import getLogger | ||
|
|
||
| from opentelemetry.sdk.resources import Resource, ResourceDetector | ||
|
|
||
| logger = getLogger(__name__) | ||
|
|
||
| # Resource attribute keys contributed by LoongSuite. | ||
| HOST_IP = "host.ip" | ||
| GEN_AI_INSTRUMENTATION_SDK_NAME = "gen_ai.instrumentation.sdk.name" | ||
|
|
||
| # Fixed value identifying the GenAI instrumentation SDK shipped with LoongSuite. | ||
| _GEN_AI_INSTRUMENTATION_SDK_NAME_VALUE = "loongsuite-genai-utils" | ||
|
|
||
| _FALLBACK_HOST_IP = "127.0.0.1" | ||
|
|
||
|
|
||
| def _get_host_ip() -> str: | ||
| """Best-effort detection of the local host IP address. | ||
| Opens a UDP socket towards a public address to discover which local | ||
| interface would be used for outbound traffic. No packet is actually sent. | ||
| Falls back to ``127.0.0.1`` when detection fails. | ||
| """ | ||
| sock = None | ||
| try: | ||
| sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
| sock.connect(("8.8.8.8", 80)) | ||
| return sock.getsockname()[0] | ||
| except OSError as exception: | ||
| logger.debug( | ||
| "Failed to detect host ip, falling back to %s. Exception: %s", | ||
| _FALLBACK_HOST_IP, | ||
| exception, | ||
| ) | ||
| return _FALLBACK_HOST_IP | ||
| finally: | ||
| if sock is not None: | ||
| sock.close() | ||
|
|
||
|
|
||
| def _get_host_ip_with_pid() -> str: | ||
| """Returns the host IP combined with the current process id. | ||
| The format is ``<ip>-<pid>``, for example ``127.0.0.1-1``. | ||
| """ | ||
| return f"{_get_host_ip()}-{os.getpid()}" | ||
|
|
||
|
|
||
| class LoongSuiteResourceDetector(ResourceDetector): | ||
| """Detects LoongSuite specific resource attributes. | ||
| Contributes the following attributes to the resource: | ||
| * ``host.ip`` formatted as ``<ip>-<pid>`` (e.g. ``127.0.0.1-1``). | ||
| * ``gen_ai.instrumentation.sdk.name`` set to ``loongsuite-genai-utils``. | ||
| """ | ||
|
|
||
| def detect(self) -> Resource: | ||
| return Resource( | ||
| { | ||
| HOST_IP: _get_host_ip_with_pid(), | ||
| GEN_AI_INSTRUMENTATION_SDK_NAME: _GEN_AI_INSTRUMENTATION_SDK_NAME_VALUE, | ||
| } | ||
|
Comment on lines
+74
to
+79
|
||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Info]
tool_typeis unconditionally set to"function"here and across all other instrumentations in this PR. Some agent frameworks support non-function tool types (e.g.,code_interpreter,retrieval,builtin). If the framework exposes the actual tool type, consider deriving it rather than hardcoding. If"function"is the only supported type today, a comment documenting this assumption would help future maintainers.