@@ -27,10 +27,6 @@ type hooks struct {
2727 spanCache * sync.Map
2828}
2929
30- type textIOAnnotator interface {
31- AnnotateTextIO (input , output string , opts ... llmobs.AnnotateOption )
32- }
33-
3430// AddServerHooks appends Datadog tracing hooks to an existing server.Hooks object.
3531func AddServerHooks (hooks * server.Hooks ) {
3632 ddHooks := newHooks ()
@@ -100,29 +96,34 @@ func (h *hooks) onError(ctx context.Context, id any, method mcp.MCPMethod, messa
10096 if ! ok {
10197 return
10298 }
103- span , ok := value .(llmobs.Span )
99+
100+ span , ok := value .(* llmobs.TaskSpan )
104101 if ! ok {
105102 return
106103 }
104+
105+ defer span .Finish (llmobs .WithError (err ))
106+
107107 inputJSON , marshalErr := json .Marshal (message )
108108 if marshalErr != nil {
109109 instr .Logger ().Warn ("mcp-go: failed to marshal error message: %v" , marshalErr )
110110 }
111- if annotator , ok := span .(textIOAnnotator ); ok {
112- annotator .AnnotateTextIO (string (inputJSON ), err .Error ())
113- }
114- span .Finish (llmobs .WithError (err ))
111+ span .AnnotateTextIO (string (inputJSON ), err .Error ())
112+
115113}
116114
117115func finishSpanWithIO [Req any , Res any ](h * hooks , id any , request Req , result Res ) {
118116 value , ok := h .spanCache .LoadAndDelete (id )
119117 if ! ok {
120118 return
121119 }
122- span , ok := value .(llmobs.Span )
120+ span , ok := value .(* llmobs.TaskSpan )
123121 if ! ok {
124122 return
125123 }
124+
125+ defer span .Finish ()
126+
126127 inputJSON , marshalErr := json .Marshal (request )
127128 if marshalErr != nil {
128129 instr .Logger ().Warn ("mcp-go: failed to marshal request: %v" , marshalErr )
@@ -131,10 +132,6 @@ func finishSpanWithIO[Req any, Res any](h *hooks, id any, request Req, result Re
131132 if marshalErr != nil {
132133 instr .Logger ().Warn ("mcp-go: failed to marshal result: %v" , marshalErr )
133134 }
134- outputText := string (resultJSON )
135135
136- if annotator , ok := span .(textIOAnnotator ); ok {
137- annotator .AnnotateTextIO (string (inputJSON ), outputText )
138- }
139- span .Finish ()
136+ span .AnnotateTextIO (string (inputJSON ), string (resultJSON ))
140137}
0 commit comments