Skip to content

[exporter/Sematext] Second PR of New component: Sematext Exporter #37642

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

Merged
merged 34 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
40ef42e
Add metrics support for Config
AkhigbeEromo Feb 3, 2025
689d903
Add test for config
AkhigbeEromo Feb 3, 2025
df50493
Add basic metrics support
AkhigbeEromo Feb 3, 2025
0bcde39
Add changelog
AkhigbeEromo Feb 3, 2025
ad0cdd6
Fix lint and Unit tests
AkhigbeEromo Feb 3, 2025
2880243
Correct collector module version
AkhigbeEromo Feb 3, 2025
78cc7c0
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Feb 4, 2025
dc30f2e
Update collector module version
AkhigbeEromo Feb 4, 2025
3a4c43e
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Feb 4, 2025
8f3d2f7
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Feb 5, 2025
f40edcf
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Feb 11, 2025
66f5869
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Feb 13, 2025
c3b0fc3
Update collector module version
AkhigbeEromo Feb 13, 2025
2d4a90c
Remove deprecated component
AkhigbeEromo Feb 13, 2025
59a0b09
Update Collector Module Version
AkhigbeEromo Feb 13, 2025
dc4098d
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Feb 13, 2025
d41025c
Remove default metrics endpoint and app token
AkhigbeEromo Feb 18, 2025
05a1524
Move Queue Settings to cover both metrics and logs
AkhigbeEromo Feb 18, 2025
fdb3910
Update exporter/sematextexporter/config.go
AkhigbeEromo Feb 18, 2025
532ae2e
Update README.md
AkhigbeEromo Feb 18, 2025
aeced5d
Remove default for region
AkhigbeEromo Feb 18, 2025
5c3960e
Fix lint issues
AkhigbeEromo Feb 18, 2025
3af5124
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Feb 18, 2025
475e5ce
Update module version
AkhigbeEromo Feb 18, 2025
0a35131
Update README.md
AkhigbeEromo Feb 18, 2025
44ad551
Update config.go
AkhigbeEromo Feb 18, 2025
ca70f1d
Update module version
AkhigbeEromo Feb 18, 2025
2d1396d
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Mar 5, 2025
5ada085
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Mar 5, 2025
40a1ece
Update go.mod
AkhigbeEromo Mar 6, 2025
2253617
Update go.sum
AkhigbeEromo Mar 6, 2025
3c1ee0c
Merge branch 'main' into Otel-Contrib-Second-PR
AkhigbeEromo Mar 6, 2025
c0a9896
Update exporter/sematextexporter/go.sum
AkhigbeEromo Mar 6, 2025
3ecbc2a
Update exporter/sematextexporter/go.mod
AkhigbeEromo Mar 6, 2025
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
27 changes: 27 additions & 0 deletions .chloggen/sematext-metrics.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: new_component

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: sematextexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: basic metrics implementation

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

# (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]
29 changes: 12 additions & 17 deletions exporter/sematextexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,39 @@ This exporter supports sending metrics to [Sematext Cloud](https://sematext.com/

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`
* `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
* `region` Region specifies the Sematext region the user is operating in; must be one of:
* `US`
* `EU`
* `metrics.app_token` specifies the token of the Sematext Monitoring App to which metrics data will be sent. It must be a valid UUID string in the format `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`. For example: `2046e37c-4fac-45f6-831d-922d43fde759`.
* `logs.app_token` specifies the token of the Sematext Logs App to which logs data will be sent. It must be a valid UUID string in the format `xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`. For example: `9604e37c-49ac-49f6-831d-922d43fde759`.
* `metrics.payload_max_lines` (default = 1_000) Maximum number of lines allowed per HTTP POST request
* `metrics.payload_max_bytes` (default = 300_000) Maximum number of bytes allowed per HTTP POST request
* `metrics.metrics_schema` (default = telegraf-prometheus-v2) The chosen metrics schema to write
* `metrics.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


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

Example:
```yaml
timeout: 500ms
region: US
sending_queue:
enabled: true
num_consumers: 3
queue_size: 10
retry_on_failure:
enabled: true
initial_interval: 1s
max_interval: 3s
max_elapsed_time: 10s
region: US
metrics:
app_token: 2064e37c-4fac-45f6-831d-922d43fde759
sending_queue:
enabled: true
num_consumers: 3
queue_size: 10
payload_max_lines: 100
payload_max_bytes: 1000
logs:
app_token: 9064e37c-4gac-49f6-831d-922l43fse759
```
67 changes: 66 additions & 1 deletion exporter/sematextexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,69 @@

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

type Config struct{}
import (
"fmt"
"regexp"
"strings"

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

const (
euRegion = "eu"
usRegion = "us"
euMetricsEndpoint = "https://spm-receiver.eu.sematext.com"
usMetricsEndpoint = "https://spm-receiver.sematext.com"
)

type Config struct {
confighttp.ClientConfig `mapstructure:",squash"`
configretry.BackOffConfig `mapstructure:"retry_on_failure"`
QueueSettings exporterhelper.QueueConfig `mapstructure:"sending_queue"`
// Region specifies the Sematext region the user is operating in
// Options:
// - EU
// - US
Region string `mapstructure:"region"`
// MetricsConfig defines the configuration specific to metrics
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:"-"`
// MetricsSchema indicates the metrics schema to emit to line protocol.
// Default: telegraf-prometheus-v2
MetricsSchema string `mapstructure:"-"`
// 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) != euRegion && strings.ToLower(cfg.Region) != usRegion && strings.ToLower(cfg.Region) != "" {
return fmt.Errorf("invalid region: %s. please use either 'EU' or 'US'", cfg.Region)
}
if !isValidUUID(cfg.MetricsConfig.AppToken) && cfg.MetricsConfig.AppToken != "" {
return fmt.Errorf("invalid metrics app_token: %s. app_token is not a valid UUID", cfg.MetricsConfig.AppToken)
}

if strings.ToLower(cfg.Region) == euRegion {
cfg.MetricsEndpoint = euMetricsEndpoint
}
if strings.ToLower(cfg.Region) == usRegion {
cfg.MetricsEndpoint = usMetricsEndpoint
}

return nil
}

func isValidUUID(uuid string) bool {
const uuidPattern = `^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$`
return regexp.MustCompile(uuidPattern).MatchString(strings.ToLower(uuid))
}
155 changes: 155 additions & 0 deletions exporter/sematextexporter/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package sematextexporter

import (
"os"
"path/filepath"
"strings"
"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/confmap/xconfmap"
"go.opentelemetry.io/collector/exporter/exporterhelper"

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

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

content, err := os.ReadFile(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

contentStr := strings.ReplaceAll(string(content), "<METRICS_APP_TOKEN>", metricsAppToken)

tmpConfigPath := filepath.Join("testdata", "config_tmp.yaml")
err = os.WriteFile(tmpConfigPath, []byte(contentStr), 0o600)
require.NoError(t, err)
defer os.Remove(tmpConfigPath)
cm, err := confmaptest.LoadConf(tmpConfigPath)
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"},
},
QueueSettings: exporterhelper.QueueConfig{
Enabled: true,
NumConsumers: 3,
QueueSize: 10,
},
MetricsConfig: MetricsConfig{
MetricsEndpoint: usMetricsEndpoint,
AppToken: metricsAppToken,
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: usRegion,
},
},
}

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, xconfmap.Validate(cfg))
assert.Equal(t, tt.expected, cfg)
})
}
}

func TestConfigValidation(t *testing.T) {
tests := []struct {
name string
config *Config
expectError bool
}{
{
name: "Valid configuration 1",
config: &Config{
Region: usRegion,
MetricsConfig: MetricsConfig{
AppToken: metricsAppToken,
},
},
expectError: false,
},
{
name: "Valid configuration 2",
config: &Config{
Region: euRegion,
MetricsConfig: MetricsConfig{
AppToken: metricsAppToken,
},
},
expectError: false,
},
{
name: "Invalid region",
config: &Config{
Region: "ASIA",
MetricsConfig: MetricsConfig{
AppToken: metricsAppToken,
},
},
expectError: true,
},
{
name: "Invalid metrics AppToken",
config: &Config{
Region: usRegion,
MetricsConfig: MetricsConfig{
AppToken: "short-token",
},
},
expectError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if tt.expectError {
assert.Error(t, err, "Expected an error for invalid configuration")
} else {
assert.NoError(t, err, "Expected no error for valid configuration")
}
})
}
}
24 changes: 23 additions & 1 deletion exporter/sematextexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ package sematextexporter // import "github.com/open-telemetry/opentelemetry-coll

import (
"context"
"time"

"github.com/google/uuid"
"github.com/influxdata/influxdb-observability/common"
"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/plog"
Expand All @@ -17,6 +23,8 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/sematextexporter/internal/metadata"
)

var metricsAppToken = uuid.NewString()

// NewFactory creates a factory for the Sematext metrics exporter.
func NewFactory() exporter.Factory {
return exporter.NewFactory(
Expand All @@ -28,7 +36,21 @@ func NewFactory() exporter.Factory {
}

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

Expand Down
Loading
Loading