Search code examples
jsongostructdatadog

Send Event to Datadog


Unfortunately there is not an official Go Datadog API. I am currently using this one instead https://github.com/zorkian/go-datadog-api. Datadog forked the first version of it and recommend using it.

I am able to connect to my Dashboard:

    client := datadog.NewClient("...", "...")

    dash, err := client.GetDashboard(...)
    if err != nil {
      log.Fatalf("fatal: %s\n", err)
    }

But I do not know how to send create/track an event. This is my current approach but if fails badly.

    c := datadog.Client{}
    title := "Abc"
    e := datadog.Event{ Title: &title }
    c.PostEvent(&e)

From my understanding and the missing documentation, I would have to fill out some of these variables in this struct (https://github.com/zorkian/go-datadog-api/blob/master/events.go)

// Event is a single event.
// all fields will be filled out.
type Event struct {
  Id          *int     `json:"id,omitempty"`
  Title       *string  `json:"title,omitempty"`
  Text        *string  `json:"text,omitempty"`
  Time        *int     `json:"date_happened,omitempty"` // UNIX time.
  Priority    *string  `json:"priority,omitempty"`
  AlertType   *string  `json:"alert_type,omitempty"`
  Host        *string  `json:"host,omitempty"`
  Aggregation *string  `json:"aggregation_key,omitempty"`
  SourceType  *string  `json:"source_type_name,omitempty"`
  Tags        []string `json:"tags,omitempty"`
  Url         *string  `json:"url,omitempty"`
  Resource    *string  `json:"resource,omitempty"`
  EventType   *string  `json:"event_type,omitempty"`
}

Can you please help me with that?


Solution

  • In the code you posted:

    c := datadog.Client{}
    

    This seems to be creating an empty client object.

    Shouldn't you be creating a client with your keys using datadog.NewClient("...", "...") as in the first code snippet you posted?

    c := datadog.NewClient("...", "...")
    

    Also, you should check the error returned as that will give you more hints to troubleshoot the issue:

    _, err := c.PostEvent(&e)
    if err != nil {
      log.Fatalf("fatal: %s\n", err)
    }
    

    `