Skip to content

Commit

Permalink
rabbitmqmessagereciever: scaffolding with initial config
Browse files Browse the repository at this point in the history
Signed-off-by: Benedikt Bongartz <bongartz@klimlive.de>
  • Loading branch information
frzifus committed Sep 27, 2024
1 parent 507ec47 commit 595afe0
Show file tree
Hide file tree
Showing 15 changed files with 594 additions and 0 deletions.
63 changes: 63 additions & 0 deletions receiver/rabbitmqmessagereciever/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"errors"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/config/configtls"
)

var _ component.Config = (*Config)(nil)

type Config struct {
Connection ConnectionConfig `mapstructure:"connection"`
EncodingExtensionID *component.ID `mapstructure:"encoding_extension"`
RetrySettings configretry.BackOffConfig `mapstructure:"retry_on_failure"`
}

// Validate checks if the exporter configuration is valid
func (cfg *Config) Validate() error {
if cfg.Connection.Endpoint == "" {
return errors.New("connection.endpoint is required")
}

// Password-less users are possible so only validate username
if cfg.Connection.Auth.Plain.Username == "" {
return errors.New("connection.auth.plain.username is required")
}

return nil
}

type ConnectionConfig struct {
Name string `mapstructure:"name"`
Endpoint string `mapstructure:"endpoint"`
VHost string `mapstructure:"vhost"`
TLSConfig *configtls.ClientConfig `mapstructure:"tls"`
Auth AuthConfig `mapstructure:"auth"`
Queue []QueueConfig `mapstructure:"queue"`
ConnectionTimeout time.Duration `mapstructure:"connection_timeout"`
Heartbeat time.Duration `mapstructure:"heartbeat"`
EncodingExtension component.ID `mapstructure:"encoding_extension"`
Durable bool `mapstructure:"durable"`
}

type AuthConfig struct {
Plain PlainAuth `mapstructure:"plain"`
}

type PlainAuth struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}

type QueueConfig struct {
Name string `mapstructure:"name"`
Consumer string `mapstructure:"consumer"`
Exclusive bool `mapstructure:"exclusive"`
}
7 changes: 7 additions & 0 deletions receiver/rabbitmqmessagereciever/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

// Package rabbitmqdatareceiver imports telemetry from RabbitMQ using the AMQP 0.9.1 protocol
package rabbitmqmessagereciever // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqmessagereciever"
101 changes: 101 additions & 0 deletions receiver/rabbitmqmessagereciever/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"context"
"crypto/tls"
"errors"

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

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

func createDefaultConfig() component.Config {
return &Config{
Connection: ConnectionConfig{
Name: "otel-default",
Endpoint: "127.0.0.1:5672",
Auth: AuthConfig{
Plain: PlainAuth{
Username: "guest",
},
},
},
}
}

// NewFactory creates a factory for rabbitmqdata receiver.
func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
receiver.WithMetrics(createMetricReceiver, metadata.MetricsStability),
receiver.WithLogs(createLogReceiver, metadata.MetricsStability),
receiver.WithTraces(createTraceReceiver, metadata.MetricsStability),
)
}

func createMetricReceiver(
_ context.Context,
params receiver.Settings,
baseCfg component.Config,
consumer consumer.Metrics,
) (receiver.Metrics, error) {
cfg, ok := baseCfg.(*Config)
if !ok {
return nil, errors.New("invalid config object")
}
if err := cfg.Validate(); err != nil {
return nil, err
}
set := params.TelemetrySettings
return newRabbitMQReceiver(cfg, set, nil, newTLSFactory(cfg))
}

func createLogReceiver(
_ context.Context,
params receiver.Settings,
baseCfg component.Config,
consumer consumer.Logs,
) (receiver.Logs, error) {
cfg, ok := baseCfg.(*Config)
if !ok {
return nil, errors.New("invalid config object")
}
if err := cfg.Validate(); err != nil {
return nil, err
}
set := params.TelemetrySettings
return newRabbitMQReceiver(cfg, set, nil, newTLSFactory(cfg))
}

func createTraceReceiver(
_ context.Context,
params receiver.Settings,
baseCfg component.Config,
consumer consumer.Traces,
) (receiver.Traces, error) {
cfg, ok := baseCfg.(*Config)
if !ok {
return nil, errors.New("invalid config object")
}
if err := cfg.Validate(); err != nil {
return nil, err
}
set := params.TelemetrySettings
return newRabbitMQReceiver(cfg, set, nil, newTLSFactory(cfg))
}

func newTLSFactory(config *Config) tlsFactory {
if config.Connection.TLSConfig != nil {
return config.Connection.TLSConfig.LoadTLSConfig
}
return func(context.Context) (*tls.Config, error) {
return nil, nil
}
}
4 changes: 4 additions & 0 deletions receiver/rabbitmqmessagereciever/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package rabbitmqmessagereciever // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqmessagereciever"
71 changes: 71 additions & 0 deletions receiver/rabbitmqmessagereciever/generated_component_test.go

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

12 changes: 12 additions & 0 deletions receiver/rabbitmqmessagereciever/generated_package_test.go

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

58 changes: 58 additions & 0 deletions receiver/rabbitmqmessagereciever/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/receiver/rabbitmqmessagereciever

go 1.22.0

require (
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/collector/component v0.110.0
go.opentelemetry.io/collector/config/configretry v1.16.0
go.opentelemetry.io/collector/config/configtls v1.16.0
go.opentelemetry.io/collector/confmap v1.16.0
go.opentelemetry.io/collector/consumer v0.110.0
go.opentelemetry.io/collector/consumer/consumertest v0.110.0
go.opentelemetry.io/collector/pdata v1.16.0
go.opentelemetry.io/collector/receiver v0.110.0
go.uber.org/goleak v1.3.0
)

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.1 // 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.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/component/componentprofiles v0.110.0 // indirect
go.opentelemetry.io/collector/config/configopaque v1.16.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.110.0 // indirect
go.opentelemetry.io/collector/consumer/consumerprofiles v0.110.0 // indirect
go.opentelemetry.io/collector/internal/globalsignal v0.110.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.110.0 // indirect
go.opentelemetry.io/collector/pipeline v0.110.0 // indirect
go.opentelemetry.io/collector/receiver/receiverprofiles v0.110.0 // indirect
go.opentelemetry.io/otel v1.30.0 // indirect
go.opentelemetry.io/otel/metric v1.30.0 // indirect
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/grpc v1.66.2 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 595afe0

Please sign in to comment.