Debian Codegraph @

semconv/v1.34.0/dnsconv/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 dnsconv provides types and functionality for OpenTelemetry semantic
7 // conventions in the "dns" namespace.
8 package dnsconv
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 the error the DNS lookup failed with.
26 type ErrorTypeAttr string
27
28 var (
29 // ErrorTypeOther is a fallback error value to be used when the instrumentation
30 // doesn't define a custom value.
31 ErrorTypeOther ErrorTypeAttr = "_OTHER"
32 )
33
34 // LookupDuration is an instrument used to record metric values conforming to the
35 // "dns.lookup.duration" semantic conventions. It represents the measures the
36 // time taken to perform a DNS lookup.
37 type LookupDuration struct {
38 metric.Float64Histogram
39 }
40
41 // NewLookupDuration returns a new LookupDuration instrument.
42 func NewLookupDuration(
43 m metric.Meter,
44 opt ...metric.Float64HistogramOption,
45 ) (LookupDuration, error) {
46 // Check if the meter is nil.
47 if m == nil {
48 return LookupDuration{noop.Float64Histogram{}}, nil
49 }
50
51 i, err := m.Float64Histogram(
52 "dns.lookup.duration",
53 append([]metric.Float64HistogramOption{
54 metric.WithDescription("Measures the time taken to perform a DNS lookup."),
55 metric.WithUnit("s"),
56 }, opt...)...,
57 )
58 if err != nil {
59 return LookupDuration{noop.Float64Histogram{}}, err
60 }
61 return LookupDuration{i}, nil
62 }
63
64 // Inst returns the underlying metric instrument.
65 func (m LookupDuration) Inst() metric.Float64Histogram {
66 return m.Float64Histogram
67 }
68
69 // Name returns the semantic convention name of the instrument.
70 func (LookupDuration) Name() string {
71 return "dns.lookup.duration"
72 }
73
74 // Unit returns the semantic convention unit of the instrument
75 func (LookupDuration) Unit() string {
76 return "s"
77 }
78
79 // Description returns the semantic convention description of the instrument
80 func (LookupDuration) Description() string {
81 return "Measures the time taken to perform a DNS lookup."
82 }
83
84 // Record records val to the current distribution.
85 //
86 // The questionName is the the name being queried.
87 //
88 // All additional attrs passed are included in the recorded value.
89 func (m LookupDuration) Record(
90 ctx context.Context,
91 val float64,
92 questionName string,
93 attrs ...attribute.KeyValue,
94 ) {
95 o := recOptPool.Get().(*[]metric.RecordOption)
96 defer func() {
97 *o = (*o)[:0]
98 recOptPool.Put(o)
99 }()
100
101 *o = append(
102 *o,
103 metric.WithAttributes(
104 append(
105 attrs,
106 attribute.String("dns.question.name", questionName),
107 )...,
108 ),
109 )
110
111 m.Float64Histogram.Record(ctx, val, *o...)
112 }
113
114 // AttrErrorType returns an optional attribute for the "error.type" semantic
115 // convention. It represents the describes the error the DNS lookup failed with.
116 func (LookupDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
117 return attribute.String("error.type", string(val))
118 }