Debian Codegraph @

semconv/v1.36.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 for attrs.
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 if len(attrs) == 0 {
96 m.Float64Histogram.Record(ctx, val)
97 return
98 }
99
100 o := recOptPool.Get().(*[]metric.RecordOption)
101 defer func() {
102 *o = (*o)[:0]
103 recOptPool.Put(o)
104 }()
105
106 *o = append(
107 *o,
108 metric.WithAttributes(
109 append(
110 attrs,
111 attribute.String("dns.question.name", questionName),
112 )...,
113 ),
114 )
115
116 m.Float64Histogram.Record(ctx, val, *o...)
117 }
118
119 // RecordSet records val to the current distribution for set.
120 func (m LookupDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
121 if set.Len() == 0 {
122 m.Float64Histogram.Record(ctx, val)
123 return
124 }
125
126 o := recOptPool.Get().(*[]metric.RecordOption)
127 defer func() {
128 *o = (*o)[:0]
129 recOptPool.Put(o)
130 }()
131
132 *o = append(*o, metric.WithAttributeSet(set))
133 m.Float64Histogram.Record(ctx, val, *o...)
134 }
135
136 // AttrErrorType returns an optional attribute for the "error.type" semantic
137 // convention. It represents the describes the error the DNS lookup failed with.
138 func (LookupDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
139 return attribute.String("error.type", string(val))
140 }