Skip to content

Commit

Permalink
fix nit issues and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
hgaol committed Dec 11, 2024
1 parent 6176537 commit d3f6bd1
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .chloggen/36650.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ change_type: enhancement
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
note: support providing one or more time formats for timestamp parsing

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [36650]
Expand Down
16 changes: 8 additions & 8 deletions pkg/translator/azure/resourcelogs_to_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ type azureLogRecord struct {
var _ plog.Unmarshaler = (*ResourceLogsUnmarshaler)(nil)

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

func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
Expand Down Expand Up @@ -107,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, r.TimeFormat)
nanos, err := getTimestamp(log, r.TimeFormats...)
if err != nil {
r.Logger.Warn("Unable to convert timestamp from log", zap.String("timestamp", log.Time))
continue
Expand All @@ -131,11 +131,11 @@ func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
return l, nil
}

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

return 0, errMissingTimestamp
Expand All @@ -144,7 +144,7 @@ func getTimestamp(record azureLogRecord, format []string) (pcommon.Timestamp, er
// 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) {
func asTimestamp(s string, formats ...string) (pcommon.Timestamp, error) {
var err error
var t time.Time
// Try parsing with provided formats first
Expand Down
22 changes: 11 additions & 11 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", nil)
ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z")
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", nil)
ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z")
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", nil)
ts, _ := asTimestamp("2022-11-11T04:48:29.6767145Z")
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", nil)
ts, _ = asTimestamp("2022-11-11T04:48:31.6767145Z")
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", nil)
ts, _ := asTimestamp("2023-10-26T14:22:43.3416357Z")
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", nil)
ts, _ := asTimestamp("2021-10-14T22:17:11+00:00")
lr.SetTimestamp(ts)

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

func TestAsTimestamp(t *testing.T) {
timestamp := "2022-11-11T04:48:27.6767145Z"
nanos, err := asTimestamp(timestamp, nil)
nanos, err := asTimestamp(timestamp)
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"})
nanos, err = asTimestamp(timestamp, "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"})
nanos, err = asTimestamp(timestamp, "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"})
nanos, err = asTimestamp(timestamp, "2006-01-02 15:04:05")
assert.Error(t, err)
assert.Equal(t, pcommon.Timestamp(0), nanos)

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

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

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

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

nanos, err := asTimestamp(azureTrace.Time, r.TimeFormat)
nanos, err := asTimestamp(azureTrace.Time, r.TimeFormats...)
if err != nil {
r.Logger.Warn("Invalid Timestamp", zap.String("time", azureTrace.Time))
continue
Expand Down
16 changes: 8 additions & 8 deletions pkg/translator/azurelogs/resourcelogs_to_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ type azureLogRecord struct {
var _ plog.Unmarshaler = (*ResourceLogsUnmarshaler)(nil)

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

func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
Expand Down Expand Up @@ -110,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, r.TimeFormat)
nanos, err := getTimestamp(log, r.TimeFormats...)
if err != nil {
r.Logger.Warn("Unable to convert timestamp from log", zap.String("timestamp", log.Time))
continue
Expand Down Expand Up @@ -138,11 +138,11 @@ func (r ResourceLogsUnmarshaler) UnmarshalLogs(buf []byte) (plog.Logs, error) {
return l, nil
}

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

return 0, errMissingTimestamp
Expand All @@ -151,7 +151,7 @@ func getTimestamp(record azureLogRecord, formats []string) (pcommon.Timestamp, e
// 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) {
func asTimestamp(s string, formats ...string) (pcommon.Timestamp, error) {
var err error
var t time.Time
// Try parsing with provided formats first
Expand Down
22 changes: 11 additions & 11 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", nil)
ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z")
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", nil)
ts, _ := asTimestamp("2022-11-11T04:48:27.6767145Z")
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", nil)
ts, _ := asTimestamp("2022-11-11T04:48:29.6767145Z")
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", nil)
ts, _ = asTimestamp("2022-11-11T04:48:31.6767145Z")
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", nil)
ts, _ := asTimestamp("2023-10-26T14:22:43.3416357Z")
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", nil)
ts, _ := asTimestamp("2021-10-14T22:17:11+00:00")
lr.SetTimestamp(ts)

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

func TestAsTimestamp(t *testing.T) {
timestamp := "2022-11-11T04:48:27.6767145Z"
nanos, err := asTimestamp(timestamp, nil)
nanos, err := asTimestamp(timestamp)
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"})
nanos, err = asTimestamp(timestamp, "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"})
nanos, err = asTimestamp(timestamp, "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"})
nanos, err = asTimestamp(timestamp, "2006-01-02 15:04:05")
assert.Error(t, err)
assert.Equal(t, pcommon.Timestamp(0), nanos)

timestamp = "invalid-time"
nanos, err = asTimestamp(timestamp, nil)
nanos, err = asTimestamp(timestamp)
assert.Error(t, err)
assert.Equal(t, pcommon.Timestamp(0), nanos)
}
Expand Down
10 changes: 2 additions & 8 deletions receiver/azureeventhubreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,12 @@ attribute names are copied without any changes.

Default: `false` (semantic conventions are not applied)

### time_format (optional)
### time_formats (optional)

All supported time format for logs, metrics and traces. Default is `nil` (unset), which means using the current iso8601 parser. The format is based on https://pkg.go.dev/time#Layout. If no time-zone info, will use UTC time. If all failed, it will use iso8601 format to parse.

Default: `nil`

### time_offset (optional)

The offset hours to parsed time. Mainly for cases when there's no time-zone info in time string.

Default: `0`.

### Example Configuration

```yaml
Expand All @@ -73,7 +67,7 @@ receivers:
offset: "1234-5566"
format: "azure"
# optional
time_format:
time_formats:
# All supported time format. Default is empty string array, which means using the current iso8601 parser. The format is based on https://pkg.go.dev/time#Layout. If no time-zone info, will use UTC time.
logs: ["01/02/2006 15:04:05","2006-01-02 15:04:05","2006-01-02T15:04:05Z07:00"]
metrics: ["01/02/2006 15:04:05"]
Expand Down
12 changes: 6 additions & 6 deletions receiver/azureeventhubreceiver/azureresourcelogs_unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ func newAzureResourceLogsUnmarshaler(buildInfo component.BuildInfo, logger *zap.
if applySemanticConventions {
return AzureResourceLogsEventUnmarshaler{
unmarshaler: &azurelogs.ResourceLogsUnmarshaler{
Version: buildInfo.Version,
Logger: logger,
TimeFormat: timeFormat,
Version: buildInfo.Version,
Logger: logger,
TimeFormats: timeFormat,
},
}
}
return AzureResourceLogsEventUnmarshaler{
unmarshaler: &azure.ResourceLogsUnmarshaler{
Version: buildInfo.Version,
Logger: logger,
TimeFormat: timeFormat,
Version: buildInfo.Version,
Logger: logger,
TimeFormats: timeFormat,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type azureTracesEventUnmarshaler struct {
func newAzureTracesUnmarshaler(buildInfo component.BuildInfo, logger *zap.Logger, timeFormat []string) eventTracesUnmarshaler {
return azureTracesEventUnmarshaler{
unmarshaler: &azure.TracesUnmarshaler{
Version: buildInfo.Version,
Logger: logger,
TimeFormat: timeFormat,
Version: buildInfo.Version,
Logger: logger,
TimeFormats: timeFormat,
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion receiver/azureeventhubreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Config struct {
Format string `mapstructure:"format"`
ConsumerGroup string `mapstructure:"group"`
ApplySemanticConventions bool `mapstructure:"apply_semantic_conventions"`
TimeFormat TimeFormat `mapstructure:"time_format"`
TimeFormats TimeFormat `mapstructure:"time_formats"`
}

type TimeFormat struct {
Expand Down
Loading

0 comments on commit d3f6bd1

Please sign in to comment.