Debian Codegraph @

semconv/v1.39.0/genaiconv/metric.go

raw
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 // ProviderNameAttr is an attribute conforming to the gen_ai.provider.name
69 // semantic conventions. It represents the Generative AI provider as identified
70 // by the client or server instrumentation.
71 type ProviderNameAttr string
72
73 var (
74 // ProviderNameOpenAI is the [OpenAI].
75 //
76 // [OpenAI]: https://openai.com/
77 ProviderNameOpenAI ProviderNameAttr = "openai"
78 // ProviderNameGCPGenAI is the any Google generative AI endpoint.
79 ProviderNameGCPGenAI ProviderNameAttr = "gcp.gen_ai"
80 // ProviderNameGCPVertexAI is the [Vertex AI].
81 //
82 // [Vertex AI]: https://cloud.google.com/vertex-ai
83 ProviderNameGCPVertexAI ProviderNameAttr = "gcp.vertex_ai"
84 // ProviderNameGCPGemini is the [Gemini].
85 //
86 // [Gemini]: https://cloud.google.com/products/gemini
87 ProviderNameGCPGemini ProviderNameAttr = "gcp.gemini"
88 // ProviderNameAnthropic is the [Anthropic].
89 //
90 // [Anthropic]: https://www.anthropic.com/
91 ProviderNameAnthropic ProviderNameAttr = "anthropic"
92 // ProviderNameCohere is the [Cohere].
93 //
94 // [Cohere]: https://cohere.com/
95 ProviderNameCohere ProviderNameAttr = "cohere"
96 // ProviderNameAzureAIInference is the azure AI Inference.
97 ProviderNameAzureAIInference ProviderNameAttr = "azure.ai.inference"
98 // ProviderNameAzureAIOpenAI is the [Azure OpenAI].
99 //
100 // [Azure OpenAI]: https://azure.microsoft.com/products/ai-services/openai-service/
101 ProviderNameAzureAIOpenAI ProviderNameAttr = "azure.ai.openai"
102 // ProviderNameIBMWatsonxAI is the [IBM Watsonx AI].
103 //
104 // [IBM Watsonx AI]: https://www.ibm.com/products/watsonx-ai
105 ProviderNameIBMWatsonxAI ProviderNameAttr = "ibm.watsonx.ai"
106 // ProviderNameAWSBedrock is the [AWS Bedrock].
107 //
108 // [AWS Bedrock]: https://aws.amazon.com/bedrock
109 ProviderNameAWSBedrock ProviderNameAttr = "aws.bedrock"
110 // ProviderNamePerplexity is the [Perplexity].
111 //
112 // [Perplexity]: https://www.perplexity.ai/
113 ProviderNamePerplexity ProviderNameAttr = "perplexity"
114 // ProviderNameXAI is the [xAI].
115 //
116 // [xAI]: https://x.ai/
117 ProviderNameXAI ProviderNameAttr = "x_ai"
118 // ProviderNameDeepseek is the [DeepSeek].
119 //
120 // [DeepSeek]: https://www.deepseek.com/
121 ProviderNameDeepseek ProviderNameAttr = "deepseek"
122 // ProviderNameGroq is the [Groq].
123 //
124 // [Groq]: https://groq.com/
125 ProviderNameGroq ProviderNameAttr = "groq"
126 // ProviderNameMistralAI is the [Mistral AI].
127 //
128 // [Mistral AI]: https://mistral.ai/
129 ProviderNameMistralAI ProviderNameAttr = "mistral_ai"
130 )
131
132 // TokenTypeAttr is an attribute conforming to the gen_ai.token.type semantic
133 // conventions. It represents the type of token being counted.
134 type TokenTypeAttr string
135
136 var (
137 // TokenTypeInput is the input tokens (prompt, input, etc.).
138 TokenTypeInput TokenTypeAttr = "input"
139 // TokenTypeOutput is the output tokens (completion, response, etc.).
140 TokenTypeOutput TokenTypeAttr = "output"
141 )
142
143 // ClientOperationDuration is an instrument used to record metric values
144 // conforming to the "gen_ai.client.operation.duration" semantic conventions. It
145 // represents the genAI operation duration.
146 type ClientOperationDuration struct {
147 metric.Float64Histogram
148 }
149
150 var newClientOperationDurationOpts = []metric.Float64HistogramOption{
151 metric.WithDescription("GenAI operation duration."),
152 metric.WithUnit("s"),
153 }
154
155 // NewClientOperationDuration returns a new ClientOperationDuration instrument.
156 func NewClientOperationDuration(
157 m metric.Meter,
158 opt ...metric.Float64HistogramOption,
159 ) (ClientOperationDuration, error) {
160 // Check if the meter is nil.
161 if m == nil {
162 return ClientOperationDuration{noop.Float64Histogram{}}, nil
163 }
164
165 if len(opt) == 0 {
166 opt = newClientOperationDurationOpts
167 } else {
168 opt = append(opt, newClientOperationDurationOpts...)
169 }
170
171 i, err := m.Float64Histogram(
172 "gen_ai.client.operation.duration",
173 opt...,
174 )
175 if err != nil {
176 return ClientOperationDuration{noop.Float64Histogram{}}, err
177 }
178 return ClientOperationDuration{i}, nil
179 }
180
181 // Inst returns the underlying metric instrument.
182 func (m ClientOperationDuration) Inst() metric.Float64Histogram {
183 return m.Float64Histogram
184 }
185
186 // Name returns the semantic convention name of the instrument.
187 func (ClientOperationDuration) Name() string {
188 return "gen_ai.client.operation.duration"
189 }
190
191 // Unit returns the semantic convention unit of the instrument
192 func (ClientOperationDuration) Unit() string {
193 return "s"
194 }
195
196 // Description returns the semantic convention description of the instrument
197 func (ClientOperationDuration) Description() string {
198 return "GenAI operation duration."
199 }
200
201 // Record records val to the current distribution for attrs.
202 //
203 // The operationName is the the name of the operation being performed.
204 //
205 // The providerName is the the Generative AI provider as identified by the client
206 // or server instrumentation.
207 //
208 // All additional attrs passed are included in the recorded value.
209 func (m ClientOperationDuration) Record(
210 ctx context.Context,
211 val float64,
212 operationName OperationNameAttr,
213 providerName ProviderNameAttr,
214 attrs ...attribute.KeyValue,
215 ) {
216 if len(attrs) == 0 {
217 m.Float64Histogram.Record(ctx, val, metric.WithAttributes(
218 attribute.String("gen_ai.operation.name", string(operationName)),
219 attribute.String("gen_ai.provider.name", string(providerName)),
220 ))
221 return
222 }
223
224 o := recOptPool.Get().(*[]metric.RecordOption)
225 defer func() {
226 *o = (*o)[:0]
227 recOptPool.Put(o)
228 }()
229
230 *o = append(
231 *o,
232 metric.WithAttributes(
233 append(
234 attrs,
235 attribute.String("gen_ai.operation.name", string(operationName)),
236 attribute.String("gen_ai.provider.name", string(providerName)),
237 )...,
238 ),
239 )
240
241 m.Float64Histogram.Record(ctx, val, *o...)
242 }
243
244 // RecordSet records val to the current distribution for set.
245 func (m ClientOperationDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
246 if set.Len() == 0 {
247 m.Float64Histogram.Record(ctx, val)
248 return
249 }
250
251 o := recOptPool.Get().(*[]metric.RecordOption)
252 defer func() {
253 *o = (*o)[:0]
254 recOptPool.Put(o)
255 }()
256
257 *o = append(*o, metric.WithAttributeSet(set))
258 m.Float64Histogram.Record(ctx, val, *o...)
259 }
260
261 // AttrErrorType returns an optional attribute for the "error.type" semantic
262 // convention. It represents the describes a class of error the operation ended
263 // with.
264 func (ClientOperationDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
265 return attribute.String("error.type", string(val))
266 }
267
268 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
269 // semantic convention. It represents the name of the GenAI model a request is
270 // being made to.
271 func (ClientOperationDuration) AttrRequestModel(val string) attribute.KeyValue {
272 return attribute.String("gen_ai.request.model", val)
273 }
274
275 // AttrServerPort returns an optional attribute for the "server.port" semantic
276 // convention. It represents the genAI server port.
277 func (ClientOperationDuration) AttrServerPort(val int) attribute.KeyValue {
278 return attribute.Int("server.port", val)
279 }
280
281 // AttrResponseModel returns an optional attribute for the
282 // "gen_ai.response.model" semantic convention. It represents the name of the
283 // model that generated the response.
284 func (ClientOperationDuration) AttrResponseModel(val string) attribute.KeyValue {
285 return attribute.String("gen_ai.response.model", val)
286 }
287
288 // AttrServerAddress returns an optional attribute for the "server.address"
289 // semantic convention. It represents the genAI server address.
290 func (ClientOperationDuration) AttrServerAddress(val string) attribute.KeyValue {
291 return attribute.String("server.address", val)
292 }
293
294 // ClientTokenUsage is an instrument used to record metric values conforming to
295 // the "gen_ai.client.token.usage" semantic conventions. It represents the number
296 // of input and output tokens used.
297 type ClientTokenUsage struct {
298 metric.Int64Histogram
299 }
300
301 var newClientTokenUsageOpts = []metric.Int64HistogramOption{
302 metric.WithDescription("Number of input and output tokens used."),
303 metric.WithUnit("{token}"),
304 }
305
306 // NewClientTokenUsage returns a new ClientTokenUsage instrument.
307 func NewClientTokenUsage(
308 m metric.Meter,
309 opt ...metric.Int64HistogramOption,
310 ) (ClientTokenUsage, error) {
311 // Check if the meter is nil.
312 if m == nil {
313 return ClientTokenUsage{noop.Int64Histogram{}}, nil
314 }
315
316 if len(opt) == 0 {
317 opt = newClientTokenUsageOpts
318 } else {
319 opt = append(opt, newClientTokenUsageOpts...)
320 }
321
322 i, err := m.Int64Histogram(
323 "gen_ai.client.token.usage",
324 opt...,
325 )
326 if err != nil {
327 return ClientTokenUsage{noop.Int64Histogram{}}, err
328 }
329 return ClientTokenUsage{i}, nil
330 }
331
332 // Inst returns the underlying metric instrument.
333 func (m ClientTokenUsage) Inst() metric.Int64Histogram {
334 return m.Int64Histogram
335 }
336
337 // Name returns the semantic convention name of the instrument.
338 func (ClientTokenUsage) Name() string {
339 return "gen_ai.client.token.usage"
340 }
341
342 // Unit returns the semantic convention unit of the instrument
343 func (ClientTokenUsage) Unit() string {
344 return "{token}"
345 }
346
347 // Description returns the semantic convention description of the instrument
348 func (ClientTokenUsage) Description() string {
349 return "Number of input and output tokens used."
350 }
351
352 // Record records val to the current distribution for attrs.
353 //
354 // The operationName is the the name of the operation being performed.
355 //
356 // The providerName is the the Generative AI provider as identified by the client
357 // or server instrumentation.
358 //
359 // The tokenType is the the type of token being counted.
360 //
361 // All additional attrs passed are included in the recorded value.
362 func (m ClientTokenUsage) Record(
363 ctx context.Context,
364 val int64,
365 operationName OperationNameAttr,
366 providerName ProviderNameAttr,
367 tokenType TokenTypeAttr,
368 attrs ...attribute.KeyValue,
369 ) {
370 if len(attrs) == 0 {
371 m.Int64Histogram.Record(ctx, val, metric.WithAttributes(
372 attribute.String("gen_ai.operation.name", string(operationName)),
373 attribute.String("gen_ai.provider.name", string(providerName)),
374 attribute.String("gen_ai.token.type", string(tokenType)),
375 ))
376 return
377 }
378
379 o := recOptPool.Get().(*[]metric.RecordOption)
380 defer func() {
381 *o = (*o)[:0]
382 recOptPool.Put(o)
383 }()
384
385 *o = append(
386 *o,
387 metric.WithAttributes(
388 append(
389 attrs,
390 attribute.String("gen_ai.operation.name", string(operationName)),
391 attribute.String("gen_ai.provider.name", string(providerName)),
392 attribute.String("gen_ai.token.type", string(tokenType)),
393 )...,
394 ),
395 )
396
397 m.Int64Histogram.Record(ctx, val, *o...)
398 }
399
400 // RecordSet records val to the current distribution for set.
401 func (m ClientTokenUsage) RecordSet(ctx context.Context, val int64, set attribute.Set) {
402 if set.Len() == 0 {
403 m.Int64Histogram.Record(ctx, val)
404 return
405 }
406
407 o := recOptPool.Get().(*[]metric.RecordOption)
408 defer func() {
409 *o = (*o)[:0]
410 recOptPool.Put(o)
411 }()
412
413 *o = append(*o, metric.WithAttributeSet(set))
414 m.Int64Histogram.Record(ctx, val, *o...)
415 }
416
417 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
418 // semantic convention. It represents the name of the GenAI model a request is
419 // being made to.
420 func (ClientTokenUsage) AttrRequestModel(val string) attribute.KeyValue {
421 return attribute.String("gen_ai.request.model", val)
422 }
423
424 // AttrServerPort returns an optional attribute for the "server.port" semantic
425 // convention. It represents the genAI server port.
426 func (ClientTokenUsage) AttrServerPort(val int) attribute.KeyValue {
427 return attribute.Int("server.port", val)
428 }
429
430 // AttrResponseModel returns an optional attribute for the
431 // "gen_ai.response.model" semantic convention. It represents the name of the
432 // model that generated the response.
433 func (ClientTokenUsage) AttrResponseModel(val string) attribute.KeyValue {
434 return attribute.String("gen_ai.response.model", val)
435 }
436
437 // AttrServerAddress returns an optional attribute for the "server.address"
438 // semantic convention. It represents the genAI server address.
439 func (ClientTokenUsage) AttrServerAddress(val string) attribute.KeyValue {
440 return attribute.String("server.address", val)
441 }
442
443 // ServerRequestDuration is an instrument used to record metric values conforming
444 // to the "gen_ai.server.request.duration" semantic conventions. It represents
445 // the generative AI server request duration such as time-to-last byte or last
446 // output token.
447 type ServerRequestDuration struct {
448 metric.Float64Histogram
449 }
450
451 var newServerRequestDurationOpts = []metric.Float64HistogramOption{
452 metric.WithDescription("Generative AI server request duration such as time-to-last byte or last output token."),
453 metric.WithUnit("s"),
454 }
455
456 // NewServerRequestDuration returns a new ServerRequestDuration instrument.
457 func NewServerRequestDuration(
458 m metric.Meter,
459 opt ...metric.Float64HistogramOption,
460 ) (ServerRequestDuration, error) {
461 // Check if the meter is nil.
462 if m == nil {
463 return ServerRequestDuration{noop.Float64Histogram{}}, nil
464 }
465
466 if len(opt) == 0 {
467 opt = newServerRequestDurationOpts
468 } else {
469 opt = append(opt, newServerRequestDurationOpts...)
470 }
471
472 i, err := m.Float64Histogram(
473 "gen_ai.server.request.duration",
474 opt...,
475 )
476 if err != nil {
477 return ServerRequestDuration{noop.Float64Histogram{}}, err
478 }
479 return ServerRequestDuration{i}, nil
480 }
481
482 // Inst returns the underlying metric instrument.
483 func (m ServerRequestDuration) Inst() metric.Float64Histogram {
484 return m.Float64Histogram
485 }
486
487 // Name returns the semantic convention name of the instrument.
488 func (ServerRequestDuration) Name() string {
489 return "gen_ai.server.request.duration"
490 }
491
492 // Unit returns the semantic convention unit of the instrument
493 func (ServerRequestDuration) Unit() string {
494 return "s"
495 }
496
497 // Description returns the semantic convention description of the instrument
498 func (ServerRequestDuration) Description() string {
499 return "Generative AI server request duration such as time-to-last byte or last output token."
500 }
501
502 // Record records val to the current distribution for attrs.
503 //
504 // The operationName is the the name of the operation being performed.
505 //
506 // The providerName is the the Generative AI provider as identified by the client
507 // or server instrumentation.
508 //
509 // All additional attrs passed are included in the recorded value.
510 func (m ServerRequestDuration) Record(
511 ctx context.Context,
512 val float64,
513 operationName OperationNameAttr,
514 providerName ProviderNameAttr,
515 attrs ...attribute.KeyValue,
516 ) {
517 if len(attrs) == 0 {
518 m.Float64Histogram.Record(ctx, val, metric.WithAttributes(
519 attribute.String("gen_ai.operation.name", string(operationName)),
520 attribute.String("gen_ai.provider.name", string(providerName)),
521 ))
522 return
523 }
524
525 o := recOptPool.Get().(*[]metric.RecordOption)
526 defer func() {
527 *o = (*o)[:0]
528 recOptPool.Put(o)
529 }()
530
531 *o = append(
532 *o,
533 metric.WithAttributes(
534 append(
535 attrs,
536 attribute.String("gen_ai.operation.name", string(operationName)),
537 attribute.String("gen_ai.provider.name", string(providerName)),
538 )...,
539 ),
540 )
541
542 m.Float64Histogram.Record(ctx, val, *o...)
543 }
544
545 // RecordSet records val to the current distribution for set.
546 func (m ServerRequestDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
547 if set.Len() == 0 {
548 m.Float64Histogram.Record(ctx, val)
549 return
550 }
551
552 o := recOptPool.Get().(*[]metric.RecordOption)
553 defer func() {
554 *o = (*o)[:0]
555 recOptPool.Put(o)
556 }()
557
558 *o = append(*o, metric.WithAttributeSet(set))
559 m.Float64Histogram.Record(ctx, val, *o...)
560 }
561
562 // AttrErrorType returns an optional attribute for the "error.type" semantic
563 // convention. It represents the describes a class of error the operation ended
564 // with.
565 func (ServerRequestDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
566 return attribute.String("error.type", string(val))
567 }
568
569 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
570 // semantic convention. It represents the name of the GenAI model a request is
571 // being made to.
572 func (ServerRequestDuration) AttrRequestModel(val string) attribute.KeyValue {
573 return attribute.String("gen_ai.request.model", val)
574 }
575
576 // AttrServerPort returns an optional attribute for the "server.port" semantic
577 // convention. It represents the genAI server port.
578 func (ServerRequestDuration) AttrServerPort(val int) attribute.KeyValue {
579 return attribute.Int("server.port", val)
580 }
581
582 // AttrResponseModel returns an optional attribute for the
583 // "gen_ai.response.model" semantic convention. It represents the name of the
584 // model that generated the response.
585 func (ServerRequestDuration) AttrResponseModel(val string) attribute.KeyValue {
586 return attribute.String("gen_ai.response.model", val)
587 }
588
589 // AttrServerAddress returns an optional attribute for the "server.address"
590 // semantic convention. It represents the genAI server address.
591 func (ServerRequestDuration) AttrServerAddress(val string) attribute.KeyValue {
592 return attribute.String("server.address", val)
593 }
594
595 // ServerTimePerOutputToken is an instrument used to record metric values
596 // conforming to the "gen_ai.server.time_per_output_token" semantic conventions.
597 // It represents the time per output token generated after the first token for
598 // successful responses.
599 type ServerTimePerOutputToken struct {
600 metric.Float64Histogram
601 }
602
603 var newServerTimePerOutputTokenOpts = []metric.Float64HistogramOption{
604 metric.WithDescription("Time per output token generated after the first token for successful responses."),
605 metric.WithUnit("s"),
606 }
607
608 // NewServerTimePerOutputToken returns a new ServerTimePerOutputToken instrument.
609 func NewServerTimePerOutputToken(
610 m metric.Meter,
611 opt ...metric.Float64HistogramOption,
612 ) (ServerTimePerOutputToken, error) {
613 // Check if the meter is nil.
614 if m == nil {
615 return ServerTimePerOutputToken{noop.Float64Histogram{}}, nil
616 }
617
618 if len(opt) == 0 {
619 opt = newServerTimePerOutputTokenOpts
620 } else {
621 opt = append(opt, newServerTimePerOutputTokenOpts...)
622 }
623
624 i, err := m.Float64Histogram(
625 "gen_ai.server.time_per_output_token",
626 opt...,
627 )
628 if err != nil {
629 return ServerTimePerOutputToken{noop.Float64Histogram{}}, err
630 }
631 return ServerTimePerOutputToken{i}, nil
632 }
633
634 // Inst returns the underlying metric instrument.
635 func (m ServerTimePerOutputToken) Inst() metric.Float64Histogram {
636 return m.Float64Histogram
637 }
638
639 // Name returns the semantic convention name of the instrument.
640 func (ServerTimePerOutputToken) Name() string {
641 return "gen_ai.server.time_per_output_token"
642 }
643
644 // Unit returns the semantic convention unit of the instrument
645 func (ServerTimePerOutputToken) Unit() string {
646 return "s"
647 }
648
649 // Description returns the semantic convention description of the instrument
650 func (ServerTimePerOutputToken) Description() string {
651 return "Time per output token generated after the first token for successful responses."
652 }
653
654 // Record records val to the current distribution for attrs.
655 //
656 // The operationName is the the name of the operation being performed.
657 //
658 // The providerName is the the Generative AI provider as identified by the client
659 // or server instrumentation.
660 //
661 // All additional attrs passed are included in the recorded value.
662 func (m ServerTimePerOutputToken) Record(
663 ctx context.Context,
664 val float64,
665 operationName OperationNameAttr,
666 providerName ProviderNameAttr,
667 attrs ...attribute.KeyValue,
668 ) {
669 if len(attrs) == 0 {
670 m.Float64Histogram.Record(ctx, val, metric.WithAttributes(
671 attribute.String("gen_ai.operation.name", string(operationName)),
672 attribute.String("gen_ai.provider.name", string(providerName)),
673 ))
674 return
675 }
676
677 o := recOptPool.Get().(*[]metric.RecordOption)
678 defer func() {
679 *o = (*o)[:0]
680 recOptPool.Put(o)
681 }()
682
683 *o = append(
684 *o,
685 metric.WithAttributes(
686 append(
687 attrs,
688 attribute.String("gen_ai.operation.name", string(operationName)),
689 attribute.String("gen_ai.provider.name", string(providerName)),
690 )...,
691 ),
692 )
693
694 m.Float64Histogram.Record(ctx, val, *o...)
695 }
696
697 // RecordSet records val to the current distribution for set.
698 func (m ServerTimePerOutputToken) RecordSet(ctx context.Context, val float64, set attribute.Set) {
699 if set.Len() == 0 {
700 m.Float64Histogram.Record(ctx, val)
701 return
702 }
703
704 o := recOptPool.Get().(*[]metric.RecordOption)
705 defer func() {
706 *o = (*o)[:0]
707 recOptPool.Put(o)
708 }()
709
710 *o = append(*o, metric.WithAttributeSet(set))
711 m.Float64Histogram.Record(ctx, val, *o...)
712 }
713
714 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
715 // semantic convention. It represents the name of the GenAI model a request is
716 // being made to.
717 func (ServerTimePerOutputToken) AttrRequestModel(val string) attribute.KeyValue {
718 return attribute.String("gen_ai.request.model", val)
719 }
720
721 // AttrServerPort returns an optional attribute for the "server.port" semantic
722 // convention. It represents the genAI server port.
723 func (ServerTimePerOutputToken) AttrServerPort(val int) attribute.KeyValue {
724 return attribute.Int("server.port", val)
725 }
726
727 // AttrResponseModel returns an optional attribute for the
728 // "gen_ai.response.model" semantic convention. It represents the name of the
729 // model that generated the response.
730 func (ServerTimePerOutputToken) AttrResponseModel(val string) attribute.KeyValue {
731 return attribute.String("gen_ai.response.model", val)
732 }
733
734 // AttrServerAddress returns an optional attribute for the "server.address"
735 // semantic convention. It represents the genAI server address.
736 func (ServerTimePerOutputToken) AttrServerAddress(val string) attribute.KeyValue {
737 return attribute.String("server.address", val)
738 }
739
740 // ServerTimeToFirstToken is an instrument used to record metric values
741 // conforming to the "gen_ai.server.time_to_first_token" semantic conventions. It
742 // represents the time to generate first token for successful responses.
743 type ServerTimeToFirstToken struct {
744 metric.Float64Histogram
745 }
746
747 var newServerTimeToFirstTokenOpts = []metric.Float64HistogramOption{
748 metric.WithDescription("Time to generate first token for successful responses."),
749 metric.WithUnit("s"),
750 }
751
752 // NewServerTimeToFirstToken returns a new ServerTimeToFirstToken instrument.
753 func NewServerTimeToFirstToken(
754 m metric.Meter,
755 opt ...metric.Float64HistogramOption,
756 ) (ServerTimeToFirstToken, error) {
757 // Check if the meter is nil.
758 if m == nil {
759 return ServerTimeToFirstToken{noop.Float64Histogram{}}, nil
760 }
761
762 if len(opt) == 0 {
763 opt = newServerTimeToFirstTokenOpts
764 } else {
765 opt = append(opt, newServerTimeToFirstTokenOpts...)
766 }
767
768 i, err := m.Float64Histogram(
769 "gen_ai.server.time_to_first_token",
770 opt...,
771 )
772 if err != nil {
773 return ServerTimeToFirstToken{noop.Float64Histogram{}}, err
774 }
775 return ServerTimeToFirstToken{i}, nil
776 }
777
778 // Inst returns the underlying metric instrument.
779 func (m ServerTimeToFirstToken) Inst() metric.Float64Histogram {
780 return m.Float64Histogram
781 }
782
783 // Name returns the semantic convention name of the instrument.
784 func (ServerTimeToFirstToken) Name() string {
785 return "gen_ai.server.time_to_first_token"
786 }
787
788 // Unit returns the semantic convention unit of the instrument
789 func (ServerTimeToFirstToken) Unit() string {
790 return "s"
791 }
792
793 // Description returns the semantic convention description of the instrument
794 func (ServerTimeToFirstToken) Description() string {
795 return "Time to generate first token for successful responses."
796 }
797
798 // Record records val to the current distribution for attrs.
799 //
800 // The operationName is the the name of the operation being performed.
801 //
802 // The providerName is the the Generative AI provider as identified by the client
803 // or server instrumentation.
804 //
805 // All additional attrs passed are included in the recorded value.
806 func (m ServerTimeToFirstToken) Record(
807 ctx context.Context,
808 val float64,
809 operationName OperationNameAttr,
810 providerName ProviderNameAttr,
811 attrs ...attribute.KeyValue,
812 ) {
813 if len(attrs) == 0 {
814 m.Float64Histogram.Record(ctx, val, metric.WithAttributes(
815 attribute.String("gen_ai.operation.name", string(operationName)),
816 attribute.String("gen_ai.provider.name", string(providerName)),
817 ))
818 return
819 }
820
821 o := recOptPool.Get().(*[]metric.RecordOption)
822 defer func() {
823 *o = (*o)[:0]
824 recOptPool.Put(o)
825 }()
826
827 *o = append(
828 *o,
829 metric.WithAttributes(
830 append(
831 attrs,
832 attribute.String("gen_ai.operation.name", string(operationName)),
833 attribute.String("gen_ai.provider.name", string(providerName)),
834 )...,
835 ),
836 )
837
838 m.Float64Histogram.Record(ctx, val, *o...)
839 }
840
841 // RecordSet records val to the current distribution for set.
842 func (m ServerTimeToFirstToken) RecordSet(ctx context.Context, val float64, set attribute.Set) {
843 if set.Len() == 0 {
844 m.Float64Histogram.Record(ctx, val)
845 return
846 }
847
848 o := recOptPool.Get().(*[]metric.RecordOption)
849 defer func() {
850 *o = (*o)[:0]
851 recOptPool.Put(o)
852 }()
853
854 *o = append(*o, metric.WithAttributeSet(set))
855 m.Float64Histogram.Record(ctx, val, *o...)
856 }
857
858 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
859 // semantic convention. It represents the name of the GenAI model a request is
860 // being made to.
861 func (ServerTimeToFirstToken) AttrRequestModel(val string) attribute.KeyValue {
862 return attribute.String("gen_ai.request.model", val)
863 }
864
865 // AttrServerPort returns an optional attribute for the "server.port" semantic
866 // convention. It represents the genAI server port.
867 func (ServerTimeToFirstToken) AttrServerPort(val int) attribute.KeyValue {
868 return attribute.Int("server.port", val)
869 }
870
871 // AttrResponseModel returns an optional attribute for the
872 // "gen_ai.response.model" semantic convention. It represents the name of the
873 // model that generated the response.
874 func (ServerTimeToFirstToken) AttrResponseModel(val string) attribute.KeyValue {
875 return attribute.String("gen_ai.response.model", val)
876 }
877
878 // AttrServerAddress returns an optional attribute for the "server.address"
879 // semantic convention. It represents the genAI server address.
880 func (ServerTimeToFirstToken) AttrServerAddress(val string) attribute.KeyValue {
881 return attribute.String("server.address", val)
882 }