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

Webhookevent client #20751

Merged
merged 52 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
b0365e7
eventReceiver first pass
shalper2 Mar 29, 2023
b648940
make gotidy
shalper2 Mar 30, 2023
a7954fc
implemented receiver
shalper2 Apr 6, 2023
7d87f4a
small change to factory
shalper2 Apr 7, 2023
2729c0e
added chloggen
shalper2 Apr 7, 2023
c3ecb94
config_test
shalper2 Apr 11, 2023
6f5b3c7
config test corrections
shalper2 Apr 12, 2023
c05174d
factory_test
shalper2 Apr 13, 2023
6a773e0
receiver tests
shalper2 Apr 17, 2023
a82caf7
receiver tests fix
shalper2 Apr 19, 2023
323c7a5
make gotidy/fmt/lint
shalper2 Apr 19, 2023
79e35e6
some lint fixes
shalper2 Apr 19, 2023
e68f715
some more lint fixes
shalper2 Apr 19, 2023
8f1a6fd
removed accidental additions
shalper2 Apr 19, 2023
e6aa8df
fixed lint
shalper2 Apr 19, 2023
dbdb6da
req_to_log tests
shalper2 Apr 21, 2023
2f26445
gci
shalper2 Apr 21, 2023
92d19f5
fixed return value for factory
shalper2 Apr 24, 2023
ae71f61
added to config
shalper2 Apr 24, 2023
f5f4c58
updated config_test
shalper2 Apr 24, 2023
70de469
rebase and lint
shalper2 Apr 25, 2023
c527080
fixed accidental change to azuremonitorreceiver
shalper2 Apr 25, 2023
d1c086f
fixed version
shalper2 May 3, 2023
e9fb91c
make gotidy
shalper2 May 3, 2023
2228675
removed weird azuremonitor edit
shalper2 May 9, 2023
78659ef
version update
shalper2 May 9, 2023
aeafb73
more versions
shalper2 May 9, 2023
836ccb2
even more version stuff...
shalper2 May 9, 2023
2605397
oh another version fix
shalper2 May 9, 2023
15f472e
fun with versions
shalper2 May 9, 2023
8e28777
small fixes to receiver and req2log
shalper2 May 11, 2023
f3ecc11
fixed req_to_log and tests
shalper2 May 11, 2023
7134fde
gci
shalper2 May 11, 2023
d73fde1
make generate
shalper2 May 15, 2023
ca839ee
deleted unused TypeStr variable
shalper2 May 15, 2023
973048f
removed TypeStr
shalper2 May 15, 2023
ed8fdfb
removed typeStr, again
shalper2 May 15, 2023
7113c1e
gci'd
shalper2 May 15, 2023
592b4bf
set scopelog name and version
shalper2 May 17, 2023
68b0ace
gci
shalper2 May 17, 2023
db4d0fe
fixed license in req_to_log
shalper2 May 22, 2023
8e41fc7
changed to localhost
shalper2 May 25, 2023
3b21f22
versions
shalper2 May 25, 2023
ba8d5f4
returning sooner
shalper2 May 25, 2023
f009661
versions
shalper2 May 27, 2023
0cf962a
version fix
shalper2 May 30, 2023
ab87dfd
version fix
shalper2 May 30, 2023
4952cca
version and lint
shalper2 Jun 2, 2023
d905533
more version stuff
shalper2 Jun 2, 2023
cf6d6e2
linting
shalper2 Jun 2, 2023
ad32af1
version update 1
shalper2 Jun 6, 2023
ec7cfa9
version 2
shalper2 Jun 6, 2023
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
16 changes: 16 additions & 0 deletions .chloggen/webhookevent_client.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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: webhookeventreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "first pass implementing the components internals."

# One or more tracking issues related to the change
issues: [18101]

# (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:
50 changes: 50 additions & 0 deletions receiver/webhookeventreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,60 @@
package webhookeventreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/webhookeventreceiver"

import (
"errors"
"time"

"go.opentelemetry.io/collector/config/confighttp"
"go.uber.org/multierr"
)

var (
errMissingEndpointFromConfig = errors.New("missing receiver server endpoint from config")
errReadTimeoutExceedsMaxValue = errors.New("The duration specified for read_timeout exceeds the maximum allowed value of 10s")
errWriteTimeoutExceedsMaxValue = errors.New("The duration specified for write_timeout exceeds the maximum allowed value of 10s")
)

// Config defines configuration for the Generic Webhook receiver.
type Config struct {
confighttp.HTTPServerSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct
ReadTimeout string `mapstructure:"read_timeout"` // wait time for reading request headers in ms. Default is twenty seconds.
WriteTimeout string `mapstructure:"write_timeout"` // wait time for writing request response in ms. Default is twenty seconds.
Path string `mapstructure:"path"` // path for data collection. Default is <host>:<port>/services/collector
HealthPath string `mapstructure:"health_path"` // path for health check api. Default is /services/collector/health
}

func (cfg *Config) Validate() error {
var errs error

maxReadWriteTimeout, _ := time.ParseDuration("10s")

if cfg.HTTPServerSettings.Endpoint == "" {
errs = multierr.Append(errs, errMissingEndpointFromConfig)
}

// If a user defines a custom read/write timeout there is a maximum value
// of 10s imposed here.
if cfg.ReadTimeout != "" {
readTimeout, err := time.ParseDuration(cfg.ReadTimeout)
if err != nil {
errs = multierr.Append(errs, err)
}

if readTimeout > maxReadWriteTimeout {
errs = multierr.Append(errs, errReadTimeoutExceedsMaxValue)
}
}

if cfg.WriteTimeout != "" {
writeTimeout, err := time.ParseDuration(cfg.WriteTimeout)
if err != nil {
errs = multierr.Append(errs, err)
}

if writeTimeout > maxReadWriteTimeout {
errs = multierr.Append(errs, errWriteTimeoutExceedsMaxValue)
}
}

return errs
}
109 changes: 108 additions & 1 deletion receiver/webhookeventreceiver/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,111 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package webhookeventreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/genericwebhookreceiver"
package webhookeventreceiver

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/webhookeventreceiver/internal/metadata"
)

// only one validate check so far
func TestValidateConfig(t *testing.T) {
t.Parallel()

var errs error
errs = multierr.Append(errs, errMissingEndpointFromConfig)
errs = multierr.Append(errs, errReadTimeoutExceedsMaxValue)
errs = multierr.Append(errs, errWriteTimeoutExceedsMaxValue)

tests := []struct {
desc string
expect error
conf Config
}{
{
desc: "Missing valid endpoint",
expect: errMissingEndpointFromConfig,
conf: Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "",
},
},
},
{
desc: "ReadTimeout exceeds maximum value",
expect: errReadTimeoutExceedsMaxValue,
conf: Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "localhost:0",
},
ReadTimeout: "14s",
},
},
{
desc: "WriteTimeout exceeds maximum value",
expect: errWriteTimeoutExceedsMaxValue,
conf: Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "localhost:0",
},
WriteTimeout: "14s",
},
},
{
desc: "Multiple invalid configs",
expect: errs,
conf: Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "",
},
WriteTimeout: "14s",
ReadTimeout: "15s",
},
},
}

for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
err := test.conf.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), test.expect.Error())
})
}
}

func TestLoadConfig(t *testing.T) {
t.Parallel()

cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
// LoadConf includes the TypeStr which NewFactory does not set
id := component.NewIDWithName(metadata.Type, "valid_config")
cmNoStr, err := cm.Sub(id.String())
require.NoError(t, err)

expect := &Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: "localhost:8080",
},
ReadTimeout: "500ms",
WriteTimeout: "500ms",
Path: "some/path",
HealthPath: "health/path",
}

// create expected config
factory := NewFactory()
conf := factory.CreateDefaultConfig()
require.NoError(t, component.UnmarshalConfig(cmNoStr, conf))
require.NoError(t, component.ValidateConfig(conf))

require.Equal(t, expect, conf)
}
20 changes: 13 additions & 7 deletions receiver/webhookeventreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,26 @@ import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/receiver"

"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/webhookeventreceiver/internal/metadata"
)

const (
// might add this later, for now I wish to require a valid
// endpoint to be declared by the user.
// Default endpoints to bind to.
defaultEndpoint = ":8080"
// defaultEndpoint = "localhost:8080"
scopeLogName = "otlp/" + metadata.Type
defaultReadTimeout = "500ms"
defaultWriteTimeout = "500ms"
defaultPath = "/events"
defaultHealthPath = "/health_check"
)

// NewFactory creates a factory for Generic Webhook Receiver.
func NewFactory() component.Factory {
func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
Expand All @@ -31,9 +37,10 @@ func NewFactory() component.Factory {
// Default configuration for the generic webhook receiver
func createDefaultConfig() component.Config {
return &Config{
HTTPServerSettings: confighttp.HTTPServerSettings{
Endpoint: defaultEndpoint,
},
Path: defaultPath,
HealthPath: defaultHealthPath,
ReadTimeout: defaultReadTimeout,
WriteTimeout: defaultWriteTimeout,
}
}

Expand All @@ -45,6 +52,5 @@ func createLogsReceiver(
consumer consumer.Logs,
) (receiver.Logs, error) {
conf := cfg.(*Config)

return newLogsReceiver(params, *conf, consumer)
}
67 changes: 67 additions & 0 deletions receiver/webhookeventreceiver/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,70 @@
// SPDX-License-Identifier: Apache-2.0

package webhookeventreceiver

import (
"context"
"testing"

"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/receiver/receivertest"
)

func TestFactoryCreate(t *testing.T) {
factory := NewFactory()
require.EqualValues(t, "webhookevent", factory.Type())
}

func TestDefaultConfig(t *testing.T) {
cfg := createDefaultConfig()
require.NotNil(t, cfg, "Failed to create default configuration")
}

func TestCreateLogsReceiver(t *testing.T) {
tests := []struct {
desc string
run func(t *testing.T)
}{
{
desc: "Defaults with valid inputs",
run: func(t *testing.T) {
t.Parallel()

cfg := createDefaultConfig().(*Config)
cfg.Endpoint = "localhost:8080"
require.NoError(t, cfg.Validate(), "error validating default config")

_, err := createLogsReceiver(
context.Background(),
receivertest.NewNopCreateSettings(),
cfg,
consumertest.NewNop(),
)
require.NoError(t, err, "failed to create logs receiver")
},
},
{
desc: "Missing consumer",
run: func(t *testing.T) {
t.Parallel()

cfg := createDefaultConfig().(*Config)
cfg.Endpoint = "localhost:8080"
require.NoError(t, cfg.Validate(), "error validating default config")

_, err := createLogsReceiver(
context.Background(),
receivertest.NewNopCreateSettings(),
cfg,
nil,
)
require.Error(t, err, "Succeeded in creating a receiver without a consumer")
},
},
}

for _, test := range tests {
t.Run(test.desc, test.run)
}
}
17 changes: 12 additions & 5 deletions receiver/webhookeventreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,52 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/webhoo
go 1.19

require (
github.com/json-iterator/go v1.1.12
github.com/julienschmidt/httprouter v1.3.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector v0.79.0
go.opentelemetry.io/collector/component v0.79.0
go.opentelemetry.io/collector/confmap v0.79.0
go.opentelemetry.io/collector/consumer v0.79.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0012
go.opentelemetry.io/collector/receiver v0.79.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.24.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.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.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.9.0 // indirect
go.opentelemetry.io/collector/confmap v0.79.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector/exporter v0.79.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0012 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0012 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

retract (
Expand Down
Loading