Debian Codegraph @

semconv/v1.37.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)
218 return
219 }
220
221 o := recOptPool.Get().(*[]metric.RecordOption)
222 defer func() {
223 *o = (*o)[:0]
224 recOptPool.Put(o)
225 }()
226
227 *o = append(
228 *o,
229 metric.WithAttributes(
230 append(
231 attrs,
232 attribute.String("gen_ai.operation.name", string(operationName)),
233 attribute.String("gen_ai.provider.name", string(providerName)),
234 )...,
235 ),
236 )
237
238 m.Float64Histogram.Record(ctx, val, *o...)
239 }
240
241 // RecordSet records val to the current distribution for set.
242 func (m ClientOperationDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
243 if set.Len() == 0 {
244 m.Float64Histogram.Record(ctx, val)
245 return
246 }
247
248 o := recOptPool.Get().(*[]metric.RecordOption)
249 defer func() {
250 *o = (*o)[:0]
251 recOptPool.Put(o)
252 }()
253
254 *o = append(*o, metric.WithAttributeSet(set))
255 m.Float64Histogram.Record(ctx, val, *o...)
256 }
257
258 // AttrErrorType returns an optional attribute for the "error.type" semantic
259 // convention. It represents the describes a class of error the operation ended
260 // with.
261 func (ClientOperationDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
262 return attribute.String("error.type", string(val))
263 }
264
265 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
266 // semantic convention. It represents the name of the GenAI model a request is
267 // being made to.
268 func (ClientOperationDuration) AttrRequestModel(val string) attribute.KeyValue {
269 return attribute.String("gen_ai.request.model", val)
270 }
271
272 // AttrServerPort returns an optional attribute for the "server.port" semantic
273 // convention. It represents the genAI server port.
274 func (ClientOperationDuration) AttrServerPort(val int) attribute.KeyValue {
275 return attribute.Int("server.port", val)
276 }
277
278 // AttrResponseModel returns an optional attribute for the
279 // "gen_ai.response.model" semantic convention. It represents the name of the
280 // model that generated the response.
281 func (ClientOperationDuration) AttrResponseModel(val string) attribute.KeyValue {
282 return attribute.String("gen_ai.response.model", val)
283 }
284
285 // AttrServerAddress returns an optional attribute for the "server.address"
286 // semantic convention. It represents the genAI server address.
287 func (ClientOperationDuration) AttrServerAddress(val string) attribute.KeyValue {
288 return attribute.String("server.address", val)
289 }
290
291 // ClientTokenUsage is an instrument used to record metric values conforming to
292 // the "gen_ai.client.token.usage" semantic conventions. It represents the number
293 // of input and output tokens used.
294 type ClientTokenUsage struct {
295 metric.Int64Histogram
296 }
297
298 var newClientTokenUsageOpts = []metric.Int64HistogramOption{
299 metric.WithDescription("Number of input and output tokens used."),
300 metric.WithUnit("{token}"),
301 }
302
303 // NewClientTokenUsage returns a new ClientTokenUsage instrument.
304 func NewClientTokenUsage(
305 m metric.Meter,
306 opt ...metric.Int64HistogramOption,
307 ) (ClientTokenUsage, error) {
308 // Check if the meter is nil.
309 if m == nil {
310 return ClientTokenUsage{noop.Int64Histogram{}}, nil
311 }
312
313 if len(opt) == 0 {
314 opt = newClientTokenUsageOpts
315 } else {
316 opt = append(opt, newClientTokenUsageOpts...)
317 }
318
319 i, err := m.Int64Histogram(
320 "gen_ai.client.token.usage",
321 opt...,
322 )
323 if err != nil {
324 return ClientTokenUsage{noop.Int64Histogram{}}, err
325 }
326 return ClientTokenUsage{i}, nil
327 }
328
329 // Inst returns the underlying metric instrument.
330 func (m ClientTokenUsage) Inst() metric.Int64Histogram {
331 return m.Int64Histogram
332 }
333
334 // Name returns the semantic convention name of the instrument.
335 func (ClientTokenUsage) Name() string {
336 return "gen_ai.client.token.usage"
337 }
338
339 // Unit returns the semantic convention unit of the instrument
340 func (ClientTokenUsage) Unit() string {
341 return "{token}"
342 }
343
344 // Description returns the semantic convention description of the instrument
345 func (ClientTokenUsage) Description() string {
346 return "Number of input and output tokens used."
347 }
348
349 // Record records val to the current distribution for attrs.
350 //
351 // The operationName is the the name of the operation being performed.
352 //
353 // The providerName is the the Generative AI provider as identified by the client
354 // or server instrumentation.
355 //
356 // The tokenType is the the type of token being counted.
357 //
358 // All additional attrs passed are included in the recorded value.
359 func (m ClientTokenUsage) Record(
360 ctx context.Context,
361 val int64,
362 operationName OperationNameAttr,
363 providerName ProviderNameAttr,
364 tokenType TokenTypeAttr,
365 attrs ...attribute.KeyValue,
366 ) {
367 if len(attrs) == 0 {
368 m.Int64Histogram.Record(ctx, val)
369 return
370 }
371
372 o := recOptPool.Get().(*[]metric.RecordOption)
373 defer func() {
374 *o = (*o)[:0]
375 recOptPool.Put(o)
376 }()
377
378 *o = append(
379 *o,
380 metric.WithAttributes(
381 append(
382 attrs,
383 attribute.String("gen_ai.operation.name", string(operationName)),
384 attribute.String("gen_ai.provider.name", string(providerName)),
385 attribute.String("gen_ai.token.type", string(tokenType)),
386 )...,
387 ),
388 )
389
390 m.Int64Histogram.Record(ctx, val, *o...)
391 }
392
393 // RecordSet records val to the current distribution for set.
394 func (m ClientTokenUsage) RecordSet(ctx context.Context, val int64, set attribute.Set) {
395 if set.Len() == 0 {
396 m.Int64Histogram.Record(ctx, val)
397 return
398 }
399
400 o := recOptPool.Get().(*[]metric.RecordOption)
401 defer func() {
402 *o = (*o)[:0]
403 recOptPool.Put(o)
404 }()
405
406 *o = append(*o, metric.WithAttributeSet(set))
407 m.Int64Histogram.Record(ctx, val, *o...)
408 }
409
410 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
411 // semantic convention. It represents the name of the GenAI model a request is
412 // being made to.
413 func (ClientTokenUsage) AttrRequestModel(val string) attribute.KeyValue {
414 return attribute.String("gen_ai.request.model", val)
415 }
416
417 // AttrServerPort returns an optional attribute for the "server.port" semantic
418 // convention. It represents the genAI server port.
419 func (ClientTokenUsage) AttrServerPort(val int) attribute.KeyValue {
420 return attribute.Int("server.port", val)
421 }
422
423 // AttrResponseModel returns an optional attribute for the
424 // "gen_ai.response.model" semantic convention. It represents the name of the
425 // model that generated the response.
426 func (ClientTokenUsage) AttrResponseModel(val string) attribute.KeyValue {
427 return attribute.String("gen_ai.response.model", val)
428 }
429
430 // AttrServerAddress returns an optional attribute for the "server.address"
431 // semantic convention. It represents the genAI server address.
432 func (ClientTokenUsage) AttrServerAddress(val string) attribute.KeyValue {
433 return attribute.String("server.address", val)
434 }
435
436 // ServerRequestDuration is an instrument used to record metric values conforming
437 // to the "gen_ai.server.request.duration" semantic conventions. It represents
438 // the generative AI server request duration such as time-to-last byte or last
439 // output token.
440 type ServerRequestDuration struct {
441 metric.Float64Histogram
442 }
443
444 var newServerRequestDurationOpts = []metric.Float64HistogramOption{
445 metric.WithDescription("Generative AI server request duration such as time-to-last byte or last output token."),
446 metric.WithUnit("s"),
447 }
448
449 // NewServerRequestDuration returns a new ServerRequestDuration instrument.
450 func NewServerRequestDuration(
451 m metric.Meter,
452 opt ...metric.Float64HistogramOption,
453 ) (ServerRequestDuration, error) {
454 // Check if the meter is nil.
455 if m == nil {
456 return ServerRequestDuration{noop.Float64Histogram{}}, nil
457 }
458
459 if len(opt) == 0 {
460 opt = newServerRequestDurationOpts
461 } else {
462 opt = append(opt, newServerRequestDurationOpts...)
463 }
464
465 i, err := m.Float64Histogram(
466 "gen_ai.server.request.duration",
467 opt...,
468 )
469 if err != nil {
470 return ServerRequestDuration{noop.Float64Histogram{}}, err
471 }
472 return ServerRequestDuration{i}, nil
473 }
474
475 // Inst returns the underlying metric instrument.
476 func (m ServerRequestDuration) Inst() metric.Float64Histogram {
477 return m.Float64Histogram
478 }
479
480 // Name returns the semantic convention name of the instrument.
481 func (ServerRequestDuration) Name() string {
482 return "gen_ai.server.request.duration"
483 }
484
485 // Unit returns the semantic convention unit of the instrument
486 func (ServerRequestDuration) Unit() string {
487 return "s"
488 }
489
490 // Description returns the semantic convention description of the instrument
491 func (ServerRequestDuration) Description() string {
492 return "Generative AI server request duration such as time-to-last byte or last output token."
493 }
494
495 // Record records val to the current distribution for attrs.
496 //
497 // The operationName is the the name of the operation being performed.
498 //
499 // The providerName is the the Generative AI provider as identified by the client
500 // or server instrumentation.
501 //
502 // All additional attrs passed are included in the recorded value.
503 func (m ServerRequestDuration) Record(
504 ctx context.Context,
505 val float64,
506 operationName OperationNameAttr,
507 providerName ProviderNameAttr,
508 attrs ...attribute.KeyValue,
509 ) {
510 if len(attrs) == 0 {
511 m.Float64Histogram.Record(ctx, val)
512 return
513 }
514
515 o := recOptPool.Get().(*[]metric.RecordOption)
516 defer func() {
517 *o = (*o)[:0]
518 recOptPool.Put(o)
519 }()
520
521 *o = append(
522 *o,
523 metric.WithAttributes(
524 append(
525 attrs,
526 attribute.String("gen_ai.operation.name", string(operationName)),
527 attribute.String("gen_ai.provider.name", string(providerName)),
528 )...,
529 ),
530 )
531
532 m.Float64Histogram.Record(ctx, val, *o...)
533 }
534
535 // RecordSet records val to the current distribution for set.
536 func (m ServerRequestDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
537 if set.Len() == 0 {
538 m.Float64Histogram.Record(ctx, val)
539 return
540 }
541
542 o := recOptPool.Get().(*[]metric.RecordOption)
543 defer func() {
544 *o = (*o)[:0]
545 recOptPool.Put(o)
546 }()
547
548 *o = append(*o, metric.WithAttributeSet(set))
549 m.Float64Histogram.Record(ctx, val, *o...)
550 }
551
552 // AttrErrorType returns an optional attribute for the "error.type" semantic
553 // convention. It represents the describes a class of error the operation ended
554 // with.
555 func (ServerRequestDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
556 return attribute.String("error.type", string(val))
557 }
558
559 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
560 // semantic convention. It represents the name of the GenAI model a request is
561 // being made to.
562 func (ServerRequestDuration) AttrRequestModel(val string) attribute.KeyValue {
563 return attribute.String("gen_ai.request.model", val)
564 }
565
566 // AttrServerPort returns an optional attribute for the "server.port" semantic
567 // convention. It represents the genAI server port.
568 func (ServerRequestDuration) AttrServerPort(val int) attribute.KeyValue {
569 return attribute.Int("server.port", val)
570 }
571
572 // AttrResponseModel returns an optional attribute for the
573 // "gen_ai.response.model" semantic convention. It represents the name of the
574 // model that generated the response.
575 func (ServerRequestDuration) AttrResponseModel(val string) attribute.KeyValue {
576 return attribute.String("gen_ai.response.model", val)
577 }
578
579 // AttrServerAddress returns an optional attribute for the "server.address"
580 // semantic convention. It represents the genAI server address.
581 func (ServerRequestDuration) AttrServerAddress(val string) attribute.KeyValue {
582 return attribute.String("server.address", val)
583 }
584
585 // ServerTimePerOutputToken is an instrument used to record metric values
586 // conforming to the "gen_ai.server.time_per_output_token" semantic conventions.
587 // It represents the time per output token generated after the first token for
588 // successful responses.
589 type ServerTimePerOutputToken struct {
590 metric.Float64Histogram
591 }
592
593 var newServerTimePerOutputTokenOpts = []metric.Float64HistogramOption{
594 metric.WithDescription("Time per output token generated after the first token for successful responses."),
595 metric.WithUnit("s"),
596 }
597
598 // NewServerTimePerOutputToken returns a new ServerTimePerOutputToken instrument.
599 func NewServerTimePerOutputToken(
600 m metric.Meter,
601 opt ...metric.Float64HistogramOption,
602 ) (ServerTimePerOutputToken, error) {
603 // Check if the meter is nil.
604 if m == nil {
605 return ServerTimePerOutputToken{noop.Float64Histogram{}}, nil
606 }
607
608 if len(opt) == 0 {
609 opt = newServerTimePerOutputTokenOpts
610 } else {
611 opt = append(opt, newServerTimePerOutputTokenOpts...)
612 }
613
614 i, err := m.Float64Histogram(
615 "gen_ai.server.time_per_output_token",
616 opt...,
617 )
618 if err != nil {
619 return ServerTimePerOutputToken{noop.Float64Histogram{}}, err
620 }
621 return ServerTimePerOutputToken{i}, nil
622 }
623
624 // Inst returns the underlying metric instrument.
625 func (m ServerTimePerOutputToken) Inst() metric.Float64Histogram {
626 return m.Float64Histogram
627 }
628
629 // Name returns the semantic convention name of the instrument.
630 func (ServerTimePerOutputToken) Name() string {
631 return "gen_ai.server.time_per_output_token"
632 }
633
634 // Unit returns the semantic convention unit of the instrument
635 func (ServerTimePerOutputToken) Unit() string {
636 return "s"
637 }
638
639 // Description returns the semantic convention description of the instrument
640 func (ServerTimePerOutputToken) Description() string {
641 return "Time per output token generated after the first token for successful responses."
642 }
643
644 // Record records val to the current distribution for attrs.
645 //
646 // The operationName is the the name of the operation being performed.
647 //
648 // The providerName is the the Generative AI provider as identified by the client
649 // or server instrumentation.
650 //
651 // All additional attrs passed are included in the recorded value.
652 func (m ServerTimePerOutputToken) Record(
653 ctx context.Context,
654 val float64,
655 operationName OperationNameAttr,
656 providerName ProviderNameAttr,
657 attrs ...attribute.KeyValue,
658 ) {
659 if len(attrs) == 0 {
660 m.Float64Histogram.Record(ctx, val)
661 return
662 }
663
664 o := recOptPool.Get().(*[]metric.RecordOption)
665 defer func() {
666 *o = (*o)[:0]
667 recOptPool.Put(o)
668 }()
669
670 *o = append(
671 *o,
672 metric.WithAttributes(
673 append(
674 attrs,
675 attribute.String("gen_ai.operation.name", string(operationName)),
676 attribute.String("gen_ai.provider.name", string(providerName)),
677 )...,
678 ),
679 )
680
681 m.Float64Histogram.Record(ctx, val, *o...)
682 }
683
684 // RecordSet records val to the current distribution for set.
685 func (m ServerTimePerOutputToken) RecordSet(ctx context.Context, val float64, set attribute.Set) {
686 if set.Len() == 0 {
687 m.Float64Histogram.Record(ctx, val)
688 return
689 }
690
691 o := recOptPool.Get().(*[]metric.RecordOption)
692 defer func() {
693 *o = (*o)[:0]
694 recOptPool.Put(o)
695 }()
696
697 *o = append(*o, metric.WithAttributeSet(set))
698 m.Float64Histogram.Record(ctx, val, *o...)
699 }
700
701 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
702 // semantic convention. It represents the name of the GenAI model a request is
703 // being made to.
704 func (ServerTimePerOutputToken) AttrRequestModel(val string) attribute.KeyValue {
705 return attribute.String("gen_ai.request.model", val)
706 }
707
708 // AttrServerPort returns an optional attribute for the "server.port" semantic
709 // convention. It represents the genAI server port.
710 func (ServerTimePerOutputToken) AttrServerPort(val int) attribute.KeyValue {
711 return attribute.Int("server.port", val)
712 }
713
714 // AttrResponseModel returns an optional attribute for the
715 // "gen_ai.response.model" semantic convention. It represents the name of the
716 // model that generated the response.
717 func (ServerTimePerOutputToken) AttrResponseModel(val string) attribute.KeyValue {
718 return attribute.String("gen_ai.response.model", val)
719 }
720
721 // AttrServerAddress returns an optional attribute for the "server.address"
722 // semantic convention. It represents the genAI server address.
723 func (ServerTimePerOutputToken) AttrServerAddress(val string) attribute.KeyValue {
724 return attribute.String("server.address", val)
725 }
726
727 // ServerTimeToFirstToken is an instrument used to record metric values
728 // conforming to the "gen_ai.server.time_to_first_token" semantic conventions. It
729 // represents the time to generate first token for successful responses.
730 type ServerTimeToFirstToken struct {
731 metric.Float64Histogram
732 }
733
734 var newServerTimeToFirstTokenOpts = []metric.Float64HistogramOption{
735 metric.WithDescription("Time to generate first token for successful responses."),
736 metric.WithUnit("s"),
737 }
738
739 // NewServerTimeToFirstToken returns a new ServerTimeToFirstToken instrument.
740 func NewServerTimeToFirstToken(
741 m metric.Meter,
742 opt ...metric.Float64HistogramOption,
743 ) (ServerTimeToFirstToken, error) {
744 // Check if the meter is nil.
745 if m == nil {
746 return ServerTimeToFirstToken{noop.Float64Histogram{}}, nil
747 }
748
749 if len(opt) == 0 {
750 opt = newServerTimeToFirstTokenOpts
751 } else {
752 opt = append(opt, newServerTimeToFirstTokenOpts...)
753 }
754
755 i, err := m.Float64Histogram(
756 "gen_ai.server.time_to_first_token",
757 opt...,
758 )
759 if err != nil {
760 return ServerTimeToFirstToken{noop.Float64Histogram{}}, err
761 }
762 return ServerTimeToFirstToken{i}, nil
763 }
764
765 // Inst returns the underlying metric instrument.
766 func (m ServerTimeToFirstToken) Inst() metric.Float64Histogram {
767 return m.Float64Histogram
768 }
769
770 // Name returns the semantic convention name of the instrument.
771 func (ServerTimeToFirstToken) Name() string {
772 return "gen_ai.server.time_to_first_token"
773 }
774
775 // Unit returns the semantic convention unit of the instrument
776 func (ServerTimeToFirstToken) Unit() string {
777 return "s"
778 }
779
780 // Description returns the semantic convention description of the instrument
781 func (ServerTimeToFirstToken) Description() string {
782 return "Time to generate first token for successful responses."
783 }
784
785 // Record records val to the current distribution for attrs.
786 //
787 // The operationName is the the name of the operation being performed.
788 //
789 // The providerName is the the Generative AI provider as identified by the client
790 // or server instrumentation.
791 //
792 // All additional attrs passed are included in the recorded value.
793 func (m ServerTimeToFirstToken) Record(
794 ctx context.Context,
795 val float64,
796 operationName OperationNameAttr,
797 providerName ProviderNameAttr,
798 attrs ...attribute.KeyValue,
799 ) {
800 if len(attrs) == 0 {
801 m.Float64Histogram.Record(ctx, val)
802 return
803 }
804
805 o := recOptPool.Get().(*[]metric.RecordOption)
806 defer func() {
807 *o = (*o)[:0]
808 recOptPool.Put(o)
809 }()
810
811 *o = append(
812 *o,
813 metric.WithAttributes(
814 append(
815 attrs,
816 attribute.String("gen_ai.operation.name", string(operationName)),
817 attribute.String("gen_ai.provider.name", string(providerName)),
818 )...,
819 ),
820 )
821
822 m.Float64Histogram.Record(ctx, val, *o...)
823 }
824
825 // RecordSet records val to the current distribution for set.
826 func (m ServerTimeToFirstToken) RecordSet(ctx context.Context, val float64, set attribute.Set) {
827 if set.Len() == 0 {
828 m.Float64Histogram.Record(ctx, val)
829 return
830 }
831
832 o := recOptPool.Get().(*[]metric.RecordOption)
833 defer func() {
834 *o = (*o)[:0]
835 recOptPool.Put(o)
836 }()
837
838 *o = append(*o, metric.WithAttributeSet(set))
839 m.Float64Histogram.Record(ctx, val, *o...)
840 }
841
842 // AttrRequestModel returns an optional attribute for the "gen_ai.request.model"
843 // semantic convention. It represents the name of the GenAI model a request is
844 // being made to.
845 func (ServerTimeToFirstToken) AttrRequestModel(val string) attribute.KeyValue {
846 return attribute.String("gen_ai.request.model", val)
847 }
848
849 // AttrServerPort returns an optional attribute for the "server.port" semantic
850 // convention. It represents the genAI server port.
851 func (ServerTimeToFirstToken) AttrServerPort(val int) attribute.KeyValue {
852 return attribute.Int("server.port", val)
853 }
854
855 // AttrResponseModel returns an optional attribute for the
856 // "gen_ai.response.model" semantic convention. It represents the name of the
857 // model that generated the response.
858 func (ServerTimeToFirstToken) AttrResponseModel(val string) attribute.KeyValue {
859 return attribute.String("gen_ai.response.model", val)
860 }
861
862 // AttrServerAddress returns an optional attribute for the "server.address"
863 // semantic convention. It represents the genAI server address.
864 func (ServerTimeToFirstToken) AttrServerAddress(val string) attribute.KeyValue {
865 return attribute.String("server.address", val)
866 }