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

[exporter/sematext] New component: Sematext Exporter #36222

Closed
Closed
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
21 changes: 21 additions & 0 deletions .chloggen/sematextexporter-new-component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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: exporter/sematext
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Add a new component for exporting metrics to Sematext"
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [35540]
# (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]
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ body:
- exporter/pulsar
- exporter/rabbitmq
- exporter/sapm
- exporter/sematext
- exporter/sentry
- exporter/signalfx
- exporter/splunkhec
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ body:
- exporter/pulsar
- exporter/rabbitmq
- exporter/sapm
- exporter/sematext
- exporter/sentry
- exporter/signalfx
- exporter/splunkhec
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/other.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ body:
- exporter/pulsar
- exporter/rabbitmq
- exporter/sapm
- exporter/sematext
- exporter/sentry
- exporter/signalfx
- exporter/splunkhec
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/unmaintained.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ body:
- exporter/pulsar
- exporter/rabbitmq
- exporter/sapm
- exporter/sematext
- exporter/sentry
- exporter/signalfx
- exporter/splunkhec
Expand Down
1 change: 1 addition & 0 deletions exporter/sematextexporter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
56 changes: 56 additions & 0 deletions exporter/sematextexporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Sematext Exporter
<!-- status autogenerated section -->
| Status | |
| ------------- |-----------|
| Stability | [development]: metrics |
| Distributions | [contrib] |
| Issues | [![Open issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aopen%20label%3Aexporter%2Fsematext%20&label=open&color=orange&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aopen+is%3Aissue+label%3Aexporter%2Fsematext) [![Closed issues](https://img.shields.io/github/issues-search/open-telemetry/opentelemetry-collector-contrib?query=is%3Aissue%20is%3Aclosed%20label%3Aexporter%2Fsematext%20&label=closed&color=blue&logo=opentelemetry)](https://github.com/open-telemetry/opentelemetry-collector-contrib/issues?q=is%3Aclosed+is%3Aissue+label%3Aexporter%2Fsematext) |
| [Code Owners](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/CONTRIBUTING.md#becoming-a-code-owner) | [@Eromosele-SM](https://www.github.com/Eromosele-SM) |

[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 -->

This exporter supports sending metrics to [Sematext Cloud](https://sematext.com/) in Influx line protocol format

## Configuration

The following configuration options are supported:
* `timeout` (default = 5s) Timeout for requests
* `region` Region specifies the Sematext region the user is operating in; must be one of:
* `US`
* `EU`
* `app_token` App token specifies the token of Sematext Monitoring App to which the user wants to send data to.
* `payload_max_lines` (default = 1_000) Maximum number of lines allowed per HTTP POST request
* `payload_max_bytes` (default = 300_000) Maximum number of bytes allowed per HTTP POST request
* `metrics_schema` (default = telegraf-prometheus-v2) The chosen metrics schema to write
* `sending_queue` [details here](https://github.com/open-telemetry/opentelemetry-collector/blob/v0.25.0/exporter/exporterhelper/README.md#configuration)
* `enabled` (default = true)
* `num_consumers` (default = 10) The number of consumers from the queue
* `queue_size` (default = 1000) Maximum number of batches allowed in queue at a given time
* `retry_on_failure` [details here](https://github.com/open-telemetry/opentelemetry-collector/blob/v0.25.0/exporter/exporterhelper/README.md#configuration)
* `enabled` (default = true)
* `initial_interval` (default = 5s) Time to wait after the first failure before retrying
* `max_interval` (default = 30s) Upper bound on backoff interval
* `max_elapsed_time` (default = 120s) Maximum amount of time (including retries) spent trying to send a request/batch

The full list of settings exposed for this exporter are documented in [config.go](config.go).

Example:
```yaml
timeout: 500ms
region: US
retry_on_failure:
enabled: true
initial_interval: 1s
max_interval: 3s
max_elapsed_time: 10s
metrics:
app_token: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
sending_queue:
enabled: true
num_consumers: 3
queue_size: 10
payload_max_lines: 100
payload_max_bytes: 1000
```
59 changes: 59 additions & 0 deletions exporter/sematextexporter/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package sematextexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sematextexporter"

import (
"fmt"
"strings"

"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/exporter/exporterhelper"
)

const appTokenLength = 36

type Config struct {
confighttp.ClientConfig `mapstructure:",squash"`
configretry.BackOffConfig `mapstructure:"retry_on_failure"`
// Region specifies the Sematext region the user is operating in
// Options:
// - EU
// - US
Region string `mapstructure:"region"`
MetricsConfig `mapstructure:"metrics"`
}

type MetricsConfig struct {
// App token is the token of Sematext Monitoring App to which you want to send the metrics.
AppToken string `mapstructure:"app_token"`
// MetricsEndpoint specifies the endpoint for receiving metrics in Sematext
MetricsEndpoint string `mapstructure:"metrics_endpoint"`
QueueSettings exporterhelper.QueueConfig `mapstructure:"sending_queue"`
// MetricsSchema indicates the metrics schema to emit to line protocol.
// Default: telegraf-prometheus-v2
MetricsSchema string `mapstructure:"metrics_schema"`
// PayloadMaxLines is the maximum number of line protocol lines to POST in a single request.
PayloadMaxLines int `mapstructure:"payload_max_lines"`
// PayloadMaxBytes is the maximum number of line protocol bytes to POST in a single request.
PayloadMaxBytes int `mapstructure:"payload_max_bytes"`
}

// Validate checks for invalid or missing entries in the configuration.
func (cfg *Config) Validate() error {
if strings.ToLower(cfg.Region) != "eu" && strings.ToLower(cfg.Region) != "us" && strings.ToLower(cfg.Region) != "custom" {
return fmt.Errorf("invalid region: %s. please use either 'EU' or 'US'", cfg.Region)
}
if len(cfg.AppToken) != appTokenLength {
return fmt.Errorf("invalid app_token: %s. app_token should be 36 characters", cfg.AppToken)
}
if strings.ToLower(cfg.Region) == "eu" {
cfg.MetricsEndpoint = "https://spm-receiver.eu.sematext.com"
}
if strings.ToLower(cfg.Region) == "us" {
cfg.MetricsEndpoint = "https://spm-receiver.sematext.com"
}

return nil
}
84 changes: 84 additions & 0 deletions exporter/sematextexporter/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package sematextexporter

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/configretry"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.opentelemetry.io/collector/exporter/exporterhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sematextexporter/internal/metadata"
)

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

cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

tests := []struct {
id component.ID
expected component.Config
}{
{
id: component.NewIDWithName(metadata.Type, "default-config"),
expected: createDefaultConfig(),
},
{
id: component.NewIDWithName(metadata.Type, "override-config"),
expected: &Config{
ClientConfig: confighttp.ClientConfig{
Timeout: 500 * time.Millisecond,
Headers: map[string]configopaque.String{"User-Agent": "OpenTelemetry -> Sematext"},
},
MetricsConfig: MetricsConfig{
MetricsEndpoint: "https://spm-receiver.sematext.com",
QueueSettings: exporterhelper.QueueConfig{
Enabled: true,
NumConsumers: 3,
QueueSize: 10,
},
AppToken: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
MetricsSchema: "telegraf-prometheus-v2",
PayloadMaxLines: 72,
PayloadMaxBytes: 27,
},

BackOffConfig: configretry.BackOffConfig{
Enabled: true,
InitialInterval: 1 * time.Second,
MaxInterval: 3 * time.Second,
MaxElapsedTime: 10 * time.Second,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: backoff.DefaultMultiplier,
},
Region: "US",
},
},
}

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, sub.Unmarshal(cfg))

assert.NoError(t, component.ValidateConfig(cfg))
assert.Equal(t, tt.expected, cfg)
})
}
}
7 changes: 7 additions & 0 deletions exporter/sematextexporter/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 sematextexporter sends metrics to sematext cloud.
package sematextexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sematextexporter"
71 changes: 71 additions & 0 deletions exporter/sematextexporter/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate mdatagen metadata.yaml

package sematextexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sematextexporter"

import (
"context"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/confighttp"
"go.opentelemetry.io/collector/config/configopaque"
"go.opentelemetry.io/collector/config/configretry"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/pdata/pmetric"

"github.com/influxdata/influxdb-observability/common"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sematextexporter/internal/metadata"
)

const appToken string = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

// NewFactory creates a factory for the Sematext metrics exporter.
func NewFactory() exporter.Factory {
return exporter.NewFactory(
metadata.Type,
createDefaultConfig,
exporter.WithMetrics(createMetricsExporter, metadata.MetricsStability),
)
}

func createDefaultConfig() component.Config {
return &Config{
ClientConfig: confighttp.ClientConfig{
Timeout: 5 * time.Second,
Headers: map[string]configopaque.String{
"User-Agent": "OpenTelemetry -> Sematext",
},
},
MetricsConfig: MetricsConfig{
MetricsSchema: common.MetricsSchemaTelegrafPrometheusV2.String(),
AppToken: appToken,
QueueSettings: exporterhelper.NewDefaultQueueConfig(),
PayloadMaxLines: 1_000,
PayloadMaxBytes: 300_000,
},
BackOffConfig: configretry.NewDefaultBackOffConfig(),
Region: "custom",
}
}

func createMetricsExporter(
ctx context.Context,
set exporter.Settings,
config component.Config,
) (exporter.Metrics, error) {
cfg := config.(*Config)

return exporterhelper.NewMetrics(
ctx,
set,
cfg,
func(_ context.Context, _ pmetric.Metrics) error {
return nil
},
)
}
Loading