This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
forked from open-telemetry/opentelemetry-collector-contrib
/
sumo_marshaler.go
162 lines (138 loc) · 4.36 KB
/
sumo_marshaler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package awss3exporter // import "github.com/pelotoncycle/opentelemetry-collector-contrib/exporter/awss3exporter"
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)
const (
logBodyKey = "log"
)
type sumoMarshaler struct{}
func (*sumoMarshaler) format() string {
return string(SumoIC)
}
func newSumoICMarshaler() sumoMarshaler {
return sumoMarshaler{}
}
func logEntry(buf *bytes.Buffer, format string, a ...interface{}) {
buf.WriteString(fmt.Sprintf(format, a...))
buf.WriteString("\n")
}
func attributeValueToString(v pcommon.Value) (string, error) {
switch v.Type() {
case pcommon.ValueTypeStr:
return v.Str(), nil
case pcommon.ValueTypeBool:
return strconv.FormatBool(v.Bool()), nil
case pcommon.ValueTypeBytes:
return valueToJSON(v.Bytes().AsRaw())
case pcommon.ValueTypeDouble:
return strconv.FormatFloat(v.Double(), 'f', -1, 64), nil
case pcommon.ValueTypeInt:
return strconv.FormatInt(v.Int(), 10), nil
case pcommon.ValueTypeSlice:
return valueToJSON(v.Slice().AsRaw())
case pcommon.ValueTypeMap:
return valueToJSON(v.Map().AsRaw())
case pcommon.ValueTypeEmpty:
return "", nil
default:
return "", fmt.Errorf("unknown OpenTelemetry attribute value type: %q", v.Type())
}
}
func valueToJSON(m any) (string, error) {
jsonString := new(bytes.Buffer)
enc := json.NewEncoder(jsonString)
err := enc.Encode(m)
return strings.Trim(jsonString.String(), "\n"), err
}
const (
SourceCategoryKey = "_sourceCategory"
SourceHostKey = "_sourceHost"
SourceNameKey = "_sourceName"
)
func (sumoMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) {
buf := bytes.Buffer{}
rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
rl := rls.At(i)
ra := rl.Resource().Attributes()
sourceCategory, exists := ra.Get(SourceCategoryKey)
if !exists {
return nil, errors.New("_sourceCategory attribute does not exist")
}
sourceHost, exists := ra.Get(SourceHostKey)
if !exists {
return nil, errors.New("_sourceHost attribute does not exist")
}
sourceName, exists := ra.Get(SourceNameKey)
if !exists {
return nil, errors.New("_sourceName attribute does not exist")
}
sc, err := attributeValueToString(sourceCategory)
if err != nil {
return nil, err
}
sh, err := attributeValueToString(sourceHost)
if err != nil {
return nil, err
}
sn, err := attributeValueToString(sourceName)
if err != nil {
return nil, err
}
sc = strconv.Quote(sc)
sh = strconv.Quote(sh)
sn = strconv.Quote(sn)
// Remove the source attributes so that they won't be included in "fields" value.
ra.Remove(SourceCategoryKey)
ra.Remove(SourceHostKey)
ra.Remove(SourceNameKey)
fields, err := valueToJSON(ra.AsRaw())
if err != nil {
return nil, err
}
ills := rl.ScopeLogs()
for j := 0; j < ills.Len(); j++ {
ils := ills.At(j)
logs := ils.LogRecords()
for k := 0; k < logs.Len(); k++ {
lr := logs.At(k)
dateVal := lr.ObservedTimestamp()
message, err := getMessageJSON(lr)
if err != nil {
return nil, err
}
logEntry(&buf, "{\"date\": \"%s\",\"sourceName\":%s,\"sourceHost\":%s,\"sourceCategory\":%s,\"fields\":%s,\"message\":%s}",
dateVal, sn, sh, sc, fields, message)
}
}
}
return buf.Bytes(), nil
}
func getMessageJSON(lr plog.LogRecord) (string, error) {
// The "message" fields is a JSON created from combining the actual log body and log-level attributes,
// where the log body is stored under "log" key.
// More info:
// https://help.sumologic.com/docs/send-data/opentelemetry-collector/data-source-configurations/additional-configurations-reference/#mapping-opentelemetry-concepts-to-sumo-logic
message := new(bytes.Buffer)
enc := json.NewEncoder(message)
lr.Body().CopyTo(lr.Attributes().PutEmpty(logBodyKey))
err := enc.Encode(lr.Attributes().AsRaw())
return strings.Trim(message.String(), "\n"), err
}
func (s sumoMarshaler) MarshalTraces(_ ptrace.Traces) ([]byte, error) {
return nil, fmt.Errorf("traces can't be marshaled into %s format", s.format())
}
func (s sumoMarshaler) MarshalMetrics(_ pmetric.Metrics) ([]byte, error) {
return nil, fmt.Errorf("metrics can't be marshaled into %s format", s.format())
}