-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(alertmanager): Add exporter factory and config (#27836)
**Description:** Factory implementation of Alertmanager Exporter Initial PR - base configs and factory implementation **Link to tracking Issue:** [#23659](#23569) **Testing:** Unit tests for config and factory implementation **Documentation:** Readme and Sample Configs to use Alertmanager exporter --------- Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de> Co-authored-by: Juraci Paixão Kröhling <juraci@kroehling.de>
- Loading branch information
1 parent
59cfbe7
commit f509060
Showing
20 changed files
with
744 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: 'new_component' | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: alertmanagerexporter | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: "Add new exporter for sending events as alerts to Alertmanager" | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [23569] | ||
|
||
# (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] |
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
include ../../Makefile.Common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Alertmanager Exporter | ||
<!-- status autogenerated section --> | ||
| Status | | | ||
| ------------- |-----------| | ||
| Stability | [development]: traces | | ||
| Distributions | [contrib] | | ||
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Aexporter%2Falertmanager%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aexporter%2Falertmanager) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Aexporter%2Falertmanager%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aexporter%2Falertmanager) | | ||
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@jpkrohling](https://www.github.com/jpkrohling), [@sokoide](https://www.github.com/sokoide), [@mcube8](https://www.github.com/mcube8) | | ||
|
||
[development]: https://github.com/open-telemetry/opentelemetry-collector#development | ||
[contrib]: https://github.com/open-telemetry/opentelemetry-collector-releases/tree/main/distributions/otelcol-contrib | ||
<!-- end autogenerated section --> | ||
|
||
Exports OTEL Events (SpanEvent in Tracing added by AddEvent API) as Alerts to [Alertmanager] (https://prometheus.io/docs/alerting/latest/alertmanager/) back-end to notify Errors or Change events. | ||
|
||
Supported pipeline types: traces | ||
|
||
## Getting Started | ||
|
||
The following settings are required: | ||
|
||
- `endpoint` : Alertmanager endpoint to send events | ||
- `severity` (default info): Default severity for Alerts | ||
|
||
|
||
The following settings are optional: | ||
|
||
- `timeout` `sending_queue` and `retry_on_failure` settings as provided by [Exporter Helper](https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/exporterhelper#configuration) | ||
- [HTTP settings](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/confighttp/README.md) | ||
- [TLS and mTLS settings](https://github.com/open-telemetry/opentelemetry-collector/blob/main/config/configtls/README.md) | ||
- `generator_url` is the source of the alerts to be used in Alertmanager's payload. The default value is "opentelemetry-collector", and can be set to the URL of the opentelemetry collector. | ||
- `severity_attribute`is the spanevent Attribute name which can be used instead of default severity string in Alert payload | ||
eg: If severity_attribute is set to "foo" and the SpanEvent has an attribute called foo, foo's attribute value will be used as the severity value for that particular Alert generated from the SpanEvent. | ||
|
||
|
||
|
||
Example config: | ||
|
||
```yaml | ||
exporters: | ||
alertmanager: | ||
alertmanager/2: | ||
endpoint: "https://a.new.alertmanager.target:9093" | ||
severity: "debug" | ||
severity_attribute: "foo" | ||
tls: | ||
cert_file: /var/lib/mycert.pem | ||
key_file: /var/lib/key.pem | ||
timeout: 10s | ||
sending_queue: | ||
enabled: true | ||
num_consumers: 2 | ||
queue_size: 10 | ||
retry_on_failure: | ||
enabled: true | ||
initial_interval: 10s | ||
max_interval: 60s | ||
max_elapsed_time: 10m | ||
generator_url: "opentelemetry-collector" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package alertmanagerexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alertmanagerexporter" | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.opentelemetry.io/collector/exporter" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
) | ||
|
||
type alertmanagerExporter struct { | ||
config *Config | ||
tracesMarshaler ptrace.Marshaler | ||
settings component.TelemetrySettings | ||
endpoint string | ||
generatorURL string | ||
defaultSeverity string | ||
severityAttribute string | ||
} | ||
|
||
func (s *alertmanagerExporter) pushTraces(_ context.Context, _ ptrace.Traces) error { | ||
|
||
// To Be Implemented | ||
return nil | ||
} | ||
|
||
func (s *alertmanagerExporter) start(_ context.Context, _ component.Host) error { | ||
|
||
// To Be Implemented | ||
return nil | ||
} | ||
|
||
func (s *alertmanagerExporter) shutdown(context.Context) error { | ||
// To Be Implemented | ||
return nil | ||
} | ||
|
||
func newAlertManagerExporter(cfg *Config, set component.TelemetrySettings) *alertmanagerExporter { | ||
|
||
return &alertmanagerExporter{ | ||
config: cfg, | ||
settings: set, | ||
tracesMarshaler: &ptrace.JSONMarshaler{}, | ||
endpoint: cfg.HTTPClientSettings.Endpoint, | ||
generatorURL: cfg.GeneratorURL, | ||
defaultSeverity: cfg.DefaultSeverity, | ||
severityAttribute: cfg.SeverityAttribute, | ||
} | ||
} | ||
|
||
func newTracesExporter(ctx context.Context, cfg component.Config, set exporter.CreateSettings) (exporter.Traces, error) { | ||
config := cfg.(*Config) | ||
|
||
s := newAlertManagerExporter(config, set.TelemetrySettings) | ||
|
||
return exporterhelper.NewTracesExporter( | ||
ctx, | ||
set, | ||
cfg, | ||
s.pushTraces, | ||
exporterhelper.WithCapabilities(consumer.Capabilities{MutatesData: false}), | ||
// Disable Timeout/RetryOnFailure and SendingQueue | ||
exporterhelper.WithStart(s.start), | ||
exporterhelper.WithTimeout(config.TimeoutSettings), | ||
exporterhelper.WithRetry(config.RetrySettings), | ||
exporterhelper.WithQueue(config.QueueSettings), | ||
exporterhelper.WithShutdown(s.shutdown), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package alertmanagerexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alertmanagerexporter" | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper" | ||
) | ||
|
||
// Config defines configuration for alertmanager exporter. | ||
type Config struct { | ||
exporterhelper.TimeoutSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct. | ||
exporterhelper.QueueSettings `mapstructure:"sending_queue"` | ||
exporterhelper.RetrySettings `mapstructure:"retry_on_failure"` | ||
|
||
confighttp.HTTPClientSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct. | ||
GeneratorURL string `mapstructure:"generator_url"` | ||
DefaultSeverity string `mapstructure:"severity"` | ||
SeverityAttribute string `mapstructure:"severity_attribute"` | ||
} | ||
|
||
var _ component.Config = (*Config)(nil) | ||
|
||
// Validate checks if the exporter configuration is valid | ||
func (cfg *Config) Validate() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package alertmanagerexporter | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.opentelemetry.io/collector/config/configopaque" | ||
"go.opentelemetry.io/collector/config/configtls" | ||
"go.opentelemetry.io/collector/confmap/confmaptest" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alertmanagerexporter/internal/metadata" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
t.Parallel() | ||
|
||
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) | ||
require.NoError(t, err) | ||
|
||
// Endpoint doesn't have a default value so set it directly. | ||
defaultCfg := createDefaultConfig().(*Config) | ||
|
||
tests := []struct { | ||
id component.ID | ||
expected component.Config | ||
}{ | ||
|
||
{ | ||
id: component.NewIDWithName(metadata.Type, ""), | ||
expected: defaultCfg, | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "2"), | ||
expected: &Config{ | ||
GeneratorURL: "opentelemetry-collector", | ||
DefaultSeverity: "info", | ||
SeverityAttribute: "foo", | ||
TimeoutSettings: exporterhelper.TimeoutSettings{ | ||
Timeout: 10 * time.Second, | ||
}, | ||
RetrySettings: exporterhelper.RetrySettings{ | ||
Enabled: true, | ||
InitialInterval: 10 * time.Second, | ||
MaxInterval: 1 * time.Minute, | ||
MaxElapsedTime: 10 * time.Minute, | ||
RandomizationFactor: backoff.DefaultRandomizationFactor, | ||
Multiplier: backoff.DefaultMultiplier, | ||
}, | ||
QueueSettings: exporterhelper.QueueSettings{ | ||
Enabled: true, | ||
NumConsumers: 2, | ||
QueueSize: 10, | ||
}, | ||
HTTPClientSettings: confighttp.HTTPClientSettings{ | ||
Headers: map[string]configopaque.String{ | ||
"can you have a . here?": "F0000000-0000-0000-0000-000000000000", | ||
"header1": "234", | ||
"another": "somevalue", | ||
}, | ||
Endpoint: "a.new.alertmanager.target:9093", | ||
TLSSetting: configtls.TLSClientSetting{ | ||
TLSSetting: configtls.TLSSetting{ | ||
CAFile: "/var/lib/mycert.pem", | ||
}, | ||
}, | ||
ReadBufferSize: 0, | ||
WriteBufferSize: 524288, | ||
Timeout: time.Second * 10, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.id.String(), func(t *testing.T) { | ||
factory := NewFactory() | ||
cfg := factory.CreateDefaultConfig() | ||
|
||
sub, err := cm.Sub(tt.id.String()) | ||
require.NoError(t, err) | ||
require.NoError(t, component.UnmarshalConfig(sub, cfg)) | ||
|
||
assert.NoError(t, component.ValidateConfig(cfg)) | ||
assert.Equal(t, tt.expected, cfg) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 alertmanagerexporter exports spanevents as alerts to Prometheus Alertmanager | ||
package alertmanagerexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alertmanagerexporter" |
Oops, something went wrong.