Debian Codegraph @

semconv/v1.37.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 var newLookupDurationOpts = []metric.Float64HistogramOption{
42 metric.WithDescription("Measures the time taken to perform a DNS lookup."),
43 metric.WithUnit("s"),
44 }
45
46 // NewLookupDuration returns a new LookupDuration instrument.
47 func NewLookupDuration(
48 m metric.Meter,
49 opt ...metric.Float64HistogramOption,
50 ) (LookupDuration, error) {
51 // Check if the meter is nil.
52 if m == nil {
53 return LookupDuration{noop.Float64Histogram{}}, nil
54 }
55
56 if len(opt) == 0 {
57 opt = newLookupDurationOpts
58 } else {
59 opt = append(opt, newLookupDurationOpts...)
60 }
61
62 i, err := m.Float64Histogram(
63 "dns.lookup.duration",
64 opt...,
65 )
66 if err != nil {
67 return LookupDuration{noop.Float64Histogram{}}, err
68 }
69 return LookupDuration{i}, nil
70 }
71
72 // Inst returns the underlying metric instrument.
73 func (m LookupDuration) Inst() metric.Float64Histogram {
74 return m.Float64Histogram
75 }
76
77 // Name returns the semantic convention name of the instrument.
78 func (LookupDuration) Name() string {
79 return "dns.lookup.duration"
80 }
81
82 // Unit returns the semantic convention unit of the instrument
83 func (LookupDuration) Unit() string {
84 return "s"
85 }
86
87 // Description returns the semantic convention description of the instrument
88 func (LookupDuration) Description() string {
89 return "Measures the time taken to perform a DNS lookup."
90 }
91
92 // Record records val to the current distribution for attrs.
93 //
94 // The questionName is the the name being queried.
95 //
96 // All additional attrs passed are included in the recorded value.
97 func (m LookupDuration) Record(
98 ctx context.Context,
99 val float64,
100 questionName string,
101 attrs ...attribute.KeyValue,
102 ) {
103 if len(attrs) == 0 {
104 m.Float64Histogram.Record(ctx, val)
105 return
106 }
107
108 o := recOptPool.Get().(*[]metric.RecordOption)
109 defer func() {
110 *o = (*o)[:0]
111 recOptPool.Put(o)
112 }()
113
114 *o = append(
115 *o,
116 metric.WithAttributes(
117 append(
118 attrs,
119 attribute.String("dns.question.name", questionName),
120 )...,
121 ),
122 )
123
124 m.Float64Histogram.Record(ctx, val, *o...)
125 }
126
127 // RecordSet records val to the current distribution for set.
128 func (m LookupDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
129 if set.Len() == 0 {
130 m.Float64Histogram.Record(ctx, val)
131 return
132 }
133
134 o := recOptPool.Get().(*[]metric.RecordOption)
135 defer func() {
136 *o = (*o)[:0]
137 recOptPool.Put(o)
138 }()
139
140 *o = append(*o, metric.WithAttributeSet(set))
141 m.Float64Histogram.Record(ctx, val, *o...)
142 }
143
144 // AttrErrorType returns an optional attribute for the "error.type" semantic
145 // convention. It represents the describes the error the DNS lookup failed with.
146 func (LookupDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
147 return attribute.String("error.type", string(val))
148 }