Skip to content

Commit 72c5ef7

Browse files
authored
Update event payload to send JSON object (#22)
* update event payload to send JSON object * normalize metadata * flatten json payload
1 parent bfb44c2 commit 72c5ef7

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

collector/components/servicenowexporter/client.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,31 @@ func (c *midClient) Close() {
4545
c.httpClient.CloseIdleConnections()
4646
}
4747

48-
func (c *midClient) sendEvents(payload []ServiceNowEvent) error {
48+
func (c *midClient) sendEvents(events []ServiceNowEvent) error {
4949
url := c.config.PushEventsURL
50-
request := ServiceNowEventRequestBody{Records: payload}
51-
c.logger.Info("Sending events to ServiceNow", zap.String("url", url), zap.Any("request", request))
52-
body, err := json.Marshal(request)
53-
if err != nil {
54-
return err
55-
}
56-
r, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
57-
if err != nil {
58-
return err
59-
}
60-
r.Header.Set("Content-Type", "application/json")
61-
r.SetBasicAuth(c.config.Username, string(c.config.Password))
6250

63-
res, err := c.httpClient.Do(r)
64-
if err != nil {
65-
return err
66-
}
67-
defer res.Body.Close()
51+
for e := range events {
52+
c.logger.Info("Sending event to ServiceNow", zap.String("url", url), zap.Any("event", e))
53+
body, err := json.Marshal(e)
54+
if err != nil {
55+
return err
56+
}
57+
r, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
58+
if err != nil {
59+
return err
60+
}
61+
r.Header.Set("Content-Type", "application/json")
62+
r.SetBasicAuth(c.config.Username, string(c.config.Password))
6863

69-
if res.StatusCode != 200 {
70-
return handleNon200Response(res)
64+
res, err := c.httpClient.Do(r)
65+
if err != nil {
66+
return err
67+
}
68+
defer res.Body.Close()
69+
70+
if res.StatusCode != 200 {
71+
handleNon200Response(res)
72+
}
7173
}
7274

7375
return nil

collector/components/servicenowexporter/servicenow.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package servicenowexporter
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"strconv"
87
"strings"
98

@@ -203,7 +202,17 @@ func (e *serviceNowProducer) writeNumberDataPoints(metricName string, scope stri
203202
func ci2metricAttrs(rAttrs pcommon.Map) map[string]string {
204203
attrs := make(map[string]string)
205204
rAttrs.Range(func(k string, v pcommon.Value) bool {
206-
attrs[k] = v.AsString()
205+
if v.Type() == pcommon.ValueTypeStr {
206+
attrs[k] = v.AsString()
207+
}
208+
if v.Type() == pcommon.ValueTypeMap {
209+
v.Map().Range(func(k2 string, v2 pcommon.Value) bool {
210+
if v2.Type() == pcommon.ValueTypeStr {
211+
attrs[k+"."+k2] = v2.AsString()
212+
}
213+
return true
214+
})
215+
}
207216
return true
208217
})
209218
return attrs
@@ -348,6 +357,9 @@ func buildPath(name string, attributes pcommon.Map) string {
348357

349358
buf.WriteString(name)
350359
attributes.Range(func(k string, v pcommon.Value) bool {
360+
if v.Type() != pcommon.ValueTypeStr {
361+
return true
362+
}
351363
value := v.AsString()
352364
if value == "" {
353365
value = tagValueEmptyPlaceholder
@@ -362,7 +374,7 @@ func buildPath(name string, attributes pcommon.Map) string {
362374
return buf.String()
363375
}
364376

365-
func formatAdditionalInfo(attrs map[string]string, resourceAttrs map[string]string) (string, error) {
377+
func formatAdditionalInfo(attrs map[string]string, resourceAttrs map[string]string) (map[string]string, error) {
366378
// merge attrs + resource attrs
367379
newAttrs := make(map[string]string)
368380
for k, v := range resourceAttrs {
@@ -383,11 +395,7 @@ func formatAdditionalInfo(attrs map[string]string, resourceAttrs map[string]stri
383395
newAttrs[k] = v
384396
}
385397

386-
bytes, err := json.Marshal(newAttrs)
387-
if err != nil {
388-
return "", err
389-
}
390-
return string(bytes), nil
398+
return newAttrs, nil
391399
}
392400

393401
func formatNode(resourceAttrs map[string]string) string {
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package servicenowexporter
22

3-
type ServiceNowEventRequestBody struct {
4-
Records []ServiceNowEvent `json:"records"`
5-
}
6-
73
// https://docs.servicenow.com/bundle/vancouver-it-operations-management/page/product/event-management/task/send-events-via-web-service.html
84
type ServiceNowEvent struct {
95
// The resource on the node impacted
@@ -15,6 +11,6 @@ type ServiceNowEvent struct {
1511
Description string `json:"description"`
1612
Timestamp string `json:"time_of_event"` // yyyy-MM-dd HH:mm:ss
1713
// k8s.cluster.name:test-cluster,k8s.cluster.uid=12345
18-
AdditionalInfo string `json:"additional_info,omitempty"` // actually a json string
19-
Source string `json:"source"`
14+
AdditionalInfo map[string]string `json:"additional_info,omitempty"` // actually a json string
15+
Source string `json:"source"`
2016
}

0 commit comments

Comments
 (0)