Skip to content

Commit

Permalink
Update event payload to send JSON object (#22)
Browse files Browse the repository at this point in the history
* update event payload to send JSON object

* normalize metadata

* flatten json payload
  • Loading branch information
smithclay authored Jun 15, 2024
1 parent bfb44c2 commit 72c5ef7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
42 changes: 22 additions & 20 deletions collector/components/servicenowexporter/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,31 @@ func (c *midClient) Close() {
c.httpClient.CloseIdleConnections()
}

func (c *midClient) sendEvents(payload []ServiceNowEvent) error {
func (c *midClient) sendEvents(events []ServiceNowEvent) error {
url := c.config.PushEventsURL
request := ServiceNowEventRequestBody{Records: payload}
c.logger.Info("Sending events to ServiceNow", zap.String("url", url), zap.Any("request", request))
body, err := json.Marshal(request)
if err != nil {
return err
}
r, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
return err
}
r.Header.Set("Content-Type", "application/json")
r.SetBasicAuth(c.config.Username, string(c.config.Password))

res, err := c.httpClient.Do(r)
if err != nil {
return err
}
defer res.Body.Close()
for e := range events {
c.logger.Info("Sending event to ServiceNow", zap.String("url", url), zap.Any("event", e))
body, err := json.Marshal(e)
if err != nil {
return err
}
r, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
if err != nil {
return err
}
r.Header.Set("Content-Type", "application/json")
r.SetBasicAuth(c.config.Username, string(c.config.Password))

if res.StatusCode != 200 {
return handleNon200Response(res)
res, err := c.httpClient.Do(r)
if err != nil {
return err
}
defer res.Body.Close()

if res.StatusCode != 200 {
handleNon200Response(res)
}
}

return nil
Expand Down
24 changes: 16 additions & 8 deletions collector/components/servicenowexporter/servicenow.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package servicenowexporter
import (
"bytes"
"context"
"encoding/json"
"strconv"
"strings"

Expand Down Expand Up @@ -203,7 +202,17 @@ func (e *serviceNowProducer) writeNumberDataPoints(metricName string, scope stri
func ci2metricAttrs(rAttrs pcommon.Map) map[string]string {
attrs := make(map[string]string)
rAttrs.Range(func(k string, v pcommon.Value) bool {
attrs[k] = v.AsString()
if v.Type() == pcommon.ValueTypeStr {
attrs[k] = v.AsString()
}
if v.Type() == pcommon.ValueTypeMap {
v.Map().Range(func(k2 string, v2 pcommon.Value) bool {
if v2.Type() == pcommon.ValueTypeStr {
attrs[k+"."+k2] = v2.AsString()
}
return true
})
}
return true
})
return attrs
Expand Down Expand Up @@ -348,6 +357,9 @@ func buildPath(name string, attributes pcommon.Map) string {

buf.WriteString(name)
attributes.Range(func(k string, v pcommon.Value) bool {
if v.Type() != pcommon.ValueTypeStr {
return true
}
value := v.AsString()
if value == "" {
value = tagValueEmptyPlaceholder
Expand All @@ -362,7 +374,7 @@ func buildPath(name string, attributes pcommon.Map) string {
return buf.String()
}

func formatAdditionalInfo(attrs map[string]string, resourceAttrs map[string]string) (string, error) {
func formatAdditionalInfo(attrs map[string]string, resourceAttrs map[string]string) (map[string]string, error) {
// merge attrs + resource attrs
newAttrs := make(map[string]string)
for k, v := range resourceAttrs {
Expand All @@ -383,11 +395,7 @@ func formatAdditionalInfo(attrs map[string]string, resourceAttrs map[string]stri
newAttrs[k] = v
}

bytes, err := json.Marshal(newAttrs)
if err != nil {
return "", err
}
return string(bytes), nil
return newAttrs, nil
}

func formatNode(resourceAttrs map[string]string) string {
Expand Down
8 changes: 2 additions & 6 deletions collector/components/servicenowexporter/servicenow_event.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package servicenowexporter

type ServiceNowEventRequestBody struct {
Records []ServiceNowEvent `json:"records"`
}

// https://docs.servicenow.com/bundle/vancouver-it-operations-management/page/product/event-management/task/send-events-via-web-service.html
type ServiceNowEvent struct {
// The resource on the node impacted
Expand All @@ -15,6 +11,6 @@ type ServiceNowEvent struct {
Description string `json:"description"`
Timestamp string `json:"time_of_event"` // yyyy-MM-dd HH:mm:ss
// k8s.cluster.name:test-cluster,k8s.cluster.uid=12345
AdditionalInfo string `json:"additional_info,omitempty"` // actually a json string
Source string `json:"source"`
AdditionalInfo map[string]string `json:"additional_info,omitempty"` // actually a json string
Source string `json:"source"`
}

0 comments on commit 72c5ef7

Please sign in to comment.