Debian Codegraph @

semconv/v1.40.0/rpcconv/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 rpcconv provides types and functionality for OpenTelemetry semantic
7 // conventions in the "rpc" namespace.
8 package rpcconv
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 // SystemNameAttr is an attribute conforming to the rpc.system.name semantic
36 // conventions. It represents the Remote Procedure Call (RPC) system.
37 type SystemNameAttr string
38
39 var (
40 // SystemNameGRPC is the [gRPC].
41 //
42 // [gRPC]: https://grpc.io/
43 SystemNameGRPC SystemNameAttr = "grpc"
44 // SystemNameDubbo is the [Apache Dubbo].
45 //
46 // [Apache Dubbo]: https://dubbo.apache.org/
47 SystemNameDubbo SystemNameAttr = "dubbo"
48 // SystemNameConnectrpc is the [Connect RPC].
49 //
50 // [Connect RPC]: https://connectrpc.com/
51 SystemNameConnectrpc SystemNameAttr = "connectrpc"
52 // SystemNameJSONRPC is the [JSON-RPC].
53 //
54 // [JSON-RPC]: https://www.jsonrpc.org/
55 SystemNameJSONRPC SystemNameAttr = "jsonrpc"
56 )
57
58 // ClientCallDuration is an instrument used to record metric values conforming to
59 // the "rpc.client.call.duration" semantic conventions. It represents the
60 // measures the duration of an outgoing Remote Procedure Call (RPC).
61 type ClientCallDuration struct {
62 metric.Float64Histogram
63 }
64
65 var newClientCallDurationOpts = []metric.Float64HistogramOption{
66 metric.WithDescription("Measures the duration of an outgoing Remote Procedure Call (RPC)."),
67 metric.WithUnit("s"),
68 }
69
70 // NewClientCallDuration returns a new ClientCallDuration instrument.
71 func NewClientCallDuration(
72 m metric.Meter,
73 opt ...metric.Float64HistogramOption,
74 ) (ClientCallDuration, error) {
75 // Check if the meter is nil.
76 if m == nil {
77 return ClientCallDuration{noop.Float64Histogram{}}, nil
78 }
79
80 if len(opt) == 0 {
81 opt = newClientCallDurationOpts
82 } else {
83 opt = append(opt, newClientCallDurationOpts...)
84 }
85
86 i, err := m.Float64Histogram(
87 "rpc.client.call.duration",
88 opt...,
89 )
90 if err != nil {
91 return ClientCallDuration{noop.Float64Histogram{}}, err
92 }
93 return ClientCallDuration{i}, nil
94 }
95
96 // Inst returns the underlying metric instrument.
97 func (m ClientCallDuration) Inst() metric.Float64Histogram {
98 return m.Float64Histogram
99 }
100
101 // Name returns the semantic convention name of the instrument.
102 func (ClientCallDuration) Name() string {
103 return "rpc.client.call.duration"
104 }
105
106 // Unit returns the semantic convention unit of the instrument
107 func (ClientCallDuration) Unit() string {
108 return "s"
109 }
110
111 // Description returns the semantic convention description of the instrument
112 func (ClientCallDuration) Description() string {
113 return "Measures the duration of an outgoing Remote Procedure Call (RPC)."
114 }
115
116 // Record records val to the current distribution for attrs.
117 //
118 // The systemName is the the Remote Procedure Call (RPC) system.
119 //
120 // All additional attrs passed are included in the recorded value.
121 //
122 // When this metric is reported alongside an RPC client span, the metric value
123 // SHOULD be the same as the RPC client span duration.
124 func (m ClientCallDuration) Record(
125 ctx context.Context,
126 val float64,
127 systemName SystemNameAttr,
128 attrs ...attribute.KeyValue,
129 ) {
130 if len(attrs) == 0 {
131 m.Float64Histogram.Record(ctx, val, metric.WithAttributes(
132 attribute.String("rpc.system.name", string(systemName)),
133 ))
134 return
135 }
136
137 o := recOptPool.Get().(*[]metric.RecordOption)
138 defer func() {
139 *o = (*o)[:0]
140 recOptPool.Put(o)
141 }()
142
143 *o = append(
144 *o,
145 metric.WithAttributes(
146 append(
147 attrs[:len(attrs):len(attrs)],
148 attribute.String("rpc.system.name", string(systemName)),
149 )...,
150 ),
151 )
152
153 m.Float64Histogram.Record(ctx, val, *o...)
154 }
155
156 // RecordSet records val to the current distribution for set.
157 //
158 // When this metric is reported alongside an RPC client span, the metric value
159 // SHOULD be the same as the RPC client span duration.
160 func (m ClientCallDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
161 if set.Len() == 0 {
162 m.Float64Histogram.Record(ctx, val)
163 return
164 }
165
166 o := recOptPool.Get().(*[]metric.RecordOption)
167 defer func() {
168 *o = (*o)[:0]
169 recOptPool.Put(o)
170 }()
171
172 *o = append(*o, metric.WithAttributeSet(set))
173 m.Float64Histogram.Record(ctx, val, *o...)
174 }
175
176 // AttrErrorType returns an optional attribute for the "error.type" semantic
177 // convention. It represents the describes a class of error the operation ended
178 // with.
179 func (ClientCallDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
180 return attribute.String("error.type", string(val))
181 }
182
183 // AttrMethod returns an optional attribute for the "rpc.method" semantic
184 // convention. It represents the fully-qualified logical name of the method from
185 // the RPC interface perspective.
186 func (ClientCallDuration) AttrMethod(val string) attribute.KeyValue {
187 return attribute.String("rpc.method", val)
188 }
189
190 // AttrResponseStatusCode returns an optional attribute for the
191 // "rpc.response.status_code" semantic convention. It represents the status code
192 // of the RPC returned by the RPC server or generated by the client.
193 func (ClientCallDuration) AttrResponseStatusCode(val string) attribute.KeyValue {
194 return attribute.String("rpc.response.status_code", val)
195 }
196
197 // AttrServerAddress returns an optional attribute for the "server.address"
198 // semantic convention. It represents a string identifying a group of RPC server
199 // instances request is sent to.
200 func (ClientCallDuration) AttrServerAddress(val string) attribute.KeyValue {
201 return attribute.String("server.address", val)
202 }
203
204 // AttrServerPort returns an optional attribute for the "server.port" semantic
205 // convention. It represents the server port number.
206 func (ClientCallDuration) AttrServerPort(val int) attribute.KeyValue {
207 return attribute.Int("server.port", val)
208 }
209
210 // ServerCallDuration is an instrument used to record metric values conforming to
211 // the "rpc.server.call.duration" semantic conventions. It represents the
212 // measures the duration of an incoming Remote Procedure Call (RPC).
213 type ServerCallDuration struct {
214 metric.Float64Histogram
215 }
216
217 var newServerCallDurationOpts = []metric.Float64HistogramOption{
218 metric.WithDescription("Measures the duration of an incoming Remote Procedure Call (RPC)."),
219 metric.WithUnit("s"),
220 }
221
222 // NewServerCallDuration returns a new ServerCallDuration instrument.
223 func NewServerCallDuration(
224 m metric.Meter,
225 opt ...metric.Float64HistogramOption,
226 ) (ServerCallDuration, error) {
227 // Check if the meter is nil.
228 if m == nil {
229 return ServerCallDuration{noop.Float64Histogram{}}, nil
230 }
231
232 if len(opt) == 0 {
233 opt = newServerCallDurationOpts
234 } else {
235 opt = append(opt, newServerCallDurationOpts...)
236 }
237
238 i, err := m.Float64Histogram(
239 "rpc.server.call.duration",
240 opt...,
241 )
242 if err != nil {
243 return ServerCallDuration{noop.Float64Histogram{}}, err
244 }
245 return ServerCallDuration{i}, nil
246 }
247
248 // Inst returns the underlying metric instrument.
249 func (m ServerCallDuration) Inst() metric.Float64Histogram {
250 return m.Float64Histogram
251 }
252
253 // Name returns the semantic convention name of the instrument.
254 func (ServerCallDuration) Name() string {
255 return "rpc.server.call.duration"
256 }
257
258 // Unit returns the semantic convention unit of the instrument
259 func (ServerCallDuration) Unit() string {
260 return "s"
261 }
262
263 // Description returns the semantic convention description of the instrument
264 func (ServerCallDuration) Description() string {
265 return "Measures the duration of an incoming Remote Procedure Call (RPC)."
266 }
267
268 // Record records val to the current distribution for attrs.
269 //
270 // The systemName is the the Remote Procedure Call (RPC) system.
271 //
272 // All additional attrs passed are included in the recorded value.
273 //
274 // When this metric is reported alongside an RPC server span, the metric value
275 // SHOULD be the same as the RPC server span duration.
276 func (m ServerCallDuration) Record(
277 ctx context.Context,
278 val float64,
279 systemName SystemNameAttr,
280 attrs ...attribute.KeyValue,
281 ) {
282 if len(attrs) == 0 {
283 m.Float64Histogram.Record(ctx, val, metric.WithAttributes(
284 attribute.String("rpc.system.name", string(systemName)),
285 ))
286 return
287 }
288
289 o := recOptPool.Get().(*[]metric.RecordOption)
290 defer func() {
291 *o = (*o)[:0]
292 recOptPool.Put(o)
293 }()
294
295 *o = append(
296 *o,
297 metric.WithAttributes(
298 append(
299 attrs[:len(attrs):len(attrs)],
300 attribute.String("rpc.system.name", string(systemName)),
301 )...,
302 ),
303 )
304
305 m.Float64Histogram.Record(ctx, val, *o...)
306 }
307
308 // RecordSet records val to the current distribution for set.
309 //
310 // When this metric is reported alongside an RPC server span, the metric value
311 // SHOULD be the same as the RPC server span duration.
312 func (m ServerCallDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
313 if set.Len() == 0 {
314 m.Float64Histogram.Record(ctx, val)
315 return
316 }
317
318 o := recOptPool.Get().(*[]metric.RecordOption)
319 defer func() {
320 *o = (*o)[:0]
321 recOptPool.Put(o)
322 }()
323
324 *o = append(*o, metric.WithAttributeSet(set))
325 m.Float64Histogram.Record(ctx, val, *o...)
326 }
327
328 // AttrErrorType returns an optional attribute for the "error.type" semantic
329 // convention. It represents the describes a class of error the operation ended
330 // with.
331 func (ServerCallDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
332 return attribute.String("error.type", string(val))
333 }
334
335 // AttrMethod returns an optional attribute for the "rpc.method" semantic
336 // convention. It represents the fully-qualified logical name of the method from
337 // the RPC interface perspective.
338 func (ServerCallDuration) AttrMethod(val string) attribute.KeyValue {
339 return attribute.String("rpc.method", val)
340 }
341
342 // AttrResponseStatusCode returns an optional attribute for the
343 // "rpc.response.status_code" semantic convention. It represents the status code
344 // of the RPC returned by the RPC server or generated by the client.
345 func (ServerCallDuration) AttrResponseStatusCode(val string) attribute.KeyValue {
346 return attribute.String("rpc.response.status_code", val)
347 }
348
349 // AttrServerAddress returns an optional attribute for the "server.address"
350 // semantic convention. It represents a string identifying a group of RPC server
351 // instances request is sent to.
352 func (ServerCallDuration) AttrServerAddress(val string) attribute.KeyValue {
353 return attribute.String("server.address", val)
354 }
355
356 // AttrServerPort returns an optional attribute for the "server.port" semantic
357 // convention. It represents the server port number.
358 func (ServerCallDuration) AttrServerPort(val int) attribute.KeyValue {
359 return attribute.Int("server.port", val)
360 }