Repository: google/adk-go
File: internal/telemetry/telemetry.go (Go)

Code before:
	mu             *sync.RWMutex
}

var (
	once              sync.Once
	localTracer       tracerProviderHolder
	localTracerConfig = tracerProviderConfig{
		spanProcessors: []sdktrace.SpanProcessor{},
		mu:             &sync.RWMutex{},
	}
)

const (
	systemName           = "gcp.vertex.agent"
	genAiOperationName   = "gen_ai.operation.name"
	genAiToolDescription = "gen_ai.tool.description"
	genAiToolName        = "gen_ai.tool.name"
	genAiToolCallID      = "gen_ai.tool.call.id"
	genAiSystemName      = "gen_ai.system"

	genAiRequestModelName = "gen_ai.request.model"
	genAiRequestTopP      = "gen_ai.request.top_p"
	genAiRequestMaxTokens = "gen_ai.request.max_tokens"

	genAiResponseFinishReason        = "gen_ai.response.finish_reason"
	genAiResponsePromptTokens        = "gen_ai.response.prompt_tokens"
	genAiResponseCompletionTokens    = "gen_ai.response.completion_tokens"
	genAiResponseCachedContentTokens = "gen_ai.response.cached_content_tokens"
	genAiResponseTotalTokens         = "gen_ai.response.total_tokens"

	gcpVertexAgentLLMRequestName   = "gcp.vertex.agent.llm_request"
	gcpVertexAgentToolCallArgsName = "gcp.vertex.agent.tool_call_args"
	gcpVertexAgentEventID          = "gcp.vertex.agent.event_id"
	gcpVertexAgentToolResponseName = "gcp.vertex.agent.tool_response"
	gcpVertexAgentLLMResponseName  = "gcp.vertex.agent.llm_response"
	gcpVertexAgentInvocationID     = "gcp.vertex.agent.invocation_id"
	gcpVertexAgentSessionID        = "gcp.vertex.agent.session_id"

	executeToolName = "execute_tool"
	mergeToolName   = "(merged tools)"
)

// AddSpanProcessor adds a span processor to the local tracer config.
func AddSpanProcessor(processor sdktrace.SpanProcessor) {
	localTracerConfig.mu.Lock()
	defer localTracerConfig.mu.Unlock()
	localTracerConfig.spanProcessors = append(localTracerConfig.spanProcessors, processor)
}

// RegisterTelemetry sets up the local tracer that will be used to emit traces.
// We use local tracer to respect the global tracer configurations.

Review comment (suggestion):
nit: WDYT if we have the same names as genai API provides? in genai the field is named `event.UsageMetadata.PromptTokenCount`

```suggestion
	genAiResponsePromptTokenCount        = "gen_ai.response.prompt_token_count"
```

And the same for other added fields.

Code after:

type tracerProviderConfig struct {
	spanProcessors []sdktrace.SpanProcessor
	mu             *sync.RWMutex
}

var (
	once              sync.Once
	localTracer       tracerProviderHolder
	localTracerConfig = tracerProviderConfig{
		spanProcessors: []sdktrace.SpanProcessor{},
		mu:             &sync.RWMutex{},
	}
)

const (
	systemName           = "gcp.vertex.agent"
	genAiOperationName   = "gen_ai.operation.name"
	genAiToolDescription = "gen_ai.tool.description"
	genAiToolName        = "gen_ai.tool.name"
	genAiToolCallID      = "gen_ai.tool.call.id"
	genAiSystemName      = "gen_ai.system"

	genAiRequestModelName = "gen_ai.request.model"
	genAiRequestTopP      = "gen_ai.request.top_p"
	genAiRequestMaxTokens = "gen_ai.request.max_tokens"

	genAiResponseFinishReason            = "gen_ai.response.finish_reason"
	genAiResponsePromptTokenCount        = "gen_ai.response.prompt_token_count"
	genAiResponseCandidatesTokenCount    = "gen_ai.response.candidates_token_count"
	genAiResponseCachedContentTokenCount = "gen_ai.response.cached_content_token_count"
	genAiResponseTotalTokenCount         = "gen_ai.response.total_token_count"

	gcpVertexAgentLLMRequestName   = "gcp.vertex.agent.llm_request"
	gcpVertexAgentToolCallArgsName = "gcp.vertex.agent.tool_call_args"
	gcpVertexAgentEventID          = "gcp.vertex.agent.event_id"
	gcpVertexAgentToolResponseName = "gcp.vertex.agent.tool_response"
	gcpVertexAgentLLMResponseName  = "gcp.vertex.agent.llm_response"
	gcpVertexAgentInvocationID     = "gcp.vertex.agent.invocation_id"
	gcpVertexAgentSessionID        = "gcp.vertex.agent.session_id"

	executeToolName = "execute_tool"
	mergeToolName   = "(merged tools)"
)

// AddSpanProcessor adds a span processor to the local tracer config.
func AddSpanProcessor(processor sdktrace.SpanProcessor) {
	localTracerConfig.mu.Lock()
	defer localTracerConfig.mu.Unlock()
	localTracerConfig.spanProcessors = append(localTracerConfig.spanProcessors, processor)
}