sendEvent sends a single event to Docker events API and handles logging
(ctx context.Context, event *EventPayload)
| 61 | |
| 62 | // sendEvent sends a single event to Docker events API and handles logging |
| 63 | func (tc *Client) sendEvent(ctx context.Context, event *EventPayload) { |
| 64 | // Get version before acquiring lock to avoid deadlock |
| 65 | version := tc.getVersion() |
| 66 | |
| 67 | tc.mu.Lock() |
| 68 | defer tc.mu.Unlock() |
| 69 | |
| 70 | // Send to Docker events API if conditions are met |
| 71 | if tc.apiKey != "" && tc.endpoint != "" && tc.enabled { |
| 72 | tc.logger.Debug("Sending telemetry event via HTTP", "event_type", event.Event, "endpoint", tc.endpoint) |
| 73 | |
| 74 | // Perform HTTP request inline |
| 75 | if err := tc.performHTTPRequest(ctx, event, version); err != nil { |
| 76 | tc.logger.Debug("Failed to send telemetry event to Docker API", "error", err, "event_type", event.Event) |
| 77 | } else { |
| 78 | tc.logger.Debug("Successfully sent telemetry event via HTTP", "event_type", event.Event) |
| 79 | } |
| 80 | } else { |
| 81 | tc.logger.Debug("Skipping HTTP telemetry event - missing endpoint or API key or disabled", |
| 82 | "event_type", event.Event, |
| 83 | "has_endpoint", tc.endpoint != "", |
| 84 | "has_api_key", tc.apiKey != "", |
| 85 | "enabled", tc.enabled) |
| 86 | } |
| 87 | |
| 88 | // Log the event |
| 89 | logArgs := []any{ |
| 90 | "event", event.Event, |
| 91 | "event_timestamp", event.EventTimestamp, |
| 92 | "source", event.Source, |
| 93 | } |
| 94 | |
| 95 | // Add key properties to log |
| 96 | if userUUID, ok := event.Properties["user_uuid"].(string); ok { |
| 97 | logArgs = append(logArgs, "user_uuid", userUUID) |
| 98 | } |
| 99 | if sessionID, ok := event.Properties["session_id"].(string); ok { |
| 100 | logArgs = append(logArgs, "session_id", sessionID) |
| 101 | } |
| 102 | |
| 103 | tc.logger.Debug("Event recorded", logArgs...) |
| 104 | |
| 105 | // Enhanced debug logging with full event structure |
| 106 | if tc.logger.Enabled(ctx, slog.LevelDebug) { |
| 107 | if jsonData, err := json.Marshal(event); err == nil { |
| 108 | tc.logger.Debug("Full telemetry event JSON", "json", string(jsonData)) |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // performHTTPRequest handles the actual HTTP request to the telemetry API |
| 114 | func (tc *Client) performHTTPRequest(ctx context.Context, event *EventPayload, version string) error { |
no test coverage detected