monitors.go.disabled•4.57 kB
// internal/tools/monitors.go
package tools
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/chussenot/datadog-mcp/internal/client"
"github.com/chussenot/datadog-mcp/internal/types"
v1 "github.com/chussenot/datadog-mcp/internal/api/v1"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
// ListMonitors lists all monitors in DataDog
func ListMonitors(ctx context.Context, client *client.DataDogClient, args types.ListMonitorsArgs) (*mcp.CallToolResult, any, error) {
log.Printf("🔍 Calling DataDog API: ListMonitors with filters: %+v", args)
params := &v1.ListMonitorsParams{}
if args.GroupStates != nil {
params.GroupStates = args.GroupStates
}
if args.Name != nil {
params.Name = args.Name
}
if args.Tags != nil {
params.Tags = args.Tags
}
resp, err := client.Client.ListMonitorsWithResponse(ctx, params)
if err != nil {
log.Printf("❌ API call error: %v", err)
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("API call failed: %v", err)}},
}, nil, nil
}
log.Printf("📊 API Response Status: %d", resp.StatusCode())
log.Printf("📊 API Response Headers: %+v", resp.HTTPResponse.Header)
// Check response status
if resp.StatusCode() < 200 || resp.StatusCode() >= 300 {
body := "No response body"
if resp.Body != nil {
body = string(resp.Body)
}
log.Printf("❌ API error response: status %d, body: %s", resp.StatusCode(), body)
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("API returned status %d: %s", resp.StatusCode(), body)}},
}, nil, nil
}
// Check if we have JSON response data
if resp.JSON200 == nil {
log.Printf("❌ No JSON200 data in response")
log.Printf("❌ Response body: %s", string(resp.Body))
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{&mcp.TextContent{Text: "No JSON response data available"}},
}, nil, nil
}
// Convert the response to JSON string
jsonBytes, err := json.Marshal(resp.JSON200)
if err != nil {
log.Printf("❌ Failed to marshal JSON response: %v", err)
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Failed to marshal response: %v", err)}},
}, nil, nil
}
monitorCount := 0
if resp.JSON200 != nil {
monitorCount = len(*resp.JSON200)
}
log.Printf("✅ Successfully retrieved %d monitors", monitorCount)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: string(jsonBytes)},
},
}, nil, nil
}
// GetMonitor gets details of a specific monitor
func GetMonitor(ctx context.Context, client *client.DataDogClient, args types.GetMonitorArgs) (*mcp.CallToolResult, any, error) {
log.Printf("🔍 Calling DataDog API: GetMonitor with ID: %d", args.MonitorID)
resp, err := client.Client.GetMonitorWithResponse(ctx, args.MonitorID, &v1.GetMonitorParams{})
if err != nil {
log.Printf("❌ API call error: %v", err)
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("API call failed: %v", err)}},
}, nil, nil
}
log.Printf("📊 API Response Status: %d", resp.StatusCode())
log.Printf("📊 API Response Headers: %+v", resp.HTTPResponse.Header)
// Check response status
if resp.StatusCode() < 200 || resp.StatusCode() >= 300 {
body := "No response body"
if resp.Body != nil {
body = string(resp.Body)
}
log.Printf("❌ API error response: status %d, body: %s", resp.StatusCode(), body)
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("API returned status %d: %s", resp.StatusCode(), body)}},
}, nil, nil
}
// Check if we have JSON response data
if resp.JSON200 == nil {
log.Printf("❌ No JSON200 data in response")
log.Printf("❌ Response body: %s", string(resp.Body))
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{&mcp.TextContent{Text: "No JSON response data available"}},
}, nil, nil
}
// Convert the response to JSON string
jsonBytes, err := json.Marshal(resp.JSON200)
if err != nil {
log.Printf("❌ Failed to marshal JSON response: %v", err)
return &mcp.CallToolResult{
IsError: true,
Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("Failed to marshal response: %v", err)}},
}, nil, nil
}
log.Printf("✅ Successfully retrieved monitor: %s", resp.JSON200.Name)
return &mcp.CallToolResult{
Content: []mcp.Content{
&mcp.TextContent{Text: string(jsonBytes)},
},
}, nil, nil
}