Skip to content

Commit

Permalink
Allow specifying name on the connection to RabbitMQ (#34682)
Browse files Browse the repository at this point in the history
  • Loading branch information
snichme authored Oct 22, 2024
1 parent 7d92d6a commit ad6a19f
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 28 deletions.
27 changes: 27 additions & 0 deletions .chloggen/rabbitmq-connection-name.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. filelogreceiver)
component: rabbitmqexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allow to configure the name of the AMQP connection in the rabbitmqexporter

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

# (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: [user]
45 changes: 23 additions & 22 deletions exporter/rabbitmqexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,36 @@ This component expects that exchanges, queues, and bindings already exist - they

The following settings can be configured:
- `connection`:
- `endpoint` (required, ex = amqp://localhost:5672): Endpoint to connect to RabbitMQ
- `vhost` (optional): The RabbitMQ [virtual host](https://www.rabbitmq.com/docs/vhosts) to connect to
- `auth`:
- `plain`: Configuration if using SASL PLAIN authentication
- `username` (required): username for authentication
- `password`: password for authentication
- `tls` (optional): [TLS configuration](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configtls/configtls.go#L32)
- `routing`:
- `routing_key` (default = otlp_spans for traces, otlp_metrics for metrics, otlp_logs for logs): Routing key used to route exported messages to RabbitMQ consumers
- `exchange`: Name of the exchange used to route messages. If omitted, the [default exchange](https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-default) is used which routes to a queue with the same as the routing key. Only [direct exchanges](https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-direct) are currently supported. Note that this component does not handle queue creation or binding.
- `durable` (default = true): Whether to instruct RabbitMQ to make messages [durable](https://www.rabbitmq.com/docs/queues#durability) by writing to disk
- `encoding_extension`: (defaults to OTLP protobuf format): ID of the [encoding extension](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/encoding) to use to marshal data
- `retry_on_failure`:
- `enabled` (default = false)
- `endpoint` (required, ex = amqp://localhost:5672): Endpoint to connect to RabbitMQ
- `vhost` (optional): The RabbitMQ [virtual host](https://www.rabbitmq.com/docs/vhosts) to connect to
- `auth`:
- `plain`: Configuration if using SASL PLAIN authentication
- `username` (required): username for authentication
- `password`: password for authentication
- `tls` (optional): [TLS configuration](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configtls/configtls.go#L32)
- `name` (optional): The name of the connection, visible in in RabbitMQ management interface
- `routing`:
- `routing_key` (default = otlp_spans for traces, otlp_metrics for metrics, otlp_logs for logs): Routing key used to route exported messages to RabbitMQ consumers
- `exchange`: Name of the exchange used to route messages. If omitted, the [default exchange](https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-default) is used which routes to a queue with the same as the routing key. Only [direct exchanges](https://www.rabbitmq.com/tutorials/amqp-concepts#exchange-direct) are currently supported. Note that this component does not handle queue creation or binding.
- `durable` (default = true): Whether to instruct RabbitMQ to make messages [durable](https://www.rabbitmq.com/docs/queues#durability) by writing to disk
- `encoding_extension`: (defaults to OTLP protobuf format): ID of the [encoding extension](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/encoding) to use to marshal data
- `retry_on_failure`:
- `enabled` (default = false)

Example config:

```yaml
exporters:
rabbitmq:
connection:
endpoint: amqp://localhost:5672
auth:
plain:
username: user
password: pass
encoding_extension: otlp_encoding/rabbitmq
connection:
endpoint: amqp://localhost:5672
auth:
plain:
username: user
password: pass
encoding_extension: otlp_encoding/rabbitmq

extensions:
otlp_encoding/rabbitmq:
protocol: otlp_json
```
```
1 change: 1 addition & 0 deletions exporter/rabbitmqexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ConnectionConfig struct {
ConnectionTimeout time.Duration `mapstructure:"connection_timeout"`
Heartbeat time.Duration `mapstructure:"heartbeat"`
PublishConfirmationTimeout time.Duration `mapstructure:"publish_confirmation_timeout"`
Name string `mapstructure:"name"`
}

type RoutingConfig struct {
Expand Down
25 changes: 19 additions & 6 deletions exporter/rabbitmqexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const (
metricsRoutingKey = "otlp_metrics"
logsRoutingKey = "otlp_logs"

spansConnectionName = "otel-collector-spans"
metricsConnectionName = "otel-collector-metrics"
logsConnectionName = "otel-collector-logs"
defaultSpansConnectionName = "otel-collector-spans"
defaultMetricsConnectionName = "otel-collector-metrics"
defaultLogsConnectionName = "otel-collector-logs"
)

func NewFactory() exporter.Factory {
Expand Down Expand Up @@ -66,7 +66,11 @@ func createTracesExporter(
config := cfg.(*Config)

routingKey := getRoutingKeyOrDefault(config, spansRoutingKey)
r := newRabbitmqExporter(config, set.TelemetrySettings, newPublisherFactory(set), newTLSFactory(config), routingKey, spansConnectionName)
connectionName := defaultSpansConnectionName
if config.Connection.Name != "" {
connectionName = config.Connection.Name
}
r := newRabbitmqExporter(config, set.TelemetrySettings, newPublisherFactory(set), newTLSFactory(config), routingKey, connectionName)

return exporterhelper.NewTracesExporter(
ctx,
Expand All @@ -88,7 +92,12 @@ func createMetricsExporter(
config := (cfg.(*Config))

routingKey := getRoutingKeyOrDefault(config, metricsRoutingKey)
r := newRabbitmqExporter(config, set.TelemetrySettings, newPublisherFactory(set), newTLSFactory(config), routingKey, metricsConnectionName)

connectionName := defaultMetricsConnectionName
if config.Connection.Name != "" {
connectionName = config.Connection.Name
}
r := newRabbitmqExporter(config, set.TelemetrySettings, newPublisherFactory(set), newTLSFactory(config), routingKey, connectionName)

return exporterhelper.NewMetricsExporter(
ctx,
Expand All @@ -110,7 +119,11 @@ func createLogsExporter(
config := (cfg.(*Config))

routingKey := getRoutingKeyOrDefault(config, logsRoutingKey)
r := newRabbitmqExporter(config, set.TelemetrySettings, newPublisherFactory(set), newTLSFactory(config), routingKey, logsConnectionName)
connectionName := defaultLogsConnectionName
if config.Connection.Name != "" {
connectionName = config.Connection.Name
}
r := newRabbitmqExporter(config, set.TelemetrySettings, newPublisherFactory(set), newTLSFactory(config), routingKey, connectionName)

return exporterhelper.NewLogsExporter(
ctx,
Expand Down
30 changes: 30 additions & 0 deletions exporter/rabbitmqexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ func TestCreateLogsExporter(t *testing.T) {
assert.NotNil(t, te)
}

func TestCreateMetricsExporterWithConnectionName(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.Connection.Name = "my-conn-name"

te, err := factory.CreateMetricsExporter(context.Background(), exportertest.NewNopSettings(), cfg)
assert.NoError(t, err)
assert.NotNil(t, te)
}

func TestCreateExporterWithCustomRoutingKey(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
Expand All @@ -57,6 +67,16 @@ func TestCreateExporterWithCustomRoutingKey(t *testing.T) {
assert.NotNil(t, te)
}

func TestCreateExporterWithConnectionName(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.Connection.Name = "my-conn-name"

te, err := factory.CreateLogsExporter(context.Background(), exportertest.NewNopSettings(), cfg)
assert.NoError(t, err)
assert.NotNil(t, te)
}

func TestCreateExporterWithTLSSettings(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
Expand All @@ -66,3 +86,13 @@ func TestCreateExporterWithTLSSettings(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, te)
}

func TestCreateTracesExporterWithConnectionName(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.Connection.Name = "my-conn-name"

te, err := factory.CreateTracesExporter(context.Background(), exportertest.NewNopSettings(), cfg)
assert.NoError(t, err)
assert.NotNil(t, te)
}

0 comments on commit ad6a19f

Please sign in to comment.