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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ You can use the following arguments with `otelcol.receiver.cloudflare`:
| `endpoint` | `string` | The `<HOST:PORT>` endpoint address on which the receiver awaits requests from Cloudflare. | | yes |
| `secret` | `string` | If this value is set, the receiver expects to see it in any valid requests under the `X-CF-Secret` header. | | no |
| `attributes` | `map[string]string` | Sets log attributes from message fields. Only string, boolean, integer, or float fields can be mapped. | | no |
| `delimiter` | `string` | The separator to join nested fields in the log message when setting attributes. | `"."` | no |
| `separator` | `string` | The separator to join nested fields in the log message when setting attributes. | `"."` | no |
| `timestamp_field` | `string` | Log field name that contains timestamp. | `"EdgeStartTimestamp"` | no |
| `timestamp_format` | `string` | One of `unix`, `unixnano`, or `rfc3339`, matching how your LogPush job encodes the timestamp field. | `"rfc3339"` | no |

Expand Down
29 changes: 15 additions & 14 deletions internal/component/otelcol/receiver/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,25 @@ type Arguments struct {

// SetToDefault implements syntax.Defaulter.
func (args *Arguments) SetToDefault() {
// Defaults filled by upstream OTel receiver in a factory.
cfg := cloudflarereceiver.NewFactory().CreateDefaultConfig().(*cloudflarereceiver.Config)
*args = Arguments{
TimestampField: cfg.Logs.TimestampField,
TimestampFormat: cfg.Logs.TimestampFormat,
Separator: cfg.Logs.Separator,
}
}

func (args Arguments) receiverConfig() *cloudflarereceiver.Config {
tlsCfg := args.TLS.Convert()
logCfg := cloudflarereceiver.LogsConfig{
Secret: args.Secret,
Endpoint: args.Endpoint,
TLS: tlsCfg.Get(),
Attributes: args.Attributes,
TimestampField: args.TimestampField,
TimestampFormat: args.TimestampFormat,
Separator: args.Separator,
}

return &cloudflarereceiver.Config{
Logs: logCfg,
}
cfg := cloudflarereceiver.NewFactory().CreateDefaultConfig().(*cloudflarereceiver.Config)
cfg.Logs.Secret = args.Secret
cfg.Logs.Endpoint = args.Endpoint
cfg.Logs.TLS = tlsCfg.Get()
cfg.Logs.Attributes = args.Attributes
cfg.Logs.TimestampField = args.TimestampField
cfg.Logs.TimestampFormat = args.TimestampFormat
cfg.Logs.Separator = args.Separator
return cfg
}

// Validate implements syntax.Validator.
Expand Down
76 changes: 38 additions & 38 deletions internal/component/otelcol/receiver/cloudflare/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (
"github.com/grafana/alloy/syntax"
)

// expectedConfig returns the upstream factory defaults with override applied,
// so each case only spells out what it actually overrides.
func expectedConfig(override func(logs *cloudflarereceiver.LogsConfig)) cloudflarereceiver.Config {
cfg := cloudflarereceiver.NewFactory().CreateDefaultConfig().(*cloudflarereceiver.Config)
override(&cfg.Logs)
return *cfg
}

func TestArguments_UnmarshalAlloy(t *testing.T) {
cases := []struct {
testName string
Expand All @@ -23,11 +31,9 @@ func TestArguments_UnmarshalAlloy(t *testing.T) {
endpoint = "localhost:8080/webhook"
output {}
`,
expected: cloudflarereceiver.Config{
Logs: cloudflarereceiver.LogsConfig{
Endpoint: "localhost:8080/webhook",
},
},
expected: expectedConfig(func(logs *cloudflarereceiver.LogsConfig) {
logs.Endpoint = "localhost:8080/webhook"
}),
},
{
testName: "full configuration without TLS",
Expand All @@ -43,19 +49,17 @@ func TestArguments_UnmarshalAlloy(t *testing.T) {
separator = "_"
output {}
`,
expected: cloudflarereceiver.Config{
Logs: cloudflarereceiver.LogsConfig{
Secret: "my-secret",
Endpoint: "localhost:8080/cloudflare-webhook",
Attributes: map[string]string{
"service.name": "cloudflare-logs",
"environment": "production",
},
TimestampField: "EdgeStartTimestamp",
TimestampFormat: "unix",
Separator: "_",
},
},
expected: expectedConfig(func(logs *cloudflarereceiver.LogsConfig) {
logs.Secret = "my-secret"
logs.Endpoint = "localhost:8080/cloudflare-webhook"
logs.Attributes = map[string]string{
"service.name": "cloudflare-logs",
"environment": "production",
}
logs.TimestampField = "EdgeStartTimestamp"
logs.TimestampFormat = "unix"
logs.Separator = "_"
}),
},
{
testName: "configuration with TLS",
Expand All @@ -69,19 +73,17 @@ func TestArguments_UnmarshalAlloy(t *testing.T) {
timestamp_format = "unixnano"
output {}
`,
expected: cloudflarereceiver.Config{
Logs: cloudflarereceiver.LogsConfig{
Secret: "my-secret",
Endpoint: "localhost:8443/secure-webhook",
TLS: &configtls.ServerConfig{
Config: configtls.Config{
CertFile: "/path/to/cert.pem",
KeyFile: "/path/to/key.pem",
},
expected: expectedConfig(func(logs *cloudflarereceiver.LogsConfig) {
logs.Secret = "my-secret"
logs.Endpoint = "localhost:8443/secure-webhook"
logs.TLS = &configtls.ServerConfig{
Config: configtls.Config{
CertFile: "/path/to/cert.pem",
KeyFile: "/path/to/key.pem",
},
TimestampFormat: "unixnano",
},
},
}
logs.TimestampFormat = "unixnano"
}),
},
{
testName: "configuration with custom timestamp field",
Expand All @@ -92,14 +94,12 @@ func TestArguments_UnmarshalAlloy(t *testing.T) {
timestamp_format = "rfc3339"
output {}
`,
expected: cloudflarereceiver.Config{
Logs: cloudflarereceiver.LogsConfig{
Secret: "my-secret",
Endpoint: "localhost:8080/webhook",
TimestampField: "RequestTimestamp",
TimestampFormat: "rfc3339",
},
},
expected: expectedConfig(func(logs *cloudflarereceiver.LogsConfig) {
logs.Secret = "my-secret"
logs.Endpoint = "localhost:8080/webhook"
logs.TimestampField = "RequestTimestamp"
logs.TimestampFormat = "rfc3339"
}),
},
}

Expand Down
Loading