-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathresourcelogs_to_logs.go
222 lines (192 loc) · 6.86 KB
/
resourcelogs_to_logs.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package azure // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/azure"
import (
"bytes"
"encoding/json"
"errors"
"strconv"
"time"
jsoniter "github.com/json-iterator/go"
"github.com/relvacode/iso8601"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
conventions "go.opentelemetry.io/collector/semconv/v1.13.0"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)
const (
// Constants for OpenTelemetry Specs
scopeName = "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/azure"
// Constants for Azure Log Records
azureCategory = "azure.category"
azureCorrelationID = "azure.correlation.id"
azureDuration = "azure.duration"
azureIdentity = "azure.identity"
azureOperationName = "azure.operation.name"
azureOperationVersion = "azure.operation.version"
azureProperties = "azure.properties"
azureResourceID = "azure.resource.id"
azureResultType = "azure.result.type"
azureResultSignature = "azure.result.signature"
azureResultDescription = "azure.result.description"
azureTenantID = "azure.tenant.id"
)
var errMissingTimestamp = errors.New("missing timestamp")
// azureRecords represents an array of Azure log records
// as exported via an Azure Event Hub
type azureRecords struct {
Records []azureLogRecord `json:"records"`
}
// azureLogRecord represents a single Azure log following
// the common schema:
// https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/resource-logs-schema
type azureLogRecord struct {
Time string `json:"time"`
Timestamp string `json:"timeStamp"`
ResourceID string `json:"resourceId"`
TenantID *string `json:"tenantId"`
OperationName string `json:"operationName"`
OperationVersion *string `json:"operationVersion"`
Category string `json:"category"`
ResultType *string `json:"resultType"`
ResultSignature *string `json:"resultSignature"`
ResultDescription *string `json:"resultDescription"`
DurationMs *json.Number `json:"durationMs"`
CallerIPAddress *string `json:"callerIpAddress"`
CorrelationID *string `json:"correlationId"`
Identity *any `json:"identity"`
Level *json.Number `json:"Level"`
Location *string `json:"location"`
Properties *any `json:"properties"`
}
var _ plog.Unmarshaler = (*ResourceLogsUnmarshaler)(nil)
type ResourceLogsUnmarshaler struct {
Version string
Logger *zap.Logger
TimeFormats []string
}
func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
l := plog.NewLogs()
var azureLogs azureRecords
decoder := jsoniter.NewDecoder(bytes.NewReader(buf))
if err := decoder.Decode(&azureLogs); err != nil {
return l, err
}
var resourceIDs []string
azureResourceLogs := make(map[string][]azureLogRecord)
for _, azureLog := range azureLogs.Records {
azureResourceLogs[azureLog.ResourceID] = append(azureResourceLogs[azureLog.ResourceID], azureLog)
keyExists := slices.Contains(resourceIDs, azureLog.ResourceID)
if !keyExists {
resourceIDs = append(resourceIDs, azureLog.ResourceID)
}
}
for _, resourceID := range resourceIDs {
logs := azureResourceLogs[resourceID]
resourceLogs := l.ResourceLogs().AppendEmpty()
resourceLogs.Resource().Attributes().PutStr(azureResourceID, resourceID)
scopeLogs := resourceLogs.ScopeLogs().AppendEmpty()
scopeLogs.Scope().SetName(scopeName)
scopeLogs.Scope().SetVersion(r.Version)
logRecords := scopeLogs.LogRecords()
for i := 0; i < len(logs); i++ {
log := logs[i]
nanos, err := getTimestamp(log, r.TimeFormats...)
if err != nil {
r.Logger.Warn("Unable to convert timestamp from log", zap.String("timestamp", log.Time))
continue
}
lr := logRecords.AppendEmpty()
lr.SetTimestamp(nanos)
if log.Level != nil {
severity := asSeverity(*log.Level)
lr.SetSeverityNumber(severity)
lr.SetSeverityText(log.Level.String())
}
if err := lr.Attributes().FromRaw(extractRawAttributes(log)); err != nil {
return l, err
}
}
}
return l, nil
}
func getTimestamp(record azureLogRecord, formats ...string) (pcommon.Timestamp, error) {
if record.Time != "" {
return asTimestamp(record.Time, formats...)
} else if record.Timestamp != "" {
return asTimestamp(record.Timestamp, formats...)
}
return 0, errMissingTimestamp
}
// asTimestamp will parse an ISO8601 string into an OpenTelemetry
// nanosecond timestamp. If the string cannot be parsed, it will
// return zero and the error.
func asTimestamp(s string, formats ...string) (pcommon.Timestamp, error) {
var err error
var t time.Time
// Try parsing with provided formats first
for _, format := range formats {
if t, err = time.Parse(format, s); err == nil {
return pcommon.Timestamp(t.UnixNano()), nil
}
}
// Fallback to ISO 8601 parsing if no format matches
if t, err = iso8601.ParseString(s); err == nil {
return pcommon.Timestamp(t.UnixNano()), nil
}
return 0, err
}
// asSeverity converts the Azure log level to equivalent
// OpenTelemetry severity numbers. If the log level is not
// valid, then the 'Unspecified' value is returned.
func asSeverity(number json.Number) plog.SeverityNumber {
switch number.String() {
case "Informational":
return plog.SeverityNumberInfo
case "Warning":
return plog.SeverityNumberWarn
case "Error":
return plog.SeverityNumberError
case "Critical":
return plog.SeverityNumberFatal
default:
levelNumber, _ := number.Int64()
if levelNumber > 0 {
return plog.SeverityNumber(levelNumber)
}
return plog.SeverityNumberUnspecified
}
}
func extractRawAttributes(log azureLogRecord) map[string]any {
attrs := map[string]any{}
attrs[azureCategory] = log.Category
setIf(attrs, azureCorrelationID, log.CorrelationID)
if log.DurationMs != nil {
duration, err := strconv.ParseInt(log.DurationMs.String(), 10, 64)
if err == nil {
attrs[azureDuration] = duration
}
}
if log.Identity != nil {
attrs[azureIdentity] = *log.Identity
}
attrs[azureOperationName] = log.OperationName
setIf(attrs, azureOperationVersion, log.OperationVersion)
if log.Properties != nil {
attrs[azureProperties] = *log.Properties
}
setIf(attrs, azureResultDescription, log.ResultDescription)
setIf(attrs, azureResultSignature, log.ResultSignature)
setIf(attrs, azureResultType, log.ResultType)
setIf(attrs, azureTenantID, log.TenantID)
setIf(attrs, conventions.AttributeCloudRegion, log.Location)
attrs[conventions.AttributeCloudProvider] = conventions.AttributeCloudProviderAzure
setIf(attrs, conventions.AttributeNetSockPeerAddr, log.CallerIPAddress)
return attrs
}
func setIf(attrs map[string]any, key string, value *string) {
if value != nil && *value != "" {
attrs[key] = *value
}
}