Debian Codegraph @

semconv/v1.40.0/mcpconv/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 mcpconv provides types and functionality for OpenTelemetry semantic
7 // conventions in the "mcp" namespace.
8 package mcpconv
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 // GenAIOperationNameAttr is an attribute conforming to the gen_ai.operation.name
36 // semantic conventions. It represents the name of the GenAI operation being
37 // performed.
38 type GenAIOperationNameAttr string
39
40 var (
41 // GenAIOperationNameChat is the chat completion operation such as
42 // [OpenAI Chat API].
43 //
44 // [OpenAI Chat API]: https://platform.openai.com/docs/api-reference/chat
45 GenAIOperationNameChat GenAIOperationNameAttr = "chat"
46 // GenAIOperationNameGenerateContent is the multimodal content generation
47 // operation such as [Gemini Generate Content].
48 //
49 // [Gemini Generate Content]: https://ai.google.dev/api/generate-content
50 GenAIOperationNameGenerateContent GenAIOperationNameAttr = "generate_content"
51 // GenAIOperationNameTextCompletion is the text completions operation such as
52 // [OpenAI Completions API (Legacy)].
53 //
54 // [OpenAI Completions API (Legacy)]: https://platform.openai.com/docs/api-reference/completions
55 GenAIOperationNameTextCompletion GenAIOperationNameAttr = "text_completion"
56 // GenAIOperationNameEmbeddings is the embeddings operation such as
57 // [OpenAI Create embeddings API].
58 //
59 // [OpenAI Create embeddings API]: https://platform.openai.com/docs/api-reference/embeddings/create
60 GenAIOperationNameEmbeddings GenAIOperationNameAttr = "embeddings"
61 // GenAIOperationNameRetrieval is the retrieval operation such as
62 // [OpenAI Search Vector Store API].
63 //
64 // [OpenAI Search Vector Store API]: https://platform.openai.com/docs/api-reference/vector-stores/search
65 GenAIOperationNameRetrieval GenAIOperationNameAttr = "retrieval"
66 // GenAIOperationNameCreateAgent is the create GenAI agent.
67 GenAIOperationNameCreateAgent GenAIOperationNameAttr = "create_agent"
68 // GenAIOperationNameInvokeAgent is the invoke GenAI agent.
69 GenAIOperationNameInvokeAgent GenAIOperationNameAttr = "invoke_agent"
70 // GenAIOperationNameExecuteTool is the execute a tool.
71 GenAIOperationNameExecuteTool GenAIOperationNameAttr = "execute_tool"
72 )
73
74 // MethodNameAttr is an attribute conforming to the mcp.method.name semantic
75 // conventions. It represents the name of the request or notification method.
76 type MethodNameAttr string
77
78 var (
79 // MethodNameNotificationsCancelled is the notification cancelling a
80 // previously-issued request.
81 MethodNameNotificationsCancelled MethodNameAttr = "notifications/cancelled"
82 // MethodNameInitialize is the request to initialize the MCP client.
83 MethodNameInitialize MethodNameAttr = "initialize"
84 // MethodNameNotificationsInitialized is the notification indicating that the
85 // MCP client has been initialized.
86 MethodNameNotificationsInitialized MethodNameAttr = "notifications/initialized"
87 // MethodNameNotificationsProgress is the notification indicating the progress
88 // for a long-running operation.
89 MethodNameNotificationsProgress MethodNameAttr = "notifications/progress"
90 // MethodNamePing is the request to check that the other party is still alive.
91 MethodNamePing MethodNameAttr = "ping"
92 // MethodNameResourcesList is the request to list resources available on server.
93 MethodNameResourcesList MethodNameAttr = "resources/list"
94 // MethodNameResourcesTemplatesList is the request to list resource templates
95 // available on server.
96 MethodNameResourcesTemplatesList MethodNameAttr = "resources/templates/list"
97 // MethodNameResourcesRead is the request to read a resource.
98 MethodNameResourcesRead MethodNameAttr = "resources/read"
99 // MethodNameNotificationsResourcesListChanged is the notification indicating
100 // that the list of resources has changed.
101 MethodNameNotificationsResourcesListChanged MethodNameAttr = "notifications/resources/list_changed"
102 // MethodNameResourcesSubscribe is the request to subscribe to a resource.
103 MethodNameResourcesSubscribe MethodNameAttr = "resources/subscribe"
104 // MethodNameResourcesUnsubscribe is the request to unsubscribe from resource
105 // updates.
106 MethodNameResourcesUnsubscribe MethodNameAttr = "resources/unsubscribe"
107 // MethodNameNotificationsResourcesUpdated is the notification indicating that a
108 // resource has been updated.
109 MethodNameNotificationsResourcesUpdated MethodNameAttr = "notifications/resources/updated"
110 // MethodNamePromptsList is the request to list prompts available on server.
111 MethodNamePromptsList MethodNameAttr = "prompts/list"
112 // MethodNamePromptsGet is the request to get a prompt.
113 MethodNamePromptsGet MethodNameAttr = "prompts/get"
114 // MethodNameNotificationsPromptsListChanged is the notification indicating that
115 // the list of prompts has changed.
116 MethodNameNotificationsPromptsListChanged MethodNameAttr = "notifications/prompts/list_changed"
117 // MethodNameToolsList is the request to list tools available on server.
118 MethodNameToolsList MethodNameAttr = "tools/list"
119 // MethodNameToolsCall is the request to call a tool.
120 MethodNameToolsCall MethodNameAttr = "tools/call"
121 // MethodNameNotificationsToolsListChanged is the notification indicating that
122 // the list of tools has changed.
123 MethodNameNotificationsToolsListChanged MethodNameAttr = "notifications/tools/list_changed"
124 // MethodNameLoggingSetLevel is the request to set the logging level.
125 MethodNameLoggingSetLevel MethodNameAttr = "logging/setLevel"
126 // MethodNameNotificationsMessage is the notification indicating that a message
127 // has been received.
128 MethodNameNotificationsMessage MethodNameAttr = "notifications/message"
129 // MethodNameSamplingCreateMessage is the request to create a sampling message.
130 MethodNameSamplingCreateMessage MethodNameAttr = "sampling/createMessage"
131 // MethodNameCompletionComplete is the request to complete a prompt.
132 MethodNameCompletionComplete MethodNameAttr = "completion/complete"
133 // MethodNameRootsList is the request to list roots available on server.
134 MethodNameRootsList MethodNameAttr = "roots/list"
135 // MethodNameNotificationsRootsListChanged is the notification indicating that
136 // the list of roots has changed.
137 MethodNameNotificationsRootsListChanged MethodNameAttr = "notifications/roots/list_changed"
138 // MethodNameElicitationCreate is the request from the server to elicit
139 // additional information from the user via the client.
140 MethodNameElicitationCreate MethodNameAttr = "elicitation/create"
141 )
142
143 // NetworkTransportAttr is an attribute conforming to the network.transport
144 // semantic conventions. It represents the transport protocol used for the MCP
145 // session.
146 type NetworkTransportAttr string
147
148 var (
149 // NetworkTransportTCP is the TCP.
150 NetworkTransportTCP NetworkTransportAttr = "tcp"
151 // NetworkTransportUDP is the UDP.
152 NetworkTransportUDP NetworkTransportAttr = "udp"
153 // NetworkTransportPipe is the named or anonymous pipe.
154 NetworkTransportPipe NetworkTransportAttr = "pipe"
155 // NetworkTransportUnix is the unix domain socket.
156 NetworkTransportUnix NetworkTransportAttr = "unix"
157 // NetworkTransportQUIC is the QUIC.
158 NetworkTransportQUIC NetworkTransportAttr = "quic"
159 )
160
161 // ClientOperationDuration is an instrument used to record metric values
162 // conforming to the "mcp.client.operation.duration" semantic conventions. It
163 // represents the duration of the MCP request or notification as observed on the
164 // sender from the time it was sent until the response or ack is received.
165 type ClientOperationDuration struct {
166 metric.Float64Histogram
167 }
168
169 var newClientOperationDurationOpts = []metric.Float64HistogramOption{
170 metric.WithDescription("The duration of the MCP request or notification as observed on the sender from the time it was sent until the response or ack is received."),
171 metric.WithUnit("s"),
172 }
173
174 // NewClientOperationDuration returns a new ClientOperationDuration instrument.
175 func NewClientOperationDuration(
176 m metric.Meter,
177 opt ...metric.Float64HistogramOption,
178 ) (ClientOperationDuration, error) {
179 // Check if the meter is nil.
180 if m == nil {
181 return ClientOperationDuration{noop.Float64Histogram{}}, nil
182 }
183
184 if len(opt) == 0 {
185 opt = newClientOperationDurationOpts
186 } else {
187 opt = append(opt, newClientOperationDurationOpts...)
188 }
189
190 i, err := m.Float64Histogram(
191 "mcp.client.operation.duration",
192 opt...,
193 )
194 if err != nil {
195 return ClientOperationDuration{noop.Float64Histogram{}}, err
196 }
197 return ClientOperationDuration{i}, nil
198 }
199
200 // Inst returns the underlying metric instrument.
201 func (m ClientOperationDuration) Inst() metric.Float64Histogram {
202 return m.Float64Histogram
203 }
204
205 // Name returns the semantic convention name of the instrument.
206 func (ClientOperationDuration) Name() string {
207 return "mcp.client.operation.duration"
208 }
209
210 // Unit returns the semantic convention unit of the instrument
211 func (ClientOperationDuration) Unit() string {
212 return "s"
213 }
214
215 // Description returns the semantic convention description of the instrument
216 func (ClientOperationDuration) Description() string {
217 return "The duration of the MCP request or notification as observed on the sender from the time it was sent until the response or ack is received."
218 }
219
220 // Record records val to the current distribution for attrs.
221 //
222 // The methodName is the the name of the request or notification method.
223 //
224 // All additional attrs passed are included in the recorded value.
225 func (m ClientOperationDuration) Record(
226 ctx context.Context,
227 val float64,
228 methodName MethodNameAttr,
229 attrs ...attribute.KeyValue,
230 ) {
231 if len(attrs) == 0 {
232 m.Float64Histogram.Record(ctx, val, metric.WithAttributes(
233 attribute.String("mcp.method.name", string(methodName)),
234 ))
235 return
236 }
237
238 o := recOptPool.Get().(*[]metric.RecordOption)
239 defer func() {
240 *o = (*o)[:0]
241 recOptPool.Put(o)
242 }()
243
244 *o = append(
245 *o,
246 metric.WithAttributes(
247 append(
248 attrs[:len(attrs):len(attrs)],
249 attribute.String("mcp.method.name", string(methodName)),
250 )...,
251 ),
252 )
253
254 m.Float64Histogram.Record(ctx, val, *o...)
255 }
256
257 // RecordSet records val to the current distribution for set.
258 func (m ClientOperationDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
259 if set.Len() == 0 {
260 m.Float64Histogram.Record(ctx, val)
261 return
262 }
263
264 o := recOptPool.Get().(*[]metric.RecordOption)
265 defer func() {
266 *o = (*o)[:0]
267 recOptPool.Put(o)
268 }()
269
270 *o = append(*o, metric.WithAttributeSet(set))
271 m.Float64Histogram.Record(ctx, val, *o...)
272 }
273
274 // AttrErrorType returns an optional attribute for the "error.type" semantic
275 // convention. It represents the describes a class of error the operation ended
276 // with.
277 func (ClientOperationDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
278 return attribute.String("error.type", string(val))
279 }
280
281 // AttrGenAIPromptName returns an optional attribute for the "gen_ai.prompt.name"
282 // semantic convention. It represents the name of the prompt or prompt template
283 // provided in the request or response.
284 func (ClientOperationDuration) AttrGenAIPromptName(val string) attribute.KeyValue {
285 return attribute.String("gen_ai.prompt.name", val)
286 }
287
288 // AttrGenAIToolName returns an optional attribute for the "gen_ai.tool.name"
289 // semantic convention. It represents the name of the tool utilized by the agent.
290 func (ClientOperationDuration) AttrGenAIToolName(val string) attribute.KeyValue {
291 return attribute.String("gen_ai.tool.name", val)
292 }
293
294 // AttrRPCResponseStatusCode returns an optional attribute for the
295 // "rpc.response.status_code" semantic convention. It represents the error code
296 // from the JSON-RPC response.
297 func (ClientOperationDuration) AttrRPCResponseStatusCode(val string) attribute.KeyValue {
298 return attribute.String("rpc.response.status_code", val)
299 }
300
301 // AttrGenAIOperationName returns an optional attribute for the
302 // "gen_ai.operation.name" semantic convention. It represents the name of the
303 // GenAI operation being performed.
304 func (ClientOperationDuration) AttrGenAIOperationName(val GenAIOperationNameAttr) attribute.KeyValue {
305 return attribute.String("gen_ai.operation.name", string(val))
306 }
307
308 // AttrJSONRPCProtocolVersion returns an optional attribute for the
309 // "jsonrpc.protocol.version" semantic convention. It represents the protocol
310 // version, as specified in the `jsonrpc` property of the request and its
311 // corresponding response.
312 func (ClientOperationDuration) AttrJSONRPCProtocolVersion(val string) attribute.KeyValue {
313 return attribute.String("jsonrpc.protocol.version", val)
314 }
315
316 // AttrProtocolVersion returns an optional attribute for the
317 // "mcp.protocol.version" semantic convention. It represents the [version] of the
318 // Model Context Protocol used.
319 //
320 // [version]: https://modelcontextprotocol.io/specification/versioning
321 func (ClientOperationDuration) AttrProtocolVersion(val string) attribute.KeyValue {
322 return attribute.String("mcp.protocol.version", val)
323 }
324
325 // AttrNetworkProtocolName returns an optional attribute for the
326 // "network.protocol.name" semantic convention. It represents the
327 // [OSI application layer] or non-OSI equivalent.
328 //
329 // [OSI application layer]: https://wikipedia.org/wiki/Application_layer
330 func (ClientOperationDuration) AttrNetworkProtocolName(val string) attribute.KeyValue {
331 return attribute.String("network.protocol.name", val)
332 }
333
334 // AttrNetworkProtocolVersion returns an optional attribute for the
335 // "network.protocol.version" semantic convention. It represents the actual
336 // version of the protocol used for network communication.
337 func (ClientOperationDuration) AttrNetworkProtocolVersion(val string) attribute.KeyValue {
338 return attribute.String("network.protocol.version", val)
339 }
340
341 // AttrNetworkTransport returns an optional attribute for the "network.transport"
342 // semantic convention. It represents the transport protocol used for the MCP
343 // session.
344 func (ClientOperationDuration) AttrNetworkTransport(val NetworkTransportAttr) attribute.KeyValue {
345 return attribute.String("network.transport", string(val))
346 }
347
348 // AttrServerAddress returns an optional attribute for the "server.address"
349 // semantic convention. It represents the server domain name if available without
350 // reverse DNS lookup; otherwise, IP address or Unix domain socket name.
351 func (ClientOperationDuration) AttrServerAddress(val string) attribute.KeyValue {
352 return attribute.String("server.address", val)
353 }
354
355 // AttrServerPort returns an optional attribute for the "server.port" semantic
356 // convention. It represents the server port number.
357 func (ClientOperationDuration) AttrServerPort(val int) attribute.KeyValue {
358 return attribute.Int("server.port", val)
359 }
360
361 // AttrResourceURI returns an optional attribute for the "mcp.resource.uri"
362 // semantic convention. It represents the value of the resource uri.
363 func (ClientOperationDuration) AttrResourceURI(val string) attribute.KeyValue {
364 return attribute.String("mcp.resource.uri", val)
365 }
366
367 // ClientSessionDuration is an instrument used to record metric values conforming
368 // to the "mcp.client.session.duration" semantic conventions. It represents the
369 // duration of the MCP session as observed on the MCP client.
370 type ClientSessionDuration struct {
371 metric.Float64Histogram
372 }
373
374 var newClientSessionDurationOpts = []metric.Float64HistogramOption{
375 metric.WithDescription("The duration of the MCP session as observed on the MCP client."),
376 metric.WithUnit("s"),
377 }
378
379 // NewClientSessionDuration returns a new ClientSessionDuration instrument.
380 func NewClientSessionDuration(
381 m metric.Meter,
382 opt ...metric.Float64HistogramOption,
383 ) (ClientSessionDuration, error) {
384 // Check if the meter is nil.
385 if m == nil {
386 return ClientSessionDuration{noop.Float64Histogram{}}, nil
387 }
388
389 if len(opt) == 0 {
390 opt = newClientSessionDurationOpts
391 } else {
392 opt = append(opt, newClientSessionDurationOpts...)
393 }
394
395 i, err := m.Float64Histogram(
396 "mcp.client.session.duration",
397 opt...,
398 )
399 if err != nil {
400 return ClientSessionDuration{noop.Float64Histogram{}}, err
401 }
402 return ClientSessionDuration{i}, nil
403 }
404
405 // Inst returns the underlying metric instrument.
406 func (m ClientSessionDuration) Inst() metric.Float64Histogram {
407 return m.Float64Histogram
408 }
409
410 // Name returns the semantic convention name of the instrument.
411 func (ClientSessionDuration) Name() string {
412 return "mcp.client.session.duration"
413 }
414
415 // Unit returns the semantic convention unit of the instrument
416 func (ClientSessionDuration) Unit() string {
417 return "s"
418 }
419
420 // Description returns the semantic convention description of the instrument
421 func (ClientSessionDuration) Description() string {
422 return "The duration of the MCP session as observed on the MCP client."
423 }
424
425 // Record records val to the current distribution for attrs.
426 //
427 // All additional attrs passed are included in the recorded value.
428 func (m ClientSessionDuration) Record(
429 ctx context.Context,
430 val float64,
431 attrs ...attribute.KeyValue,
432 ) {
433 if len(attrs) == 0 {
434 m.Float64Histogram.Record(ctx, val)
435 return
436 }
437
438 o := recOptPool.Get().(*[]metric.RecordOption)
439 defer func() {
440 *o = (*o)[:0]
441 recOptPool.Put(o)
442 }()
443
444 *o = append(
445 *o,
446 metric.WithAttributes(
447 attrs...,
448 ),
449 )
450
451 m.Float64Histogram.Record(ctx, val, *o...)
452 }
453
454 // RecordSet records val to the current distribution for set.
455 func (m ClientSessionDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
456 if set.Len() == 0 {
457 m.Float64Histogram.Record(ctx, val)
458 return
459 }
460
461 o := recOptPool.Get().(*[]metric.RecordOption)
462 defer func() {
463 *o = (*o)[:0]
464 recOptPool.Put(o)
465 }()
466
467 *o = append(*o, metric.WithAttributeSet(set))
468 m.Float64Histogram.Record(ctx, val, *o...)
469 }
470
471 // AttrErrorType returns an optional attribute for the "error.type" semantic
472 // convention. It represents the describes a class of error the operation ended
473 // with.
474 func (ClientSessionDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
475 return attribute.String("error.type", string(val))
476 }
477
478 // AttrJSONRPCProtocolVersion returns an optional attribute for the
479 // "jsonrpc.protocol.version" semantic convention. It represents the protocol
480 // version, as specified in the `jsonrpc` property of the request and its
481 // corresponding response.
482 func (ClientSessionDuration) AttrJSONRPCProtocolVersion(val string) attribute.KeyValue {
483 return attribute.String("jsonrpc.protocol.version", val)
484 }
485
486 // AttrProtocolVersion returns an optional attribute for the
487 // "mcp.protocol.version" semantic convention. It represents the [version] of the
488 // Model Context Protocol used.
489 //
490 // [version]: https://modelcontextprotocol.io/specification/versioning
491 func (ClientSessionDuration) AttrProtocolVersion(val string) attribute.KeyValue {
492 return attribute.String("mcp.protocol.version", val)
493 }
494
495 // AttrNetworkProtocolName returns an optional attribute for the
496 // "network.protocol.name" semantic convention. It represents the
497 // [OSI application layer] or non-OSI equivalent.
498 //
499 // [OSI application layer]: https://wikipedia.org/wiki/Application_layer
500 func (ClientSessionDuration) AttrNetworkProtocolName(val string) attribute.KeyValue {
501 return attribute.String("network.protocol.name", val)
502 }
503
504 // AttrNetworkProtocolVersion returns an optional attribute for the
505 // "network.protocol.version" semantic convention. It represents the actual
506 // version of the protocol used for network communication.
507 func (ClientSessionDuration) AttrNetworkProtocolVersion(val string) attribute.KeyValue {
508 return attribute.String("network.protocol.version", val)
509 }
510
511 // AttrNetworkTransport returns an optional attribute for the "network.transport"
512 // semantic convention. It represents the transport protocol used for the MCP
513 // session.
514 func (ClientSessionDuration) AttrNetworkTransport(val NetworkTransportAttr) attribute.KeyValue {
515 return attribute.String("network.transport", string(val))
516 }
517
518 // AttrServerAddress returns an optional attribute for the "server.address"
519 // semantic convention. It represents the server domain name if available without
520 // reverse DNS lookup; otherwise, IP address or Unix domain socket name.
521 func (ClientSessionDuration) AttrServerAddress(val string) attribute.KeyValue {
522 return attribute.String("server.address", val)
523 }
524
525 // AttrServerPort returns an optional attribute for the "server.port" semantic
526 // convention. It represents the server port number.
527 func (ClientSessionDuration) AttrServerPort(val int) attribute.KeyValue {
528 return attribute.Int("server.port", val)
529 }
530
531 // ServerOperationDuration is an instrument used to record metric values
532 // conforming to the "mcp.server.operation.duration" semantic conventions. It
533 // represents the MCP request or notification duration as observed on the
534 // receiver from the time it was received until the result or ack is sent.
535 type ServerOperationDuration struct {
536 metric.Float64Histogram
537 }
538
539 var newServerOperationDurationOpts = []metric.Float64HistogramOption{
540 metric.WithDescription("MCP request or notification duration as observed on the receiver from the time it was received until the result or ack is sent."),
541 metric.WithUnit("s"),
542 }
543
544 // NewServerOperationDuration returns a new ServerOperationDuration instrument.
545 func NewServerOperationDuration(
546 m metric.Meter,
547 opt ...metric.Float64HistogramOption,
548 ) (ServerOperationDuration, error) {
549 // Check if the meter is nil.
550 if m == nil {
551 return ServerOperationDuration{noop.Float64Histogram{}}, nil
552 }
553
554 if len(opt) == 0 {
555 opt = newServerOperationDurationOpts
556 } else {
557 opt = append(opt, newServerOperationDurationOpts...)
558 }
559
560 i, err := m.Float64Histogram(
561 "mcp.server.operation.duration",
562 opt...,
563 )
564 if err != nil {
565 return ServerOperationDuration{noop.Float64Histogram{}}, err
566 }
567 return ServerOperationDuration{i}, nil
568 }
569
570 // Inst returns the underlying metric instrument.
571 func (m ServerOperationDuration) Inst() metric.Float64Histogram {
572 return m.Float64Histogram
573 }
574
575 // Name returns the semantic convention name of the instrument.
576 func (ServerOperationDuration) Name() string {
577 return "mcp.server.operation.duration"
578 }
579
580 // Unit returns the semantic convention unit of the instrument
581 func (ServerOperationDuration) Unit() string {
582 return "s"
583 }
584
585 // Description returns the semantic convention description of the instrument
586 func (ServerOperationDuration) Description() string {
587 return "MCP request or notification duration as observed on the receiver from the time it was received until the result or ack is sent."
588 }
589
590 // Record records val to the current distribution for attrs.
591 //
592 // The methodName is the the name of the request or notification method.
593 //
594 // All additional attrs passed are included in the recorded value.
595 func (m ServerOperationDuration) Record(
596 ctx context.Context,
597 val float64,
598 methodName MethodNameAttr,
599 attrs ...attribute.KeyValue,
600 ) {
601 if len(attrs) == 0 {
602 m.Float64Histogram.Record(ctx, val, metric.WithAttributes(
603 attribute.String("mcp.method.name", string(methodName)),
604 ))
605 return
606 }
607
608 o := recOptPool.Get().(*[]metric.RecordOption)
609 defer func() {
610 *o = (*o)[:0]
611 recOptPool.Put(o)
612 }()
613
614 *o = append(
615 *o,
616 metric.WithAttributes(
617 append(
618 attrs[:len(attrs):len(attrs)],
619 attribute.String("mcp.method.name", string(methodName)),
620 )...,
621 ),
622 )
623
624 m.Float64Histogram.Record(ctx, val, *o...)
625 }
626
627 // RecordSet records val to the current distribution for set.
628 func (m ServerOperationDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
629 if set.Len() == 0 {
630 m.Float64Histogram.Record(ctx, val)
631 return
632 }
633
634 o := recOptPool.Get().(*[]metric.RecordOption)
635 defer func() {
636 *o = (*o)[:0]
637 recOptPool.Put(o)
638 }()
639
640 *o = append(*o, metric.WithAttributeSet(set))
641 m.Float64Histogram.Record(ctx, val, *o...)
642 }
643
644 // AttrErrorType returns an optional attribute for the "error.type" semantic
645 // convention. It represents the describes a class of error the operation ended
646 // with.
647 func (ServerOperationDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
648 return attribute.String("error.type", string(val))
649 }
650
651 // AttrGenAIPromptName returns an optional attribute for the "gen_ai.prompt.name"
652 // semantic convention. It represents the name of the prompt or prompt template
653 // provided in the request or response.
654 func (ServerOperationDuration) AttrGenAIPromptName(val string) attribute.KeyValue {
655 return attribute.String("gen_ai.prompt.name", val)
656 }
657
658 // AttrGenAIToolName returns an optional attribute for the "gen_ai.tool.name"
659 // semantic convention. It represents the name of the tool utilized by the agent.
660 func (ServerOperationDuration) AttrGenAIToolName(val string) attribute.KeyValue {
661 return attribute.String("gen_ai.tool.name", val)
662 }
663
664 // AttrRPCResponseStatusCode returns an optional attribute for the
665 // "rpc.response.status_code" semantic convention. It represents the error code
666 // from the JSON-RPC response.
667 func (ServerOperationDuration) AttrRPCResponseStatusCode(val string) attribute.KeyValue {
668 return attribute.String("rpc.response.status_code", val)
669 }
670
671 // AttrGenAIOperationName returns an optional attribute for the
672 // "gen_ai.operation.name" semantic convention. It represents the name of the
673 // GenAI operation being performed.
674 func (ServerOperationDuration) AttrGenAIOperationName(val GenAIOperationNameAttr) attribute.KeyValue {
675 return attribute.String("gen_ai.operation.name", string(val))
676 }
677
678 // AttrJSONRPCProtocolVersion returns an optional attribute for the
679 // "jsonrpc.protocol.version" semantic convention. It represents the protocol
680 // version, as specified in the `jsonrpc` property of the request and its
681 // corresponding response.
682 func (ServerOperationDuration) AttrJSONRPCProtocolVersion(val string) attribute.KeyValue {
683 return attribute.String("jsonrpc.protocol.version", val)
684 }
685
686 // AttrProtocolVersion returns an optional attribute for the
687 // "mcp.protocol.version" semantic convention. It represents the [version] of the
688 // Model Context Protocol used.
689 //
690 // [version]: https://modelcontextprotocol.io/specification/versioning
691 func (ServerOperationDuration) AttrProtocolVersion(val string) attribute.KeyValue {
692 return attribute.String("mcp.protocol.version", val)
693 }
694
695 // AttrNetworkProtocolName returns an optional attribute for the
696 // "network.protocol.name" semantic convention. It represents the
697 // [OSI application layer] or non-OSI equivalent.
698 //
699 // [OSI application layer]: https://wikipedia.org/wiki/Application_layer
700 func (ServerOperationDuration) AttrNetworkProtocolName(val string) attribute.KeyValue {
701 return attribute.String("network.protocol.name", val)
702 }
703
704 // AttrNetworkProtocolVersion returns an optional attribute for the
705 // "network.protocol.version" semantic convention. It represents the actual
706 // version of the protocol used for network communication.
707 func (ServerOperationDuration) AttrNetworkProtocolVersion(val string) attribute.KeyValue {
708 return attribute.String("network.protocol.version", val)
709 }
710
711 // AttrNetworkTransport returns an optional attribute for the "network.transport"
712 // semantic convention. It represents the transport protocol used for the MCP
713 // session.
714 func (ServerOperationDuration) AttrNetworkTransport(val NetworkTransportAttr) attribute.KeyValue {
715 return attribute.String("network.transport", string(val))
716 }
717
718 // AttrResourceURI returns an optional attribute for the "mcp.resource.uri"
719 // semantic convention. It represents the value of the resource uri.
720 func (ServerOperationDuration) AttrResourceURI(val string) attribute.KeyValue {
721 return attribute.String("mcp.resource.uri", val)
722 }
723
724 // ServerSessionDuration is an instrument used to record metric values conforming
725 // to the "mcp.server.session.duration" semantic conventions. It represents the
726 // duration of the MCP session as observed on the MCP server.
727 type ServerSessionDuration struct {
728 metric.Float64Histogram
729 }
730
731 var newServerSessionDurationOpts = []metric.Float64HistogramOption{
732 metric.WithDescription("The duration of the MCP session as observed on the MCP server."),
733 metric.WithUnit("s"),
734 }
735
736 // NewServerSessionDuration returns a new ServerSessionDuration instrument.
737 func NewServerSessionDuration(
738 m metric.Meter,
739 opt ...metric.Float64HistogramOption,
740 ) (ServerSessionDuration, error) {
741 // Check if the meter is nil.
742 if m == nil {
743 return ServerSessionDuration{noop.Float64Histogram{}}, nil
744 }
745
746 if len(opt) == 0 {
747 opt = newServerSessionDurationOpts
748 } else {
749 opt = append(opt, newServerSessionDurationOpts...)
750 }
751
752 i, err := m.Float64Histogram(
753 "mcp.server.session.duration",
754 opt...,
755 )
756 if err != nil {
757 return ServerSessionDuration{noop.Float64Histogram{}}, err
758 }
759 return ServerSessionDuration{i}, nil
760 }
761
762 // Inst returns the underlying metric instrument.
763 func (m ServerSessionDuration) Inst() metric.Float64Histogram {
764 return m.Float64Histogram
765 }
766
767 // Name returns the semantic convention name of the instrument.
768 func (ServerSessionDuration) Name() string {
769 return "mcp.server.session.duration"
770 }
771
772 // Unit returns the semantic convention unit of the instrument
773 func (ServerSessionDuration) Unit() string {
774 return "s"
775 }
776
777 // Description returns the semantic convention description of the instrument
778 func (ServerSessionDuration) Description() string {
779 return "The duration of the MCP session as observed on the MCP server."
780 }
781
782 // Record records val to the current distribution for attrs.
783 //
784 // All additional attrs passed are included in the recorded value.
785 func (m ServerSessionDuration) Record(
786 ctx context.Context,
787 val float64,
788 attrs ...attribute.KeyValue,
789 ) {
790 if len(attrs) == 0 {
791 m.Float64Histogram.Record(ctx, val)
792 return
793 }
794
795 o := recOptPool.Get().(*[]metric.RecordOption)
796 defer func() {
797 *o = (*o)[:0]
798 recOptPool.Put(o)
799 }()
800
801 *o = append(
802 *o,
803 metric.WithAttributes(
804 attrs...,
805 ),
806 )
807
808 m.Float64Histogram.Record(ctx, val, *o...)
809 }
810
811 // RecordSet records val to the current distribution for set.
812 func (m ServerSessionDuration) RecordSet(ctx context.Context, val float64, set attribute.Set) {
813 if set.Len() == 0 {
814 m.Float64Histogram.Record(ctx, val)
815 return
816 }
817
818 o := recOptPool.Get().(*[]metric.RecordOption)
819 defer func() {
820 *o = (*o)[:0]
821 recOptPool.Put(o)
822 }()
823
824 *o = append(*o, metric.WithAttributeSet(set))
825 m.Float64Histogram.Record(ctx, val, *o...)
826 }
827
828 // AttrErrorType returns an optional attribute for the "error.type" semantic
829 // convention. It represents the describes a class of error the operation ended
830 // with.
831 func (ServerSessionDuration) AttrErrorType(val ErrorTypeAttr) attribute.KeyValue {
832 return attribute.String("error.type", string(val))
833 }
834
835 // AttrJSONRPCProtocolVersion returns an optional attribute for the
836 // "jsonrpc.protocol.version" semantic convention. It represents the protocol
837 // version, as specified in the `jsonrpc` property of the request and its
838 // corresponding response.
839 func (ServerSessionDuration) AttrJSONRPCProtocolVersion(val string) attribute.KeyValue {
840 return attribute.String("jsonrpc.protocol.version", val)
841 }
842
843 // AttrProtocolVersion returns an optional attribute for the
844 // "mcp.protocol.version" semantic convention. It represents the [version] of the
845 // Model Context Protocol used.
846 //
847 // [version]: https://modelcontextprotocol.io/specification/versioning
848 func (ServerSessionDuration) AttrProtocolVersion(val string) attribute.KeyValue {
849 return attribute.String("mcp.protocol.version", val)
850 }
851
852 // AttrNetworkProtocolName returns an optional attribute for the
853 // "network.protocol.name" semantic convention. It represents the
854 // [OSI application layer] or non-OSI equivalent.
855 //
856 // [OSI application layer]: https://wikipedia.org/wiki/Application_layer
857 func (ServerSessionDuration) AttrNetworkProtocolName(val string) attribute.KeyValue {
858 return attribute.String("network.protocol.name", val)
859 }
860
861 // AttrNetworkProtocolVersion returns an optional attribute for the
862 // "network.protocol.version" semantic convention. It represents the actual
863 // version of the protocol used for network communication.
864 func (ServerSessionDuration) AttrNetworkProtocolVersion(val string) attribute.KeyValue {
865 return attribute.String("network.protocol.version", val)
866 }
867
868 // AttrNetworkTransport returns an optional attribute for the "network.transport"
869 // semantic convention. It represents the transport protocol used for the MCP
870 // session.
871 func (ServerSessionDuration) AttrNetworkTransport(val NetworkTransportAttr) attribute.KeyValue {
872 return attribute.String("network.transport", string(val))
873 }