Skip to content

Commit 731054f

Browse files
authored
Add server attributes to Vertex AI spans (#3208)
1 parent dd68241 commit 731054f

File tree

5 files changed

+70
-1
lines changed

5 files changed

+70
-1
lines changed

instrumentation-genai/opentelemetry-instrumentation-vertexai/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
([#3192](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3192))
1212
- Initial VertexAI instrumentation
1313
([#3123](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3123))
14+
- Add server attributes to Vertex AI spans
15+
([#3208](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3208))

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/patch.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from opentelemetry.instrumentation.vertexai.utils import (
2626
GenerateContentParams,
2727
get_genai_request_attributes,
28+
get_server_attributes,
2829
get_span_name,
2930
)
3031
from opentelemetry.trace import SpanKind, Tracer
@@ -100,7 +101,11 @@ def traced_method(
100101
kwargs: Any,
101102
):
102103
params = _extract_params(*args, **kwargs)
103-
span_attributes = get_genai_request_attributes(params)
104+
api_endpoint: str = instance.api_endpoint # type: ignore[reportUnknownMemberType]
105+
span_attributes = {
106+
**get_genai_request_attributes(params),
107+
**get_server_attributes(api_endpoint),
108+
}
104109

105110
span_name = get_span_name(span_attributes)
106111
with tracer.start_as_current_span(

instrumentation-genai/opentelemetry-instrumentation-vertexai/src/opentelemetry/instrumentation/vertexai/utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
Mapping,
2323
Sequence,
2424
)
25+
from urllib.parse import urlparse
2526

2627
from opentelemetry.semconv._incubating.attributes import (
2728
gen_ai_attributes as GenAIAttributes,
2829
)
30+
from opentelemetry.semconv.attributes import server_attributes
2931
from opentelemetry.util.types import AttributeValue
3032

3133
if TYPE_CHECKING:
@@ -58,6 +60,24 @@ class GenerateContentParams:
5860
) = None
5961

6062

63+
def get_server_attributes(
64+
endpoint: str,
65+
) -> dict[str, AttributeValue]:
66+
"""Get server.* attributes from the endpoint, which is a hostname with optional port e.g.
67+
- ``us-central1-aiplatform.googleapis.com``
68+
- ``us-central1-aiplatform.googleapis.com:5431``
69+
"""
70+
parsed = urlparse(f"scheme://{endpoint}")
71+
72+
if not parsed.hostname:
73+
return {}
74+
75+
return {
76+
server_attributes.SERVER_ADDRESS: parsed.hostname,
77+
server_attributes.SERVER_PORT: parsed.port or 443,
78+
}
79+
80+
6181
def get_genai_request_attributes(
6282
params: GenerateContentParams,
6383
operation_name: GenAIAttributes.GenAiOperationNameValues = GenAIAttributes.GenAiOperationNameValues.CHAT,

instrumentation-genai/opentelemetry-instrumentation-vertexai/tests/test_chat_completions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ def test_generate_content(
3434
"gen_ai.operation.name": "chat",
3535
"gen_ai.request.model": "gemini-1.5-flash-002",
3636
"gen_ai.system": "vertex_ai",
37+
"server.address": "us-central1-aiplatform.googleapis.com",
38+
"server.port": 443,
3739
}
3840

3941

@@ -62,6 +64,8 @@ def test_generate_content_empty_model(
6264
"gen_ai.operation.name": "chat",
6365
"gen_ai.request.model": "",
6466
"gen_ai.system": "vertex_ai",
67+
"server.address": "us-central1-aiplatform.googleapis.com",
68+
"server.port": 443,
6569
}
6670
assert_span_error(spans[0])
6771

@@ -91,6 +95,8 @@ def test_generate_content_missing_model(
9195
"gen_ai.operation.name": "chat",
9296
"gen_ai.request.model": "gemini-does-not-exist",
9397
"gen_ai.system": "vertex_ai",
98+
"server.address": "us-central1-aiplatform.googleapis.com",
99+
"server.port": 443,
94100
}
95101
assert_span_error(spans[0])
96102

@@ -122,6 +128,8 @@ def test_generate_content_invalid_temperature(
122128
"gen_ai.request.model": "gemini-1.5-flash-002",
123129
"gen_ai.request.temperature": 1000.0,
124130
"gen_ai.system": "vertex_ai",
131+
"server.address": "us-central1-aiplatform.googleapis.com",
132+
"server.port": 443,
125133
}
126134
assert_span_error(spans[0])
127135

@@ -158,6 +166,8 @@ def test_generate_content_extra_params(span_exporter, instrument_no_content):
158166
"gen_ai.request.temperature": 0.20000000298023224,
159167
"gen_ai.request.top_p": 0.949999988079071,
160168
"gen_ai.system": "vertex_ai",
169+
"server.address": "us-central1-aiplatform.googleapis.com",
170+
"server.port": 443,
161171
}
162172

163173

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from opentelemetry.instrumentation.vertexai.utils import get_server_attributes
17+
18+
19+
def test_get_server_attributes() -> None:
20+
# without port
21+
assert get_server_attributes("us-central1-aiplatform.googleapis.com") == {
22+
"server.address": "us-central1-aiplatform.googleapis.com",
23+
"server.port": 443,
24+
}
25+
26+
# with port
27+
assert get_server_attributes(
28+
"us-central1-aiplatform.googleapis.com:5432"
29+
) == {
30+
"server.address": "us-central1-aiplatform.googleapis.com",
31+
"server.port": 5432,
32+
}

0 commit comments

Comments
 (0)