Skip to content

Commit

Permalink
cwd datadog: send logs instead of events when using --api-key (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
edigaryev authored Sep 21, 2023
1 parent 272a534 commit 021ddc5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
8 changes: 4 additions & 4 deletions internal/command/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ func NewCommand() *cobra.Command {
cmd.PersistentFlags().StringVar(&secretToken, "secret-token", "",
"if specified, this value will be used as a HMAC SHA-256 secret to verify the webhook events")
cmd.PersistentFlags().StringVar(&dogstatsdAddr, "dogstatsd-addr", "",
"enables sending events via the DogStatsD protocol to the specified address "+
"enables sending webhook events as Datadog events via the DogStatsD protocol to the specified address "+
"(for example, --dogstatsd-addr=127.0.0.1:8125)")
cmd.PersistentFlags().StringVar(&apiKey, "api-key", "",
"Enables sending events via the Datadog API using the specified API key")
"Enables sending webhook events as Datadog logs via the Datadog API using the specified API key")
cmd.PersistentFlags().StringVar(&apiSite, "api-site", "datadoghq.com",
"specifies the Datadog site to use when sending events via the Datadog API")
"specifies the Datadog site to use when sending webhook events as Datadog logs via the Datadog API")

return cmd
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func processWebhookEvent(
evt := &datadogsender.Event{
Title: "Webhook event",
Text: string(body),
Tags: []string{fmt.Sprintf("type:%s", presentedEventType)},
Tags: []string{fmt.Sprintf("webhook_event_type:%s", presentedEventType)},
}

// Enrich the event with tags
Expand Down
23 changes: 11 additions & 12 deletions internal/datadogsender/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"errors"
"fmt"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"strings"
)

var ErrAPISenderFailed = errors.New("API sender failed to send the event")

type APISender struct {
apiClient *datadog.APIClient
eventsAPI *datadogV1.EventsApi
logsAPI *datadogV2.LogsApi

apiKey string
apiSite string
Expand All @@ -23,7 +24,7 @@ func NewAPISender(apiKey string, apiSite string) (*APISender, error) {

return &APISender{
apiClient: apiClient,
eventsAPI: datadogV1.NewEventsApi(apiClient),
logsAPI: datadogV2.NewLogsApi(apiClient),

apiKey: apiKey,
apiSite: apiSite,
Expand All @@ -47,18 +48,16 @@ func (sender *APISender) SendEvent(ctx context.Context, event *Event) (string, e
"site": sender.apiSite,
})

response, _, err := sender.eventsAPI.CreateEvent(ctx, datadogV1.EventCreateRequest{
Title: event.Title,
Text: event.Text,
Tags: event.Tags,
_, _, err := sender.logsAPI.SubmitLog(ctx, []datadogV2.HTTPLogItem{
{
Ddsource: datadog.PtrString("Cirrus Webhooks Server"),
Ddtags: datadog.PtrString(strings.Join(event.Tags, ",")),
Message: event.Text,
},
})
if err != nil {
return "", fmt.Errorf("%w: %v", ErrAPISenderFailed, err)
}

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
return "", nil
}

0 comments on commit 021ddc5

Please sign in to comment.