include/pmcp/pmcp_otel.h

filter: Statementstmt 100% 65 / 65 Branchbran 68.5% 48 / 70 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.7% 113 / 135 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_oa.h ↑ Summary include/pmcp/pmcp_bus.h »
1 /* pmcp_otel.h - a span per MCP request, under Punk-OpenTelemetry.
2 *
3 * Needs: pmcp_clos.h, pmcp_rpc.h, pmcp_proto.h.
4 *
5 * Punk-OpenTelemetry names the request's server span from its route, so every
6 * call would read `POST /mcp`. Each request with an id gets a child span of
7 * that one instead, named and attributed as the OpenTelemetry MCP conventions
8 * say: "{mcp.method.name} {target}", where the target is the tool or prompt
9 * name, kind SERVER, with mcp.method.name, gen_ai.tool.name or
10 * gen_ai.prompt.name, mcp.resource.uri, jsonrpc.request.id, mcp.session.id and
11 * mcp.protocol.version. A JSON-RPC error sets error.type to its code and the
12 * status to ERROR. A tool result with isError sets error.type tool_error and
13 * leaves the status alone, since the call worked.
14 *
15 * Nothing happens without the plugin (the context has no otel_span method),
16 * when the request was not sampled (otel_span is undef), or over stdio (no
17 * server span to hang it on).
18 */
19  
20 #ifndef PMCP_OTEL_H
21 #define PMCP_OTEL_H
22  
23 #define PMCP_OTEL_KIND_SERVER 2
24 #define PMCP_OTEL_STATUS_ERROR 2
25  
26 25 static void pmcp_otel_attr(pTHX_ SV *span, const char *k, SV *v)
27 {
28 SV *argv[2], *r;
29 25 ▶ if (!v || !SvOK(v)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(v)) return; 25 0 25
if (!v || !SvOK(v)) return; 0 25 25
30 25 argv[0] = sv_2mortal(newSVpv(k, 0));
31 25 argv[1] = v;
32 25 r = pmcp_call_method(aTHX_ span, "attr", argv, 2, G_SCALAR, NULL);
33 25 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 25 0 25
34 }
35  
36 /* The span, mortal, or NULL. *tracer is set mortal alongside it. */
37 415 static SV *pmcp_otel_start(pTHX_ pmcp_req *r, const char *method, STRLEN mlen,
38 HV *params, SV **tracer)
39 {
40 415 SV *ref, *tid, *sid, *span, *argv[5], *name, *err = NULL;
41 HV *parent, *stash;
42 415 SV *target = NULL, *uri = NULL;
43  
44 415 *tracer = NULL;
45 415 ▶ if (!r->c || r->stdio || !SvROK(r->c) || !SvOBJECT(SvRV(r->c))) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!r->c || r->stdio || !SvROK(r->c) || !SvOBJECT(SvRV(r->c))) return NULL; 375 40 415
if (!r->c || r->stdio || !SvROK(r->c) || !SvOBJECT(SvRV(r->c))) return NULL; 361 14 375
if (!r->c || r->stdio || !SvROK(r->c) || !SvOBJECT(SvRV(r->c))) return NULL; 361 0 361
if (!r->c || r->stdio || !SvROK(r->c) || !SvOBJECT(SvRV(r->c))) return NULL; 0 361 361
46 361 stash = SvSTASH(SvRV(r->c));
47 361 ▶ if (!gv_fetchmethod_autoload(stash, "otel_span", 0)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!gv_fetchmethod_autoload(stash, "otel_span", 0)) return NULL; 354 7 361
48  
49 7 ref = pmcp_call_method(aTHX_ r->c, "otel_span", NULL, 0, G_SCALAR, &err);
50 7 ▶ if (!ref) { SvREFCNT_dec(err); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!ref) { SvREFCNT_dec(err); return NULL; } 0 7 7
51 7 sv_2mortal(ref);
52 7 ▶ if (!SvROK(ref)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(ref)) return NULL; 1 6 7
53 6 *tracer = pmcp_call_method(aTHX_ r->c, "otel", NULL, 0, G_SCALAR, &err);
54 6 ▶ if (!*tracer) { SvREFCNT_dec(err); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!*tracer) { SvREFCNT_dec(err); return NULL; } 0 6 6
55 6 sv_2mortal(*tracer);
56 6 ▶ if (!SvROK(*tracer)) { *tracer = NULL; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(*tracer)) { *tracer = NULL; return NULL; } 0 6 6
57  
58 6 tid = pmcp_call_method(aTHX_ ref, "trace_id", NULL, 0, G_SCALAR, NULL);
59 6 sid = pmcp_call_method(aTHX_ ref, "span_id", NULL, 0, G_SCALAR, NULL);
60 6 parent = newHV();
61 6 ▶ (void)hv_stores(parent, "trace_id", tid ? tid : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(parent, "trace_id", tid ? tid : newSV(0)); 0 6 6
62 6 ▶ (void)hv_stores(parent, "span_id", sid ? sid : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(parent, "span_id", sid ? sid : newSV(0)); 0 6 6
63 6 (void)hv_stores(parent, "sampled", newSViv(1));
64  
65 6 ▶ if (params) {
Branch: 1 branch
BranchTrueFalseTotal
if (params) { 5 1 6
66 5 SV **nv = hv_fetchs(params, "name", 0);
67 5 SV **uv = hv_fetchs(params, "uri", 0);
68 5 ▶ if (nv && pmcp_is_str(*nv)) target = *nv;
Branch: 2 branches
BranchTrueFalseTotal
if (nv && pmcp_is_str(*nv)) target = *nv; 4 1 5
if (nv && pmcp_is_str(*nv)) target = *nv; 4 0 4
69 5 ▶ if (uv && pmcp_is_str(*uv)) uri = *uv;
Branch: 2 branches
BranchTrueFalseTotal
if (uv && pmcp_is_str(*uv)) uri = *uv; 1 4 5
if (uv && pmcp_is_str(*uv)) uri = *uv; 1 0 1
70 }
71 6 name = sv_2mortal(newSVpvn(method, mlen));
72 6 ▶ if (target) sv_catpvf(name, " %" SVf, SVfARG(target));
Branch: 1 branch
BranchTrueFalseTotal
if (target) sv_catpvf(name, " %" SVf, SVfARG(target)); 4 2 6
73  
74 6 argv[0] = name;
75 6 argv[1] = sv_2mortal(newSVpvs("kind"));
76 6 argv[2] = sv_2mortal(newSViv(PMCP_OTEL_KIND_SERVER));
77 6 argv[3] = sv_2mortal(newSVpvs("parent"));
78 6 argv[4] = sv_2mortal(newRV_noinc((SV *)parent));
79 6 span = pmcp_call_method(aTHX_ *tracer, "start", argv, 5, G_SCALAR, &err);
80 6 ▶ if (!span) { SvREFCNT_dec(err); *tracer = NULL; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!span) { SvREFCNT_dec(err); *tracer = NULL; return NULL; } 0 6 6
81 6 sv_2mortal(span);
82 6 ▶ if (!SvROK(span)) { *tracer = NULL; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(span)) { *tracer = NULL; return NULL; } 0 6 6
83  
84 6 pmcp_otel_attr(aTHX_ span, "mcp.method.name", sv_2mortal(newSVpvn(method, mlen)));
85 6 ▶ if (target)
Branch: 1 branch
BranchTrueFalseTotal
if (target) 4 2 6
86 4 ▶ pmcp_otel_attr(aTHX_ span, (mlen == 11 && memEQ(method, "prompts/get", 11))
Branch: 2 branches
BranchTrueFalseTotal
pmcp_otel_attr(aTHX_ span, (mlen == 11 && memEQ(method, "prompts/get", 11)) 1 3 4
pmcp_otel_attr(aTHX_ span, (mlen == 11 && memEQ(method, "prompts/get", 11)) 1 0 1
87 ? "gen_ai.prompt.name" : "gen_ai.tool.name", target);
88 6 ▶ if (uri) pmcp_otel_attr(aTHX_ span, "mcp.resource.uri", uri);
Branch: 1 branch
BranchTrueFalseTotal
if (uri) pmcp_otel_attr(aTHX_ span, "mcp.resource.uri", uri); 1 5 6
89 6 ▶ if (r->id) pmcp_otel_attr(aTHX_ span, "jsonrpc.request.id", sv_2mortal(newSVpvf("%" SVf, SVfARG(r->id))));
Branch: 1 branch
BranchTrueFalseTotal
if (r->id) pmcp_otel_attr(aTHX_ span, "jsonrpc.request.id", sv_2mortal(newSVpvf("%" SVf, SVfARG(r->id)))); 6 0 6
90 6 ▶ if (r->session) pmcp_otel_attr(aTHX_ span, "mcp.session.id", r->session);
Branch: 1 branch
BranchTrueFalseTotal
if (r->session) pmcp_otel_attr(aTHX_ span, "mcp.session.id", r->session); 0 6 6
91 6 pmcp_otel_attr(aTHX_ span, "mcp.protocol.version",
92 6 sv_2mortal(newSVpv(PMCP_VER_NAME[r->version], 0)));
93 6 return span;
94 }
95  
96 /* End the span and queue it. ecode is the JSON-RPC error code, or 0. */
97 6 static void pmcp_otel_end(pTHX_ pmcp_req *r, SV *span, SV *tracer, int ecode)
98 {
99 SV *argv[2], *ret;
100 6 ▶ if (!span || !tracer) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!span || !tracer) return; 6 0 6
if (!span || !tracer) return; 0 6 6
101 6 ▶ if (ecode) {
Branch: 1 branch
BranchTrueFalseTotal
if (ecode) { 1 5 6
102 1 pmcp_otel_attr(aTHX_ span, "error.type", sv_2mortal(newSVpvf("%d", ecode)));
103 1 argv[0] = sv_2mortal(newSViv(PMCP_OTEL_STATUS_ERROR));
104 1 ▶ argv[1] = sv_2mortal(newSVpv(r->emsg ? r->emsg : "error", 0));
Branch: 1 branch
BranchTrueFalseTotal
argv[1] = sv_2mortal(newSVpv(r->emsg ? r->emsg : "error", 0)); 1 0 1
105 1 ret = pmcp_call_method(aTHX_ span, "status", argv, 2, G_SCALAR, NULL);
106 1 ▶ if (ret) SvREFCNT_dec(ret);
Branch: 1 branch
BranchTrueFalseTotal
if (ret) SvREFCNT_dec(ret); 1 0 1
107 }
108 5 ▶ else if (r->tool_error) {
Branch: 1 branch
BranchTrueFalseTotal
else if (r->tool_error) { 1 4 5
109 1 pmcp_otel_attr(aTHX_ span, "error.type", sv_2mortal(newSVpvs("tool_error")));
110 }
111 6 argv[0] = span;
112 6 ret = pmcp_call_method(aTHX_ tracer, "enqueue", argv, 1, G_SCALAR, NULL);
113 6 ▶ if (ret) SvREFCNT_dec(ret);
Branch: 1 branch
BranchTrueFalseTotal
if (ret) SvREFCNT_dec(ret); 6 0 6
114 }
115  
116 #endif /* PMCP_OTEL_H */
« include/pmcp/pmcp_oa.h ↑ Summary include/pmcp/pmcp_bus.h »