Skip to content

Commit

Permalink
Report DataDog internal Event ID (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
fkorotkov authored Sep 20, 2023
1 parent dd249d7 commit 272a534
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
7 changes: 4 additions & 3 deletions internal/command/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func processWebhookEvent(
logger.Debugf("skipping event of type %q because we only process events of types %s",
presentedEventType, strings.Join(eventTypesSet.ToSlice(), ", "))

return nil
return ctx.String(http.StatusOK, fmt.Sprintf("skipping event of type %q", presentedEventType))
}

body, err := io.ReadAll(ctx.Request().Body)
Expand All @@ -183,11 +183,12 @@ func processWebhookEvent(
// Enrich the event with tags
enrichEventWithTags(body, evt, logger)

if err := sender.SendEvent(ctx.Request().Context(), evt); err != nil {
message, err := sender.SendEvent(ctx.Request().Context(), evt)
if err != nil {
return fmt.Errorf("%w: %v", ErrDatadogFailed, err)
}

return nil
return ctx.String(http.StatusCreated, message)
}

func verifyEvent(ctx echo.Context, body []byte) error {
Expand Down
12 changes: 8 additions & 4 deletions internal/datadogsender/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewAPISender(apiKey string, apiSite string) (*APISender, error) {
}, nil
}

func (sender *APISender) SendEvent(ctx context.Context, event *Event) error {
func (sender *APISender) SendEvent(ctx context.Context, event *Event) (string, error) {
ctx = context.WithValue(
ctx,
datadog.ContextAPIKeys,
Expand All @@ -47,14 +47,18 @@ func (sender *APISender) SendEvent(ctx context.Context, event *Event) error {
"site": sender.apiSite,
})

_, _, err := sender.eventsAPI.CreateEvent(ctx, datadogV1.EventCreateRequest{
response, _, err := sender.eventsAPI.CreateEvent(ctx, datadogV1.EventCreateRequest{
Title: event.Title,
Text: event.Text,
Tags: event.Tags,
})
if err != nil {
return fmt.Errorf("%w: %v", ErrAPISenderFailed, err)
return "", fmt.Errorf("%w: %v", ErrAPISenderFailed, err)
}

return nil
if response.Event == nil {
return "", fmt.Errorf("%w: %v", ErrAPISenderFailed, "response.Event is nil")
}

return fmt.Sprintf("DataDog event id: %v", response.Event.Id), nil
}
2 changes: 1 addition & 1 deletion internal/datadogsender/datadogsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ type Event struct {
}

type Sender interface {
SendEvent(context.Context, *Event) error
SendEvent(context.Context, *Event) (string, error)
}
6 changes: 3 additions & 3 deletions internal/datadogsender/dogstatsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func NewDogstatsdSender(addr string) (*DogstatsdSender, error) {
}, nil
}

func (sender *DogstatsdSender) SendEvent(ctx context.Context, event *Event) error {
func (sender *DogstatsdSender) SendEvent(ctx context.Context, event *Event) (string, error) {
if err := sender.client.Event(&statsd.Event{
Title: event.Title,
Text: event.Text,
Tags: event.Tags,
}); err != nil {
return fmt.Errorf("%w: %v", ErrDogstatsdSenderFailed, err)
return "", fmt.Errorf("%w: %v", ErrDogstatsdSenderFailed, err)
}

return nil
return "", nil
}

0 comments on commit 272a534

Please sign in to comment.