/
marshal.go
122 lines (103 loc) · 3.67 KB
/
marshal.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
// Copyright observIQ, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chronicleforwarderexporter
import (
"context"
"encoding/json"
"fmt"
"github.com/observiq/bindplane-agent/expr"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/zap"
)
const logTypeField = `attributes["log_type"]`
type marshaler struct {
cfg Config
teleSettings component.TelemetrySettings
}
type logMarshaler interface {
MarshalRawLogs(ctx context.Context, ld plog.Logs) ([]string, error)
}
func newMarshaler(cfg Config, teleSettings component.TelemetrySettings) *marshaler {
return &marshaler{
cfg: cfg,
teleSettings: teleSettings,
}
}
func (m *marshaler) MarshalRawLogs(ctx context.Context, ld plog.Logs) ([]string, error) {
var rawLogs []string
for i := 0; i < ld.ResourceLogs().Len(); i++ {
resourceLog := ld.ResourceLogs().At(i)
for j := 0; j < resourceLog.ScopeLogs().Len(); j++ {
scopeLog := resourceLog.ScopeLogs().At(j)
for k := 0; k < scopeLog.LogRecords().Len(); k++ {
logRecord := scopeLog.LogRecords().At(k)
rawLog, err := m.getRawLog(ctx, logRecord, scopeLog.Scope(), resourceLog.Resource())
if err != nil {
m.teleSettings.Logger.Error("Error processing log record", zap.Error(err))
continue
}
if rawLog == "" {
m.teleSettings.Logger.Error("Error processing log record: raw log is empty")
continue
}
rawLogs = append(rawLogs, rawLog)
}
}
}
return rawLogs, nil
}
func (m *marshaler) getRawLog(ctx context.Context, logRecord plog.LogRecord, scope pcommon.InstrumentationScope, resource pcommon.Resource) (string, error) {
if m.cfg.RawLogField == "" {
entireLogRecord := map[string]any{
"body": logRecord.Body().AsRaw(),
"attributes": logRecord.Attributes().AsRaw(),
"resource_attributes": resource.Attributes().AsRaw(),
}
bytesLogRecord, err := json.Marshal(entireLogRecord)
if err != nil {
return "", fmt.Errorf("marshal log record: %w", err)
}
return string(bytesLogRecord), nil
}
return m.getRawField(ctx, m.cfg.RawLogField, logRecord, scope, resource)
}
func (m *marshaler) getRawField(ctx context.Context, field string, logRecord plog.LogRecord, scope pcommon.InstrumentationScope, resource pcommon.Resource) (string, error) {
lrExpr, err := expr.NewOTTLLogRecordExpression(field, m.teleSettings)
if err != nil {
return "", fmt.Errorf("raw_log_field is invalid: %s", err)
}
tCtx := ottllog.NewTransformContext(logRecord, scope, resource)
lrExprResult, err := lrExpr.Execute(ctx, tCtx)
if err != nil {
return "", fmt.Errorf("execute log record expression: %w", err)
}
if lrExprResult == nil {
return "", nil
}
switch result := lrExprResult.(type) {
case string:
return result, nil
case pcommon.Map:
bytes, err := json.Marshal(result.AsRaw())
if err != nil {
return "", fmt.Errorf("marshal log record expression result: %w", err)
}
return string(bytes), nil
default:
return "", fmt.Errorf("unsupported log record expression result type: %T", lrExprResult)
}
}