mirror of
https://github.com/hanzoai/otel-collector.git
synced 2026-10-30 18:26:25 +00:00
- Go module: github.com/SigNoz/signoz-otel-collector → github.com/hanzoai/otel-collector - All signoz-prefixed packages → o11y-prefixed - Docker: signoz/* → ghcr.io/hanzoai/otel-collector - ClickHouse exporter → Hanzo Datastore - Env vars: SIGNOZ_* → HANZO_* - Directory renames for all processor/receiver/exporter packages
179 lines
4.5 KiB
Go
179 lines
4.5 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package o11ytransformprocessor
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"go.opentelemetry.io/collector/component"
|
|
"go.opentelemetry.io/collector/confmap/confmaptest"
|
|
"go.opentelemetry.io/collector/confmap/xconfmap"
|
|
|
|
"github.com/hanzoai/otel-collector/processor/o11ytransformprocessor/internal/common"
|
|
"github.com/hanzoai/otel-collector/processor/o11ytransformprocessor/internal/metadata"
|
|
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
|
|
)
|
|
|
|
func TestLoadConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
id component.ID
|
|
expected component.Config
|
|
errors []error
|
|
}{
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, ""),
|
|
expected: &Config{
|
|
ErrorMode: ottl.PropagateError,
|
|
TraceStatements: []common.ContextStatements{
|
|
{
|
|
Context: "span",
|
|
Statements: []string{
|
|
`set(name, "bear") where attributes["http.path"] == "/animal"`,
|
|
`keep_keys(attributes, ["http.method", "http.path"])`,
|
|
},
|
|
},
|
|
{
|
|
Context: "resource",
|
|
Statements: []string{
|
|
`set(attributes["name"], "bear")`,
|
|
},
|
|
},
|
|
},
|
|
MetricStatements: []common.ContextStatements{
|
|
{
|
|
Context: "datapoint",
|
|
Statements: []string{
|
|
`set(metric.name, "bear") where attributes["http.path"] == "/animal"`,
|
|
`keep_keys(attributes, ["http.method", "http.path"])`,
|
|
},
|
|
},
|
|
{
|
|
Context: "resource",
|
|
Statements: []string{
|
|
`set(attributes["name"], "bear")`,
|
|
},
|
|
},
|
|
},
|
|
LogStatements: []common.ContextStatements{
|
|
{
|
|
Context: "log",
|
|
Statements: []string{
|
|
`set(body, "bear") where attributes["http.path"] == "/animal"`,
|
|
`keep_keys(attributes, ["http.method", "http.path"])`,
|
|
},
|
|
},
|
|
{
|
|
Context: "resource",
|
|
Statements: []string{
|
|
`set(attributes["name"], "bear")`,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, "ignore_errors"),
|
|
expected: &Config{
|
|
ErrorMode: ottl.IgnoreError,
|
|
TraceStatements: []common.ContextStatements{
|
|
{
|
|
Context: "resource",
|
|
Statements: []string{
|
|
`set(attributes["name"], "bear")`,
|
|
},
|
|
},
|
|
},
|
|
MetricStatements: []common.ContextStatements{},
|
|
LogStatements: []common.ContextStatements{},
|
|
},
|
|
},
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, "bad_syntax_trace"),
|
|
},
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, "unknown_function_trace"),
|
|
},
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, "bad_syntax_metric"),
|
|
},
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, "unknown_function_metric"),
|
|
},
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, "bad_syntax_log"),
|
|
},
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, "unknown_function_log"),
|
|
},
|
|
{
|
|
id: component.NewIDWithName(metadata.Type, "bad_syntax_multi_signal"),
|
|
errors: []error{
|
|
errors.New("unexpected token \"where\""),
|
|
errors.New("unexpected token \"attributes\""),
|
|
errors.New("unexpected token \"none\""),
|
|
}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.id.String(), func(t *testing.T) {
|
|
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
|
|
assert.NoError(t, err)
|
|
|
|
factory := NewFactory()
|
|
cfg := factory.CreateDefaultConfig()
|
|
|
|
sub, err := cm.Sub(tt.id.String())
|
|
assert.NoError(t, err)
|
|
assert.NoError(t, sub.Unmarshal(cfg))
|
|
|
|
if tt.expected == nil {
|
|
err = xconfmap.Validate(cfg)
|
|
assert.Error(t, err)
|
|
|
|
if len(tt.errors) > 0 {
|
|
for _, expectedErr := range tt.errors {
|
|
assert.ErrorContains(t, err, expectedErr.Error())
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
assert.NoError(t, xconfmap.Validate(cfg))
|
|
assert.Equal(t, tt.expected, cfg)
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_UnknownContextID(t *testing.T) {
|
|
id := component.NewIDWithName(metadata.Type, "unknown_context")
|
|
|
|
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
|
|
assert.NoError(t, err)
|
|
|
|
factory := NewFactory()
|
|
cfg := factory.CreateDefaultConfig()
|
|
|
|
sub, err := cm.Sub(id.String())
|
|
assert.NoError(t, err)
|
|
assert.Error(t, sub.Unmarshal(cfg))
|
|
}
|
|
|
|
func Test_UnknownErrorMode(t *testing.T) {
|
|
id := component.NewIDWithName(metadata.Type, "unknown_error_mode")
|
|
|
|
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
|
|
assert.NoError(t, err)
|
|
|
|
factory := NewFactory()
|
|
cfg := factory.CreateDefaultConfig()
|
|
|
|
sub, err := cm.Sub(id.String())
|
|
assert.NoError(t, err)
|
|
assert.Error(t, sub.Unmarshal(cfg))
|
|
}
|