Skip to content

Commit f40ecc5

Browse files
committed
Update operation name to use a constant for consistency.
1 parent 194f0b0 commit f40ecc5

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/generate_content.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
_logger = logging.getLogger(__name__)
4646

4747

48+
# Constant used for the value of 'gen_ai.operation.name".
49+
_GENERATE_CONTENT_OP_NAME = "generate_content"
50+
4851
# Constant used to make the absence of content more understandable.
4952
_CONTENT_ELIDED = "<elided>"
5053

@@ -220,13 +223,13 @@ def __init__(
220223

221224
def start_span_as_current_span(self, model_name, function_name):
222225
return self._otel_wrapper.start_as_current_span(
223-
f"generate_content {model_name}",
226+
f"{_GENERATE_CONTENT_OP_NAME} {model_name}",
224227
start_time=self._start_time,
225228
attributes={
226229
code_attributes.CODE_FUNCTION_NAME: function_name,
227230
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
228231
gen_ai_attributes.GEN_AI_REQUEST_MODEL: self._genai_request_model,
229-
gen_ai_attributes.GEN_AI_OPERATION_NAME: "GenerateContent",
232+
gen_ai_attributes.GEN_AI_OPERATION_NAME: _GENERATE_CONTENT_OP_NAME,
230233
},
231234
)
232235

@@ -464,7 +467,7 @@ def _record_token_usage_metric(self):
464467
gen_ai_attributes.GEN_AI_TOKEN_TYPE: "input",
465468
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
466469
gen_ai_attributes.GEN_AI_REQUEST_MODEL: self._genai_request_model,
467-
gen_ai_attributes.GEN_AI_OPERATION_NAME: "GenerateContent",
470+
gen_ai_attributes.GEN_AI_OPERATION_NAME: _GENERATE_CONTENT_OP_NAME,
468471
},
469472
)
470473
self._otel_wrapper.token_usage_metric.record(
@@ -473,15 +476,15 @@ def _record_token_usage_metric(self):
473476
gen_ai_attributes.GEN_AI_TOKEN_TYPE: "output",
474477
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
475478
gen_ai_attributes.GEN_AI_REQUEST_MODEL: self._genai_request_model,
476-
gen_ai_attributes.GEN_AI_OPERATION_NAME: "GenerateContent",
479+
gen_ai_attributes.GEN_AI_OPERATION_NAME: _GENERATE_CONTENT_OP_NAME,
477480
},
478481
)
479482

480483
def _record_duration_metric(self):
481484
attributes = {
482485
gen_ai_attributes.GEN_AI_SYSTEM: self._genai_system,
483486
gen_ai_attributes.GEN_AI_REQUEST_MODEL: self._genai_request_model,
484-
gen_ai_attributes.GEN_AI_OPERATION_NAME: "GenerateContent",
487+
gen_ai_attributes.GEN_AI_OPERATION_NAME: _GENERATE_CONTENT_OP_NAME,
485488
}
486489
if self._error_type is not None:
487490
attributes[error_attributes.ERROR_TYPE] = self._error_type

instrumentation-genai/opentelemetry-instrumentation-google-genai/tests/generate_content/nonstreaming_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def test_generated_span_has_minimal_genai_attributes(self):
105105
span = self.otel.get_span_named("generate_content gemini-2.0-flash")
106106
self.assertEqual(span.attributes["gen_ai.system"], "gemini")
107107
self.assertEqual(
108-
span.attributes["gen_ai.operation.name"], "GenerateContent"
108+
span.attributes["gen_ai.operation.name"], "generate_content"
109109
)
110110

111111
def test_generated_span_has_correct_function_name(self):
@@ -129,7 +129,7 @@ def test_generated_span_has_vertex_ai_system_when_configured(self):
129129
span = self.otel.get_span_named("generate_content gemini-2.0-flash")
130130
self.assertEqual(span.attributes["gen_ai.system"], "vertex_ai")
131131
self.assertEqual(
132-
span.attributes["gen_ai.operation.name"], "GenerateContent"
132+
span.attributes["gen_ai.operation.name"], "generate_content"
133133
)
134134

135135
def test_generated_span_counts_tokens(self):

0 commit comments

Comments
 (0)