Skip to content
Open
Show file tree
Hide file tree
Changes from all 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/feat_azureencoding-logs-general.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. receiver/filelog)
component: extension/azure_encoding

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Implement general Azure Resource Log parsing functionality

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

# (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: []
2 changes: 2 additions & 0 deletions extension/encoding/azureencodingextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ Currently supported following Azure Resource Logs export formats:

Currently only subset of available Azure Resource Logs Categories properly translated using OpenTelemetry SemConv.

[Transformation rules from Azure Resource Logs fields to OpenTelemetry](./internal/unmarshaler/logs/README.md).

Unsupported Categories simply copies attributes from "properties" field of incoming Azure Log Record to OpenTelemetry Log Attributes as a strings.

***time_formats (Optional)***
Expand Down
7 changes: 4 additions & 3 deletions extension/encoding/azureencodingextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ var (
)

type azureExtension struct {
config *Config
config *Config
logUnmarshaler plog.Unmarshaler
}

func (*azureExtension) UnmarshalTraces(_ []byte) (ptrace.Traces, error) {
return ptrace.Traces{}, errors.New("not implemented yet")
}

func (*azureExtension) UnmarshalLogs(_ []byte) (plog.Logs, error) {
return plog.Logs{}, errors.New("not implemented yet")
func (ex *azureExtension) UnmarshalLogs(buf []byte) (plog.Logs, error) {
return ex.logUnmarshaler.UnmarshalLogs(buf)
}

func (*azureExtension) UnmarshalMetrics(_ []byte) (pmetric.Metrics, error) {
Expand Down
9 changes: 8 additions & 1 deletion extension/encoding/azureencodingextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/azureencodingextension/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/azureencodingextension/internal/unmarshaler"
"github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/azureencodingextension/internal/unmarshaler/logs"
)

func NewFactory() extension.Factory {
Expand All @@ -22,11 +23,17 @@ func NewFactory() extension.Factory {
)
}

func createExtension(_ context.Context, _ extension.Settings, cfg component.Config) (extension.Extension, error) {
func createExtension(_ context.Context, settings extension.Settings, cfg component.Config) (extension.Extension, error) {
config := cfg.(*Config)

return &azureExtension{
config: config,
logUnmarshaler: logs.NewAzureResourceLogsUnmarshaler(
settings.BuildInfo,
settings.Logger,
config.Format,
config.Logs,
),
}, nil
}

Expand Down
18 changes: 15 additions & 3 deletions extension/encoding/azureencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/extension/encod
go 1.24.0

require (
github.com/goccy/go-json v0.10.5
github.com/json-iterator/go v1.1.12
github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding v0.140.1
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden v0.140.1
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.140.1
github.com/relvacode/iso8601 v1.7.0
github.com/stretchr/testify v1.11.1
go.opentelemetry.io/collector/component v1.46.1-0.20251120204106-2e9c82787618
go.opentelemetry.io/collector/component/componenttest v0.140.1-0.20251120204106-2e9c82787618
Expand All @@ -12,39 +17,46 @@ require (
go.opentelemetry.io/collector/extension v1.46.1-0.20251120204106-2e9c82787618
go.opentelemetry.io/collector/extension/extensiontest v0.140.1-0.20251120204106-2e9c82787618
go.opentelemetry.io/collector/pdata v1.46.1-0.20251120204106-2e9c82787618
go.opentelemetry.io/otel v1.38.0
go.uber.org/goleak v1.3.0
go.uber.org/zap v1.27.0
)

require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf/maps v0.1.2 // indirect
github.com/knadh/koanf/providers/confmap v1.0.0 // indirect
github.com/knadh/koanf/v2 v2.3.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.140.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/collector/featuregate v1.46.1-0.20251120204106-2e9c82787618 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.140.1-0.20251120204106-2e9c82787618 // indirect
go.opentelemetry.io/otel v1.38.0 // indirect
go.opentelemetry.io/otel/metric v1.38.0 // indirect
go.opentelemetry.io/otel/sdk v1.38.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.38.0 // indirect
go.opentelemetry.io/otel/trace v1.38.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/sys v0.37.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding => ../

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../../pkg/pdatautil

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden => ../../../pkg/golden
6 changes: 6 additions & 0 deletions extension/encoding/azureencodingextension/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,106 @@

package unmarshaler // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding/azureencodingextension/internal/unmarshaler"

import (
"encoding/json"
"fmt"
"time"

"github.com/relvacode/iso8601"
"go.opentelemetry.io/collector/pdata/pcommon"
)

type RecordsBatchFormat string

// Supported wrapper formats of Azure Logs Records batch
const (
FormatEventHub RecordsBatchFormat = "eventhub"
FormatBlobStorage RecordsBatchFormat = "blobstorage"
)

// JSON Path expressions that matches specific wrapper format
const (
// As exported to Azure Event Hub, e.g. `{"records": [ {...}, {...} ]}`
JSONPathEventHubLogRecords = "$.records[*]"
// As exported to Azure Blob Storage, e.g. `[ {...}, {...} ]`
JSONPathBlobStorageLogRecords = "$[*]"
)

// Commonly used attributes non-SemConv attributes across all telemetry signals
const (
AttributeAzureCategory = "azure.category"
AttributeAzureOperationName = "azure.operation.name"
)

const originalSuffix = ".original"

// AsTimestamp tries to parse a string with timestamp into OpenTelemetry
// using provided list of formats layouts.
// If not formats provided or parsing using them failed - will use an ISO8601 parser.
// 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
}

// AttrPutStrIf is a helper function to set a string attribute
// only if the value is not empty
func AttrPutStrIf(attrs pcommon.Map, attrKey, attrValue string) {
if attrValue != "" {
attrs.PutStr(attrKey, attrValue)
}
}

// AttrPutStrPtrIf is a helper function to set a string attribute
// only if the value exists and is not empty
func AttrPutStrPtrIf(attrs pcommon.Map, attrKey string, attrValue *string) {
if attrValue != nil && *attrValue != "" {
attrs.PutStr(attrKey, *attrValue)
}
}

// AttrPutIntNumberIf is a helper function to set an int64 attribute with defined key,
// trying to parse it from json.Number value
// If parsing failed - no attribute will be set
func AttrPutIntNumberIf(attrs pcommon.Map, attrKey string, attrValue json.Number) {
if i, err := attrValue.Int64(); err == nil {
attrs.PutInt(attrKey, i)
}
}

// AttrPutIntNumberPtrIf is a same function as AttrPutIntNumberIf but
// accepts a pointer to json.Number instead of value
func AttrPutIntNumberPtrIf(attrs pcommon.Map, attrKey string, attrValue *json.Number) {
if attrValue != nil {
AttrPutIntNumberIf(attrs, attrKey, *attrValue)
}
}

// attrPutMap is a helper function to set a map attribute with defined key,
// trying to parse it from raw value
// If parsing failed - no attribute will be set
func AttrPutMapIf(attrs pcommon.Map, attrKey string, attrValue any) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any is very bad performance wise, and giving that an encoding is an hot-path, this function would not work here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, this function is intended to store a parsed JSON object into attribute, not an any value, I'll fix it.
Please keep in mind that this function is only for data that we don't know how to properly parse, i.e. we are not supporting it at the moment or we don't have defined structure for it (for example it's dynamic structure)

if attrKey == "" || attrValue == nil {
return
}

if err := attrs.PutEmpty(attrKey).FromRaw(attrValue); err != nil {
// Failed to parse - put string representation of the attrValue
attrs.Remove(attrKey)
attrs.PutStr(attrKey+originalSuffix, fmt.Sprintf("%v", attrValue))
}
}
Loading