Debian Codegraph @

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