| 1 |
// Code generated from semantic convention specification. DO NOT EDIT. |
| 2 |
|
| 3 |
// Copyright The OpenTelemetry Authors |
| 4 |
// SPDX-License-Identifier: Apache-2.0 |
| 5 |
|
| 6 |
// Package genaiconv provides types and functionality for OpenTelemetry semantic |
| 7 |
// conventions in the "gen_ai" namespace. |
| 8 |
package genaiconv |
| 9 |
|
| 10 |
import ( |
| 11 |
"context" |
| 12 |
"sync" |
| 13 |
|
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
"go.opentelemetry.io/otel/metric" |
| 16 |
"go.opentelemetry.io/otel/metric/noop" |
| 17 |
) |
| 18 |
|
| 19 |
var ( |
| 20 |
addOptPool = &sync.Pool{New: func() any { return &[]metric.AddOption{} }} |
| 21 |
recOptPool = &sync.Pool{New: func() any { return &[]metric.RecordOption{} }} |
| 22 |
) |
| 23 |
|
| 24 |
// ErrorTypeAttr is an attribute conforming to the error.type semantic |
| 25 |
// conventions. It represents the describes a class of error the operation ended |
| 26 |
// with. |
| 27 |
type ErrorTypeAttr string |
| 28 |
|
| 29 |
var ( |
| 30 |
// ErrorTypeOther is a fallback error value to be used when the instrumentation |
| 31 |
// doesn't define a custom value. |
| 32 |
ErrorTypeOther ErrorTypeAttr = "_OTHER" |
| 33 |
) |
| 34 |
|
| 35 |
// OperationNameAttr is an attribute conforming to the gen_ai.operation.name |
| 36 |
// semantic conventions. It represents the name of the operation being performed. |
| 37 |
type OperationNameAttr string |
| 38 |
|
| 39 |
var ( |
| 40 |
// OperationNameChat is the chat completion operation such as [OpenAI Chat API] |
| 41 |
// . |
| 42 |
// |
| 43 |
// [OpenAI Chat API]: https://platform.openai.com/docs/api-reference/chat |
| 44 |
OperationNameChat OperationNameAttr = "chat" |
| 45 |
// OperationNameGenerateContent is the multimodal content generation operation |
| 46 |
// such as [Gemini Generate Content]. |
| 47 |
// |
| 48 |
// [Gemini Generate Content]: https://ai.google.dev/api/generate-content |
| 49 |
OperationNameGenerateContent OperationNameAttr = "generate_content" |
| 50 |
// OperationNameTextCompletion is the text completions operation such as |
| 51 |
// [OpenAI Completions API (Legacy)]. |
| 52 |
// |
| 53 |
// [OpenAI Completions API (Legacy)]: https://platform.openai.com/docs/api-reference/completions |
| 54 |
OperationNameTextCompletion OperationNameAttr = "text_completion" |
| 55 |
// OperationNameEmbeddings is the embeddings operation such as |
| 56 |
// [OpenAI Create embeddings API]. |
| 57 |
// |
| 58 |
// [OpenAI Create embeddings API]: https://platform.openai.com/docs/api-reference/embeddings/create |
| 59 |
OperationNameEmbeddings OperationNameAttr = "embeddings" |
| 60 |
// OperationNameCreateAgent is the create GenAI agent. |
| 61 |
OperationNameCreateAgent OperationNameAttr = "create_agent" |
| 62 |
// OperationNameInvokeAgent is the invoke GenAI agent. |
| 63 |
OperationNameInvokeAgent OperationNameAttr = "invoke_agent" |
| 64 |
// OperationNameExecuteTool is the execute a tool. |
| 65 |
OperationNameExecuteTool OperationNameAttr = "execute_tool" |
| 66 |
) |
| 67 |
|
| 68 |
// SystemAttr is an attribute conforming to the gen_ai.system semantic |
| 69 |
// conventions. It represents the Generative AI product as identified by the |
| 70 |
// client or server instrumentation. |
| 71 |
type SystemAttr string |
| 72 |
|
| 73 |
var ( |
| 74 |
// SystemOpenAI is the openAI. |
| 75 |
SystemOpenAI SystemAttr = "openai" |
| 76 |
// SystemGCPGenAI is the any Google generative AI endpoint. |
| 77 |
SystemGCPGenAI SystemAttr = "gcp.gen_ai" |
| 78 |
// SystemGCPVertexAI is the vertex AI. |
| 79 |
SystemGCPVertexAI SystemAttr = "gcp.vertex_ai" |
| 80 |
// SystemGCPGemini is the gemini. |
| 81 |
SystemGCPGemini SystemAttr = "gcp.gemini" |
| 82 |
// SystemAnthropic is the anthropic. |
| 83 |
SystemAnthropic SystemAttr = "anthropic" |
| 84 |
// SystemCohere is the cohere. |
| 85 |
SystemCohere SystemAttr = "cohere" |
| 86 |
// SystemAzAIInference is the azure AI Inference. |
| 87 |
SystemAzAIInference SystemAttr = "az.ai.inference" |
| 88 |
// SystemAzAIOpenAI is the azure OpenAI. |
| 89 |
SystemAzAIOpenAI SystemAttr = "az.ai.openai" |
| 90 |
// SystemIBMWatsonxAI is the IBM Watsonx AI. |
| 91 |
SystemIBMWatsonxAI SystemAttr = "ibm.watsonx.ai" |
| 92 |
// SystemAWSBedrock is the AWS Bedrock. |
| 93 |
SystemAWSBedrock SystemAttr = "aws.bedrock" |
| 94 |
// SystemPerplexity is the perplexity. |
| 95 |
SystemPerplexity SystemAttr = "perplexity" |
| 96 |
// SystemXai is the xAI. |
| 97 |
SystemXai SystemAttr = "xai" |
| 98 |
// SystemDeepseek is the deepSeek. |
| 99 |
SystemDeepseek SystemAttr = "deepseek" |
| 100 |
// SystemGroq is the groq. |
| 101 |
SystemGroq SystemAttr = "groq" |
| 102 |
// SystemMistralAI is the mistral AI. |
| 103 |
SystemMistralAI SystemAttr = "mistral_ai" |
| 104 |
) |
| 105 |
|
| 106 |
// TokenTypeAttr is an attribute conforming to the gen_ai.token.type semantic |
| 107 |
// conventions. It represents the type of token being counted. |
| 108 |
type TokenTypeAttr string |
| 109 |
|
| 110 |
var ( |
| 111 |
// TokenTypeInput is the input tokens (prompt, input, etc.). |
| 112 |
TokenTypeInput TokenTypeAttr = "input" |
| 113 |
// TokenTypeOutput is the output tokens (completion, response, etc.). |
| 114 |
TokenTypeOutput TokenTypeAttr = "output" |
| 115 |
) |
| 116 |
|
| 117 |
// ClientOperationDuration is an instrument used to record metric values |
| 118 |
// conforming to the "gen_ai.client.operation.duration" semantic conventions. It |
| 119 |
// represents the genAI operation duration. |
| 120 |
type ClientOperationDuration struct { |
| 121 |
metric.Float64Histogram |
| 122 |
} |
| 123 |
|
| 124 |
// NewClientOperationDuration returns a new ClientOperationDuration instrument. |
| 125 |
func NewClientOperationDuration( |
| 126 |
m metric.Meter, |
| 127 |
opt ...metric.Float64HistogramOption, |
| 128 |
) (ClientOperationDuration, error) { |
| 129 |
// Check if the meter is nil. |
| 130 |
if m == nil { |
| 131 |
return ClientOperationDuration{noop.Float64Histogram{}}, nil |
| 132 |
} |
| 133 |
|
| 134 |
i, err := m.Float64Histogram( |
| 135 |
"gen_ai.client.operation.duration", |
| 136 |
append([]metric.Float64HistogramOption{ |
| 137 |
metric.WithDescription("GenAI operation duration"), |
| 138 |
metric.WithUnit("s"), |
| 139 |
}, opt...)..., |
| 140 |
) |
| 141 |
if err != nil { |
| 142 |
return ClientOperationDuration{noop.Float64Histogram{}}, err |
| 143 |
} |
| 144 |
return ClientOperationDuration{i}, nil |
| 145 |
} |
| 146 |
|
| 147 |
// Inst returns the underlying metric instrument. |
| 148 |
func (m ClientOperationDuration) Inst() metric.Float64Histogram { |
| 149 |
return m.Float64Histogram |
| 150 |
} |
| 151 |
|
| 152 |
// Name returns the semantic convention name of the instrument. |
| 153 |
func (ClientOperationDuration) Name() string { |
| 154 |
return "gen_ai.client.operation.duration" |
| 155 |
} |
| 156 |
|
| 157 |
// Unit returns the semantic convention unit of the instrument |
| 158 |
func (ClientOperationDuration) Unit() string { |
| 159 |
return "s" |
| 160 |
} |
| 161 |
|
| 162 |
// Description returns the semantic convention description of the instrument |
| 163 |
func (ClientOperationDuration) Description() string { |
| 164 |
return "GenAI operation duration" |
| 165 |
} |
| 166 |
|
| 167 |
// Record records val to the current distribution. |
| 168 |
// |
| 169 |
// The operationName is the the name of the operation being performed. |
| 170 |
// |
| 171 |
// The system is the the Generative AI product as identified by the client or |
| 172 |
// server instrumentation. |
| 173 |
// |
| 174 |
// All additional attrs passed are included in the recorded value. |
| 175 |
func (m ClientOperationDuration) Record( |
| 176 |
ctx context.Context, |
| 177 |
val float64, |
| 178 |
operationName OperationNameAttr, |
| 179 |
system SystemAttr, |
| 180 |
attrs ...attribute.KeyValue, |
| 181 |
) { |
| 182 |
o := recOptPool.Get().(*[]metric.RecordOption) |
| 183 |
defer func() { |
| 184 |
*o = (*o)[:0] |
| 185 |
recOptPool.Put(o) |
| 186 |
}() |
| 187 |
|
| 188 |
*o = append( |
| 189 |
*o, |
| 190 |
metric.WithAttributes( |
| 191 |
append( |
| 192 |
attrs, |
| 193 |
attribute.String("gen_ai.operation.name", string(operationName)), |
| 194 |
attribute.String("gen_ai.system", string(system)), |
| 195 |
)..., |
| 196 |
), |
| 197 |
) |
| 198 |
|
| 199 |
m.Float64Histogram.Record(ctx, val, *o...) |
| 200 |
} |
| 201 |
|
| 202 |
// AttrErrorType returns an optional attribute for the "error.type" semantic |
| 203 |
// convention. It represents the describes a class of error the operation ended |
| 204 |
// with. |
| 205 |
func (ClientOperationDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue { |
| 206 |
return attribute.String("error.type", string(val)) |
| 207 |
} |
| 208 |
|
| 209 |
// AttrRequestModel returns an optional attribute for the "gen_ai.request.model" |
| 210 |
// semantic convention. It represents the name of the GenAI model a request is |
| 211 |
// being made to. |
| 212 |
func (ClientOperationDuration) AttrRequestModel(val string) attribute.KeyValue { |
| 213 |
return attribute.String("gen_ai.request.model", val) |
| 214 |
} |
| 215 |
|
| 216 |
// AttrServerPort returns an optional attribute for the "server.port" semantic |
| 217 |
// convention. It represents the genAI server port. |
| 218 |
func (ClientOperationDuration) AttrServerPort(val int) attribute.KeyValue { |
| 219 |
return attribute.Int("server.port", val) |
| 220 |
} |
| 221 |
|
| 222 |
// AttrResponseModel returns an optional attribute for the |
| 223 |
// "gen_ai.response.model" semantic convention. It represents the name of the |
| 224 |
// model that generated the response. |
| 225 |
func (ClientOperationDuration) AttrResponseModel(val string) attribute.KeyValue { |
| 226 |
return attribute.String("gen_ai.response.model", val) |
| 227 |
} |
| 228 |
|
| 229 |
// AttrServerAddress returns an optional attribute for the "server.address" |
| 230 |
// semantic convention. It represents the genAI server address. |
| 231 |
func (ClientOperationDuration) AttrServerAddress(val string) attribute.KeyValue { |
| 232 |
return attribute.String("server.address", val) |
| 233 |
} |
| 234 |
|
| 235 |
// ClientTokenUsage is an instrument used to record metric values conforming to |
| 236 |
// the "gen_ai.client.token.usage" semantic conventions. It represents the |
| 237 |
// measures number of input and output tokens used. |
| 238 |
type ClientTokenUsage struct { |
| 239 |
metric.Int64Histogram |
| 240 |
} |
| 241 |
|
| 242 |
// NewClientTokenUsage returns a new ClientTokenUsage instrument. |
| 243 |
func NewClientTokenUsage( |
| 244 |
m metric.Meter, |
| 245 |
opt ...metric.Int64HistogramOption, |
| 246 |
) (ClientTokenUsage, error) { |
| 247 |
// Check if the meter is nil. |
| 248 |
if m == nil { |
| 249 |
return ClientTokenUsage{noop.Int64Histogram{}}, nil |
| 250 |
} |
| 251 |
|
| 252 |
i, err := m.Int64Histogram( |
| 253 |
"gen_ai.client.token.usage", |
| 254 |
append([]metric.Int64HistogramOption{ |
| 255 |
metric.WithDescription("Measures number of input and output tokens used"), |
| 256 |
metric.WithUnit("{token}"), |
| 257 |
}, opt...)..., |
| 258 |
) |
| 259 |
if err != nil { |
| 260 |
return ClientTokenUsage{noop.Int64Histogram{}}, err |
| 261 |
} |
| 262 |
return ClientTokenUsage{i}, nil |
| 263 |
} |
| 264 |
|
| 265 |
// Inst returns the underlying metric instrument. |
| 266 |
func (m ClientTokenUsage) Inst() metric.Int64Histogram { |
| 267 |
return m.Int64Histogram |
| 268 |
} |
| 269 |
|
| 270 |
// Name returns the semantic convention name of the instrument. |
| 271 |
func (ClientTokenUsage) Name() string { |
| 272 |
return "gen_ai.client.token.usage" |
| 273 |
} |
| 274 |
|
| 275 |
// Unit returns the semantic convention unit of the instrument |
| 276 |
func (ClientTokenUsage) Unit() string { |
| 277 |
return "{token}" |
| 278 |
} |
| 279 |
|
| 280 |
// Description returns the semantic convention description of the instrument |
| 281 |
func (ClientTokenUsage) Description() string { |
| 282 |
return "Measures number of input and output tokens used" |
| 283 |
} |
| 284 |
|
| 285 |
// Record records val to the current distribution. |
| 286 |
// |
| 287 |
// The operationName is the the name of the operation being performed. |
| 288 |
// |
| 289 |
// The system is the the Generative AI product as identified by the client or |
| 290 |
// server instrumentation. |
| 291 |
// |
| 292 |
// The tokenType is the the type of token being counted. |
| 293 |
// |
| 294 |
// All additional attrs passed are included in the recorded value. |
| 295 |
func (m ClientTokenUsage) Record( |
| 296 |
ctx context.Context, |
| 297 |
val int64, |
| 298 |
operationName OperationNameAttr, |
| 299 |
system SystemAttr, |
| 300 |
tokenType TokenTypeAttr, |
| 301 |
attrs ...attribute.KeyValue, |
| 302 |
) { |
| 303 |
o := recOptPool.Get().(*[]metric.RecordOption) |
| 304 |
defer func() { |
| 305 |
*o = (*o)[:0] |
| 306 |
recOptPool.Put(o) |
| 307 |
}() |
| 308 |
|
| 309 |
*o = append( |
| 310 |
*o, |
| 311 |
metric.WithAttributes( |
| 312 |
append( |
| 313 |
attrs, |
| 314 |
attribute.String("gen_ai.operation.name", string(operationName)), |
| 315 |
attribute.String("gen_ai.system", string(system)), |
| 316 |
attribute.String("gen_ai.token.type", string(tokenType)), |
| 317 |
)..., |
| 318 |
), |
| 319 |
) |
| 320 |
|
| 321 |
m.Int64Histogram.Record(ctx, val, *o...) |
| 322 |
} |
| 323 |
|
| 324 |
// AttrRequestModel returns an optional attribute for the "gen_ai.request.model" |
| 325 |
// semantic convention. It represents the name of the GenAI model a request is |
| 326 |
// being made to. |
| 327 |
func (ClientTokenUsage) AttrRequestModel(val string) attribute.KeyValue { |
| 328 |
return attribute.String("gen_ai.request.model", val) |
| 329 |
} |
| 330 |
|
| 331 |
// AttrServerPort returns an optional attribute for the "server.port" semantic |
| 332 |
// convention. It represents the genAI server port. |
| 333 |
func (ClientTokenUsage) AttrServerPort(val int) attribute.KeyValue { |
| 334 |
return attribute.Int("server.port", val) |
| 335 |
} |
| 336 |
|
| 337 |
// AttrResponseModel returns an optional attribute for the |
| 338 |
// "gen_ai.response.model" semantic convention. It represents the name of the |
| 339 |
// model that generated the response. |
| 340 |
func (ClientTokenUsage) AttrResponseModel(val string) attribute.KeyValue { |
| 341 |
return attribute.String("gen_ai.response.model", val) |
| 342 |
} |
| 343 |
|
| 344 |
// AttrServerAddress returns an optional attribute for the "server.address" |
| 345 |
// semantic convention. It represents the genAI server address. |
| 346 |
func (ClientTokenUsage) AttrServerAddress(val string) attribute.KeyValue { |
| 347 |
return attribute.String("server.address", val) |
| 348 |
} |
| 349 |
|
| 350 |
// ServerRequestDuration is an instrument used to record metric values conforming |
| 351 |
// to the "gen_ai.server.request.duration" semantic conventions. It represents |
| 352 |
// the generative AI server request duration such as time-to-last byte or last |
| 353 |
// output token. |
| 354 |
type ServerRequestDuration struct { |
| 355 |
metric.Float64Histogram |
| 356 |
} |
| 357 |
|
| 358 |
// NewServerRequestDuration returns a new ServerRequestDuration instrument. |
| 359 |
func NewServerRequestDuration( |
| 360 |
m metric.Meter, |
| 361 |
opt ...metric.Float64HistogramOption, |
| 362 |
) (ServerRequestDuration, error) { |
| 363 |
// Check if the meter is nil. |
| 364 |
if m == nil { |
| 365 |
return ServerRequestDuration{noop.Float64Histogram{}}, nil |
| 366 |
} |
| 367 |
|
| 368 |
i, err := m.Float64Histogram( |
| 369 |
"gen_ai.server.request.duration", |
| 370 |
append([]metric.Float64HistogramOption{ |
| 371 |
metric.WithDescription("Generative AI server request duration such as time-to-last byte or last output token"), |
| 372 |
metric.WithUnit("s"), |
| 373 |
}, opt...)..., |
| 374 |
) |
| 375 |
if err != nil { |
| 376 |
return ServerRequestDuration{noop.Float64Histogram{}}, err |
| 377 |
} |
| 378 |
return ServerRequestDuration{i}, nil |
| 379 |
} |
| 380 |
|
| 381 |
// Inst returns the underlying metric instrument. |
| 382 |
func (m ServerRequestDuration) Inst() metric.Float64Histogram { |
| 383 |
return m.Float64Histogram |
| 384 |
} |
| 385 |
|
| 386 |
// Name returns the semantic convention name of the instrument. |
| 387 |
func (ServerRequestDuration) Name() string { |
| 388 |
return "gen_ai.server.request.duration" |
| 389 |
} |
| 390 |
|
| 391 |
// Unit returns the semantic convention unit of the instrument |
| 392 |
func (ServerRequestDuration) Unit() string { |
| 393 |
return "s" |
| 394 |
} |
| 395 |
|
| 396 |
// Description returns the semantic convention description of the instrument |
| 397 |
func (ServerRequestDuration) Description() string { |
| 398 |
return "Generative AI server request duration such as time-to-last byte or last output token" |
| 399 |
} |
| 400 |
|
| 401 |
// Record records val to the current distribution. |
| 402 |
// |
| 403 |
// The operationName is the the name of the operation being performed. |
| 404 |
// |
| 405 |
// The system is the the Generative AI product as identified by the client or |
| 406 |
// server instrumentation. |
| 407 |
// |
| 408 |
// All additional attrs passed are included in the recorded value. |
| 409 |
func (m ServerRequestDuration) Record( |
| 410 |
ctx context.Context, |
| 411 |
val float64, |
| 412 |
operationName OperationNameAttr, |
| 413 |
system SystemAttr, |
| 414 |
attrs ...attribute.KeyValue, |
| 415 |
) { |
| 416 |
o := recOptPool.Get().(*[]metric.RecordOption) |
| 417 |
defer func() { |
| 418 |
*o = (*o)[:0] |
| 419 |
recOptPool.Put(o) |
| 420 |
}() |
| 421 |
|
| 422 |
*o = append( |
| 423 |
*o, |
| 424 |
metric.WithAttributes( |
| 425 |
append( |
| 426 |
attrs, |
| 427 |
attribute.String("gen_ai.operation.name", string(operationName)), |
| 428 |
attribute.String("gen_ai.system", string(system)), |
| 429 |
)..., |
| 430 |
), |
| 431 |
) |
| 432 |
|
| 433 |
m.Float64Histogram.Record(ctx, val, *o...) |
| 434 |
} |
| 435 |
|
| 436 |
// AttrErrorType returns an optional attribute for the "error.type" semantic |
| 437 |
// convention. It represents the describes a class of error the operation ended |
| 438 |
// with. |
| 439 |
func (ServerRequestDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue { |
| 440 |
return attribute.String("error.type", string(val)) |
| 441 |
} |
| 442 |
|
| 443 |
// AttrRequestModel returns an optional attribute for the "gen_ai.request.model" |
| 444 |
// semantic convention. It represents the name of the GenAI model a request is |
| 445 |
// being made to. |
| 446 |
func (ServerRequestDuration) AttrRequestModel(val string) attribute.KeyValue { |
| 447 |
return attribute.String("gen_ai.request.model", val) |
| 448 |
} |
| 449 |
|
| 450 |
// AttrServerPort returns an optional attribute for the "server.port" semantic |
| 451 |
// convention. It represents the genAI server port. |
| 452 |
func (ServerRequestDuration) AttrServerPort(val int) attribute.KeyValue { |
| 453 |
return attribute.Int("server.port", val) |
| 454 |
} |
| 455 |
|
| 456 |
// AttrResponseModel returns an optional attribute for the |
| 457 |
// "gen_ai.response.model" semantic convention. It represents the name of the |
| 458 |
// model that generated the response. |
| 459 |
func (ServerRequestDuration) AttrResponseModel(val string) attribute.KeyValue { |
| 460 |
return attribute.String("gen_ai.response.model", val) |
| 461 |
} |
| 462 |
|
| 463 |
// AttrServerAddress returns an optional attribute for the "server.address" |
| 464 |
// semantic convention. It represents the genAI server address. |
| 465 |
func (ServerRequestDuration) AttrServerAddress(val string) attribute.KeyValue { |
| 466 |
return attribute.String("server.address", val) |
| 467 |
} |
| 468 |
|
| 469 |
// ServerTimePerOutputToken is an instrument used to record metric values |
| 470 |
// conforming to the "gen_ai.server.time_per_output_token" semantic conventions. |
| 471 |
// It represents the time per output token generated after the first token for |
| 472 |
// successful responses. |
| 473 |
type ServerTimePerOutputToken struct { |
| 474 |
metric.Float64Histogram |
| 475 |
} |
| 476 |
|
| 477 |
// NewServerTimePerOutputToken returns a new ServerTimePerOutputToken instrument. |
| 478 |
func NewServerTimePerOutputToken( |
| 479 |
m metric.Meter, |
| 480 |
opt ...metric.Float64HistogramOption, |
| 481 |
) (ServerTimePerOutputToken, error) { |
| 482 |
// Check if the meter is nil. |
| 483 |
if m == nil { |
| 484 |
return ServerTimePerOutputToken{noop.Float64Histogram{}}, nil |
| 485 |
} |
| 486 |
|
| 487 |
i, err := m.Float64Histogram( |
| 488 |
"gen_ai.server.time_per_output_token", |
| 489 |
append([]metric.Float64HistogramOption{ |
| 490 |
metric.WithDescription("Time per output token generated after the first token for successful responses"), |
| 491 |
metric.WithUnit("s"), |
| 492 |
}, opt...)..., |
| 493 |
) |
| 494 |
if err != nil { |
| 495 |
return ServerTimePerOutputToken{noop.Float64Histogram{}}, err |
| 496 |
} |
| 497 |
return ServerTimePerOutputToken{i}, nil |
| 498 |
} |
| 499 |
|
| 500 |
// Inst returns the underlying metric instrument. |
| 501 |
func (m ServerTimePerOutputToken) Inst() metric.Float64Histogram { |
| 502 |
return m.Float64Histogram |
| 503 |
} |
| 504 |
|
| 505 |
// Name returns the semantic convention name of the instrument. |
| 506 |
func (ServerTimePerOutputToken) Name() string { |
| 507 |
return "gen_ai.server.time_per_output_token" |
| 508 |
} |
| 509 |
|
| 510 |
// Unit returns the semantic convention unit of the instrument |
| 511 |
func (ServerTimePerOutputToken) Unit() string { |
| 512 |
return "s" |
| 513 |
} |
| 514 |
|
| 515 |
// Description returns the semantic convention description of the instrument |
| 516 |
func (ServerTimePerOutputToken) Description() string { |
| 517 |
return "Time per output token generated after the first token for successful responses" |
| 518 |
} |
| 519 |
|
| 520 |
// Record records val to the current distribution. |
| 521 |
// |
| 522 |
// The operationName is the the name of the operation being performed. |
| 523 |
// |
| 524 |
// The system is the the Generative AI product as identified by the client or |
| 525 |
// server instrumentation. |
| 526 |
// |
| 527 |
// All additional attrs passed are included in the recorded value. |
| 528 |
func (m ServerTimePerOutputToken) Record( |
| 529 |
ctx context.Context, |
| 530 |
val float64, |
| 531 |
operationName OperationNameAttr, |
| 532 |
system SystemAttr, |
| 533 |
attrs ...attribute.KeyValue, |
| 534 |
) { |
| 535 |
o := recOptPool.Get().(*[]metric.RecordOption) |
| 536 |
defer func() { |
| 537 |
*o = (*o)[:0] |
| 538 |
recOptPool.Put(o) |
| 539 |
}() |
| 540 |
|
| 541 |
*o = append( |
| 542 |
*o, |
| 543 |
metric.WithAttributes( |
| 544 |
append( |
| 545 |
attrs, |
| 546 |
attribute.String("gen_ai.operation.name", string(operationName)), |
| 547 |
attribute.String("gen_ai.system", string(system)), |
| 548 |
)..., |
| 549 |
), |
| 550 |
) |
| 551 |
|
| 552 |
m.Float64Histogram.Record(ctx, val, *o...) |
| 553 |
} |
| 554 |
|
| 555 |
// AttrRequestModel returns an optional attribute for the "gen_ai.request.model" |
| 556 |
// semantic convention. It represents the name of the GenAI model a request is |
| 557 |
// being made to. |
| 558 |
func (ServerTimePerOutputToken) AttrRequestModel(val string) attribute.KeyValue { |
| 559 |
return attribute.String("gen_ai.request.model", val) |
| 560 |
} |
| 561 |
|
| 562 |
// AttrServerPort returns an optional attribute for the "server.port" semantic |
| 563 |
// convention. It represents the genAI server port. |
| 564 |
func (ServerTimePerOutputToken) AttrServerPort(val int) attribute.KeyValue { |
| 565 |
return attribute.Int("server.port", val) |
| 566 |
} |
| 567 |
|
| 568 |
// AttrResponseModel returns an optional attribute for the |
| 569 |
// "gen_ai.response.model" semantic convention. It represents the name of the |
| 570 |
// model that generated the response. |
| 571 |
func (ServerTimePerOutputToken) AttrResponseModel(val string) attribute.KeyValue { |
| 572 |
return attribute.String("gen_ai.response.model", val) |
| 573 |
} |
| 574 |
|
| 575 |
// AttrServerAddress returns an optional attribute for the "server.address" |
| 576 |
// semantic convention. It represents the genAI server address. |
| 577 |
func (ServerTimePerOutputToken) AttrServerAddress(val string) attribute.KeyValue { |
| 578 |
return attribute.String("server.address", val) |
| 579 |
} |
| 580 |
|
| 581 |
// ServerTimeToFirstToken is an instrument used to record metric values |
| 582 |
// conforming to the "gen_ai.server.time_to_first_token" semantic conventions. It |
| 583 |
// represents the time to generate first token for successful responses. |
| 584 |
type ServerTimeToFirstToken struct { |
| 585 |
metric.Float64Histogram |
| 586 |
} |
| 587 |
|
| 588 |
// NewServerTimeToFirstToken returns a new ServerTimeToFirstToken instrument. |
| 589 |
func NewServerTimeToFirstToken( |
| 590 |
m metric.Meter, |
| 591 |
opt ...metric.Float64HistogramOption, |
| 592 |
) (ServerTimeToFirstToken, error) { |
| 593 |
// Check if the meter is nil. |
| 594 |
if m == nil { |
| 595 |
return ServerTimeToFirstToken{noop.Float64Histogram{}}, nil |
| 596 |
} |
| 597 |
|
| 598 |
i, err := m.Float64Histogram( |
| 599 |
"gen_ai.server.time_to_first_token", |
| 600 |
append([]metric.Float64HistogramOption{ |
| 601 |
metric.WithDescription("Time to generate first token for successful responses"), |
| 602 |
metric.WithUnit("s"), |
| 603 |
}, opt...)..., |
| 604 |
) |
| 605 |
if err != nil { |
| 606 |
return ServerTimeToFirstToken{noop.Float64Histogram{}}, err |
| 607 |
} |
| 608 |
return ServerTimeToFirstToken{i}, nil |
| 609 |
} |
| 610 |
|
| 611 |
// Inst returns the underlying metric instrument. |
| 612 |
func (m ServerTimeToFirstToken) Inst() metric.Float64Histogram { |
| 613 |
return m.Float64Histogram |
| 614 |
} |
| 615 |
|
| 616 |
// Name returns the semantic convention name of the instrument. |
| 617 |
func (ServerTimeToFirstToken) Name() string { |
| 618 |
return "gen_ai.server.time_to_first_token" |
| 619 |
} |
| 620 |
|
| 621 |
// Unit returns the semantic convention unit of the instrument |
| 622 |
func (ServerTimeToFirstToken) Unit() string { |
| 623 |
return "s" |
| 624 |
} |
| 625 |
|
| 626 |
// Description returns the semantic convention description of the instrument |
| 627 |
func (ServerTimeToFirstToken) Description() string { |
| 628 |
return "Time to generate first token for successful responses" |
| 629 |
} |
| 630 |
|
| 631 |
// Record records val to the current distribution. |
| 632 |
// |
| 633 |
// The operationName is the the name of the operation being performed. |
| 634 |
// |
| 635 |
// The system is the the Generative AI product as identified by the client or |
| 636 |
// server instrumentation. |
| 637 |
// |
| 638 |
// All additional attrs passed are included in the recorded value. |
| 639 |
func (m ServerTimeToFirstToken) Record( |
| 640 |
ctx context.Context, |
| 641 |
val float64, |
| 642 |
operationName OperationNameAttr, |
| 643 |
system SystemAttr, |
| 644 |
attrs ...attribute.KeyValue, |
| 645 |
) { |
| 646 |
o := recOptPool.Get().(*[]metric.RecordOption) |
| 647 |
defer func() { |
| 648 |
*o = (*o)[:0] |
| 649 |
recOptPool.Put(o) |
| 650 |
}() |
| 651 |
|
| 652 |
*o = append( |
| 653 |
*o, |
| 654 |
metric.WithAttributes( |
| 655 |
append( |
| 656 |
attrs, |
| 657 |
attribute.String("gen_ai.operation.name", string(operationName)), |
| 658 |
attribute.String("gen_ai.system", string(system)), |
| 659 |
)..., |
| 660 |
), |
| 661 |
) |
| 662 |
|
| 663 |
m.Float64Histogram.Record(ctx, val, *o...) |
| 664 |
} |
| 665 |
|
| 666 |
// AttrRequestModel returns an optional attribute for the "gen_ai.request.model" |
| 667 |
// semantic convention. It represents the name of the GenAI model a request is |
| 668 |
// being made to. |
| 669 |
func (ServerTimeToFirstToken) AttrRequestModel(val string) attribute.KeyValue { |
| 670 |
return attribute.String("gen_ai.request.model", val) |
| 671 |
} |
| 672 |
|
| 673 |
// AttrServerPort returns an optional attribute for the "server.port" semantic |
| 674 |
// convention. It represents the genAI server port. |
| 675 |
func (ServerTimeToFirstToken) AttrServerPort(val int) attribute.KeyValue { |
| 676 |
return attribute.Int("server.port", val) |
| 677 |
} |
| 678 |
|
| 679 |
// AttrResponseModel returns an optional attribute for the |
| 680 |
// "gen_ai.response.model" semantic convention. It represents the name of the |
| 681 |
// model that generated the response. |
| 682 |
func (ServerTimeToFirstToken) AttrResponseModel(val string) attribute.KeyValue { |
| 683 |
return attribute.String("gen_ai.response.model", val) |
| 684 |
} |
| 685 |
|
| 686 |
// AttrServerAddress returns an optional attribute for the "server.address" |
| 687 |
// semantic convention. It represents the genAI server address. |
| 688 |
func (ServerTimeToFirstToken) AttrServerAddress(val string) attribute.KeyValue { |
| 689 |
return attribute.String("server.address", val) |
| 690 |
} |