Debian Codegraph @

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