Debian Codegraph @

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