Debian Codegraph @

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