Skip to content

Commit 021ddc5

Browse files
authored
cwd datadog: send logs instead of events when using --api-key (#7)
1 parent 272a534 commit 021ddc5

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

internal/command/datadog/datadog.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ func NewCommand() *cobra.Command {
6969
cmd.PersistentFlags().StringVar(&secretToken, "secret-token", "",
7070
"if specified, this value will be used as a HMAC SHA-256 secret to verify the webhook events")
7171
cmd.PersistentFlags().StringVar(&dogstatsdAddr, "dogstatsd-addr", "",
72-
"enables sending events via the DogStatsD protocol to the specified address "+
72+
"enables sending webhook events as Datadog events via the DogStatsD protocol to the specified address "+
7373
"(for example, --dogstatsd-addr=127.0.0.1:8125)")
7474
cmd.PersistentFlags().StringVar(&apiKey, "api-key", "",
75-
"Enables sending events via the Datadog API using the specified API key")
75+
"Enables sending webhook events as Datadog logs via the Datadog API using the specified API key")
7676
cmd.PersistentFlags().StringVar(&apiSite, "api-site", "datadoghq.com",
77-
"specifies the Datadog site to use when sending events via the Datadog API")
77+
"specifies the Datadog site to use when sending webhook events as Datadog logs via the Datadog API")
7878

7979
return cmd
8080
}
@@ -177,7 +177,7 @@ func processWebhookEvent(
177177
evt := &datadogsender.Event{
178178
Title: "Webhook event",
179179
Text: string(body),
180-
Tags: []string{fmt.Sprintf("type:%s", presentedEventType)},
180+
Tags: []string{fmt.Sprintf("webhook_event_type:%s", presentedEventType)},
181181
}
182182

183183
// Enrich the event with tags

internal/datadogsender/api.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"errors"
66
"fmt"
77
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
8-
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
8+
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
9+
"strings"
910
)
1011

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

1314
type APISender struct {
1415
apiClient *datadog.APIClient
15-
eventsAPI *datadogV1.EventsApi
16+
logsAPI *datadogV2.LogsApi
1617

1718
apiKey string
1819
apiSite string
@@ -23,7 +24,7 @@ func NewAPISender(apiKey string, apiSite string) (*APISender, error) {
2324

2425
return &APISender{
2526
apiClient: apiClient,
26-
eventsAPI: datadogV1.NewEventsApi(apiClient),
27+
logsAPI: datadogV2.NewLogsApi(apiClient),
2728

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

50-
response, _, err := sender.eventsAPI.CreateEvent(ctx, datadogV1.EventCreateRequest{
51-
Title: event.Title,
52-
Text: event.Text,
53-
Tags: event.Tags,
51+
_, _, err := sender.logsAPI.SubmitLog(ctx, []datadogV2.HTTPLogItem{
52+
{
53+
Ddsource: datadog.PtrString("Cirrus Webhooks Server"),
54+
Ddtags: datadog.PtrString(strings.Join(event.Tags, ",")),
55+
Message: event.Text,
56+
},
5457
})
5558
if err != nil {
5659
return "", fmt.Errorf("%w: %v", ErrAPISenderFailed, err)
5760
}
5861

59-
if response.Event == nil {
60-
return "", fmt.Errorf("%w: %v", ErrAPISenderFailed, "response.Event is nil")
61-
}
62-
63-
return fmt.Sprintf("DataDog event id: %v", response.Event.Id), nil
62+
return "", nil
6463
}

0 commit comments

Comments
 (0)