Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[receiver/azureeventhub] support parsing more time format #36762

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/36650.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: azureeventhubreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: support more time format
hgaol marked this conversation as resolved.
Show resolved Hide resolved

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [36650]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
32 changes: 21 additions & 11 deletions pkg/translator/azure/resourcelogs_to_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"strconv"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/relvacode/iso8601"
Expand Down Expand Up @@ -71,8 +72,9 @@ type azureLogRecord struct {
var _ plog.Unmarshaler = (*ResourceLogsUnmarshaler)(nil)

type ResourceLogsUnmarshaler struct {
Version string
Logger *zap.Logger
Version string
Logger *zap.Logger
TimeFormat []string
hgaol marked this conversation as resolved.
Show resolved Hide resolved
}

func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
Expand Down Expand Up @@ -105,7 +107,7 @@ func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {

for i := 0; i < len(logs); i++ {
log := logs[i]
nanos, err := getTimestamp(log)
nanos, err := getTimestamp(log, r.TimeFormat)
if err != nil {
r.Logger.Warn("Unable to convert timestamp from log", zap.String("timestamp", log.Time))
continue
Expand All @@ -129,11 +131,11 @@ func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
return l, nil
}

func getTimestamp(record azureLogRecord) (pcommon.Timestamp, error) {
func getTimestamp(record azureLogRecord, format []string) (pcommon.Timestamp, error) {
hgaol marked this conversation as resolved.
Show resolved Hide resolved
if record.Time != "" {
return asTimestamp(record.Time)
return asTimestamp(record.Time, format)
} else if record.Timestamp != "" {
return asTimestamp(record.Timestamp)
return asTimestamp(record.Timestamp, format)
}

return 0, errMissingTimestamp
Expand All @@ -142,13 +144,21 @@ func getTimestamp(record azureLogRecord) (pcommon.Timestamp, error) {
// 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) (pcommon.Timestamp, error) {
t, err := iso8601.ParseString(s)
if err != nil {
return 0, err
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
}
}

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
Expand Down
33 changes: 25 additions & 8 deletions pkg/translator/azure/resourcelogs_to_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var testBuildInfo = component.BuildInfo{
var minimumLogRecord = func() plog.LogRecord {
lr := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z")
ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z", nil)
lr.SetTimestamp(ts)
lr.Attributes().PutStr(azureOperationName, "SecretGet")
lr.Attributes().PutStr(azureCategory, "AuditEvent")
Expand All @@ -38,7 +38,7 @@ var minimumLogRecord = func() plog.LogRecord {
var maximumLogRecord1 = func() plog.LogRecord {
lr := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z")
ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z", nil)
lr.SetTimestamp(ts)
lr.SetSeverityNumber(plog.SeverityNumberWarn)
lr.SetSeverityText("Warning")
Expand Down Expand Up @@ -72,7 +72,7 @@ var maximumLogRecord2 = func() []plog.LogRecord {
lr := sl.LogRecords().AppendEmpty()
lr2 := sl.LogRecords().AppendEmpty()

ts, _ := asTimestamp("2022-11-11T04:48:29.6767145Z")
ts, _ := asTimestamp("2022-11-11T04:48:29.6767145Z", nil)
lr.SetTimestamp(ts)
lr.SetSeverityNumber(plog.SeverityNumberWarn)
lr.SetSeverityText("Warning")
Expand All @@ -98,7 +98,7 @@ var maximumLogRecord2 = func() []plog.LogRecord {
m.PutDouble("float", 41.3)
m.PutBool("bool", true)

ts, _ = asTimestamp("2022-11-11T04:48:31.6767145Z")
ts, _ = asTimestamp("2022-11-11T04:48:31.6767145Z", nil)
lr2.SetTimestamp(ts)
lr2.SetSeverityNumber(plog.SeverityNumberWarn)
lr2.SetSeverityText("Warning")
Expand Down Expand Up @@ -131,7 +131,7 @@ var maximumLogRecord2 = func() []plog.LogRecord {
var badLevelLogRecord = func() plog.LogRecord {
lr := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

ts, _ := asTimestamp("2023-10-26T14:22:43.3416357Z")
ts, _ := asTimestamp("2023-10-26T14:22:43.3416357Z", nil)
lr.SetTimestamp(ts)
lr.SetSeverityNumber(plog.SeverityNumberTrace4)
lr.SetSeverityText("4")
Expand Down Expand Up @@ -171,7 +171,7 @@ var badLevelLogRecord = func() plog.LogRecord {
var badTimeLogRecord = func() plog.LogRecord {
lr := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

ts, _ := asTimestamp("2021-10-14T22:17:11+00:00")
ts, _ := asTimestamp("2021-10-14T22:17:11+00:00", nil)
lr.SetTimestamp(ts)

lr.Attributes().PutStr(azureOperationName, "ApplicationGatewayAccess")
Expand Down Expand Up @@ -213,12 +213,29 @@ var badTimeLogRecord = func() plog.LogRecord {

func TestAsTimestamp(t *testing.T) {
timestamp := "2022-11-11T04:48:27.6767145Z"
nanos, err := asTimestamp(timestamp)
nanos, err := asTimestamp(timestamp, nil)
assert.NoError(t, err)
assert.Less(t, pcommon.Timestamp(0), nanos)

timestamp = "11/20/2024 13:57:18"
nanos, err = asTimestamp(timestamp, []string{"01/02/2006 15:04:05"})
assert.NoError(t, err)
assert.Less(t, pcommon.Timestamp(0), nanos)

// time_format set, but fallback to iso8601 and succeeded to parse
timestamp = "2022-11-11T04:48:27.6767145Z"
nanos, err = asTimestamp(timestamp, []string{"01/02/2006 15:04:05"})
assert.NoError(t, err)
assert.Less(t, pcommon.Timestamp(0), nanos)

// time_format set, but all failed to parse
timestamp = "11/20/2024 13:57:18"
nanos, err = asTimestamp(timestamp, []string{"2006-01-02 15:04:05"})
assert.Error(t, err)
assert.Equal(t, pcommon.Timestamp(0), nanos)

timestamp = "invalid-time"
nanos, err = asTimestamp(timestamp)
nanos, err = asTimestamp(timestamp, nil)
assert.Error(t, err)
assert.Equal(t, pcommon.Timestamp(0), nanos)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/translator/azure/resources_to_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ type azureTracesRecord struct {
var _ ptrace.Unmarshaler = (*TracesUnmarshaler)(nil)

type TracesUnmarshaler struct {
Version string
Logger *zap.Logger
Version string
Logger *zap.Logger
TimeFormat []string
}

func (r TracesUnmarshaler) UnmarshalTraces(buf []byte) (ptrace.Traces, error) {
Expand Down Expand Up @@ -95,7 +96,7 @@ func (r TracesUnmarshaler) UnmarshalTraces(buf []byte) (ptrace.Traces, error) {

resource.Attributes().PutStr("service.name", azureTrace.AppRoleName)

nanos, err := asTimestamp(azureTrace.Time)
nanos, err := asTimestamp(azureTrace.Time, r.TimeFormat)
if err != nil {
r.Logger.Warn("Invalid Timestamp", zap.String("time", azureTrace.Time))
continue
Expand Down
33 changes: 21 additions & 12 deletions pkg/translator/azurelogs/resourcelogs_to_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"errors"
"strconv"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/relvacode/iso8601"
Expand Down Expand Up @@ -44,7 +45,6 @@ const (

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"`
Expand Down Expand Up @@ -76,8 +76,9 @@ type azureLogRecord struct {
var _ plog.Unmarshaler = (*ResourceLogsUnmarshaler)(nil)

type ResourceLogsUnmarshaler struct {
Version string
Logger *zap.Logger
Version string
Logger *zap.Logger
TimeFormat []string
}

func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
Expand Down Expand Up @@ -109,7 +110,7 @@ func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {

for i := 0; i < len(logs); i++ {
log := logs[i]
nanos, err := getTimestamp(log)
nanos, err := getTimestamp(log, r.TimeFormat)
if err != nil {
r.Logger.Warn("Unable to convert timestamp from log", zap.String("timestamp", log.Time))
continue
Expand Down Expand Up @@ -137,11 +138,11 @@ func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
return l, nil
}

func getTimestamp(record azureLogRecord) (pcommon.Timestamp, error) {
func getTimestamp(record azureLogRecord, formats []string) (pcommon.Timestamp, error) {
if record.Time != "" {
return asTimestamp(record.Time)
return asTimestamp(record.Time, formats)
} else if record.Timestamp != "" {
return asTimestamp(record.Timestamp)
return asTimestamp(record.Timestamp, formats)
}

return 0, errMissingTimestamp
Expand All @@ -150,13 +151,21 @@ func getTimestamp(record azureLogRecord) (pcommon.Timestamp, error) {
// 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) (pcommon.Timestamp, error) {
t, err := iso8601.ParseString(s)
if err != nil {
return 0, err
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
}
}

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
Expand Down
33 changes: 25 additions & 8 deletions pkg/translator/azurelogs/resourcelogs_to_logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var testBuildInfo = component.BuildInfo{
var minimumLogRecord = func() plog.LogRecord {
lr := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z")
ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z", nil)
lr.SetTimestamp(ts)
lr.Attributes().PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure)
lr.Attributes().PutStr(conventions.AttributeCloudResourceID, "/RESOURCE_ID")
Expand All @@ -44,7 +44,7 @@ var minimumLogRecord = func() plog.LogRecord {
var maximumLogRecord1 = func() plog.LogRecord {
lr := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z")
ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z", nil)
lr.SetTimestamp(ts)
lr.SetSeverityNumber(plog.SeverityNumberWarn)
lr.SetSeverityText("Warning")
Expand Down Expand Up @@ -82,7 +82,7 @@ var maximumLogRecord2 = func() []plog.LogRecord {
lr := sl.LogRecords().AppendEmpty()
lr2 := sl.LogRecords().AppendEmpty()

ts, _ := asTimestamp("2022-11-11T04:48:29.6767145Z")
ts, _ := asTimestamp("2022-11-11T04:48:29.6767145Z", nil)
lr.SetTimestamp(ts)
lr.SetSeverityNumber(plog.SeverityNumberWarn)
lr.SetSeverityText("Warning")
Expand Down Expand Up @@ -112,7 +112,7 @@ var maximumLogRecord2 = func() []plog.LogRecord {
properties.PutDouble("float", 41.3)
properties.PutBool("bool", true)

ts, _ = asTimestamp("2022-11-11T04:48:31.6767145Z")
ts, _ = asTimestamp("2022-11-11T04:48:31.6767145Z", nil)
lr2.SetTimestamp(ts)
lr2.SetSeverityNumber(plog.SeverityNumberWarn)
lr2.SetSeverityText("Warning")
Expand Down Expand Up @@ -149,7 +149,7 @@ var maximumLogRecord2 = func() []plog.LogRecord {
var badLevelLogRecord = func() plog.LogRecord {
lr := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

ts, _ := asTimestamp("2023-10-26T14:22:43.3416357Z")
ts, _ := asTimestamp("2023-10-26T14:22:43.3416357Z", nil)
lr.SetTimestamp(ts)
lr.SetSeverityNumber(plog.SeverityNumberTrace4)
lr.SetSeverityText("4")
Expand Down Expand Up @@ -193,7 +193,7 @@ var badLevelLogRecord = func() plog.LogRecord {
var badTimeLogRecord = func() plog.LogRecord {
lr := plog.NewLogs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()

ts, _ := asTimestamp("2021-10-14T22:17:11+00:00")
ts, _ := asTimestamp("2021-10-14T22:17:11+00:00", nil)
lr.SetTimestamp(ts)

lr.Attributes().PutStr(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderAzure)
Expand Down Expand Up @@ -239,12 +239,29 @@ var badTimeLogRecord = func() plog.LogRecord {

func TestAsTimestamp(t *testing.T) {
timestamp := "2022-11-11T04:48:27.6767145Z"
nanos, err := asTimestamp(timestamp)
nanos, err := asTimestamp(timestamp, nil)
assert.NoError(t, err)
assert.Less(t, pcommon.Timestamp(0), nanos)

timestamp = "11/20/2024 13:57:18"
nanos, err = asTimestamp(timestamp, []string{"01/02/2006 15:04:05"})
assert.NoError(t, err)
assert.Less(t, pcommon.Timestamp(0), nanos)

// time_format set, but fallback to iso8601 and succeeded to parse
timestamp = "2022-11-11T04:48:27.6767145Z"
nanos, err = asTimestamp(timestamp, []string{"01/02/2006 15:04:05"})
assert.NoError(t, err)
assert.Less(t, pcommon.Timestamp(0), nanos)

// time_format set, but all failed to parse
timestamp = "11/20/2024 13:57:18"
nanos, err = asTimestamp(timestamp, []string{"2006-01-02 15:04:05"})
assert.Error(t, err)
assert.Equal(t, pcommon.Timestamp(0), nanos)

timestamp = "invalid-time"
nanos, err = asTimestamp(timestamp)
nanos, err = asTimestamp(timestamp, nil)
assert.Error(t, err)
assert.Equal(t, pcommon.Timestamp(0), nanos)
}
atoulme marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading
Loading