123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package datasetexporter
- import (
- "fmt"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "go.opentelemetry.io/collector/confmap"
- "go.opentelemetry.io/collector/exporter/exporterhelper"
- )
- func TestConfigUnmarshalUnknownAttributes(t *testing.T) {
- f := NewFactory()
- config := f.CreateDefaultConfig().(*Config)
- configMap := confmap.NewFromStringMap(map[string]any{
- "dataset_url": "https://example.com",
- "api_key": "secret",
- "unknown_attribute": "some value",
- })
- err := config.Unmarshal(configMap)
- unmarshalErr := fmt.Errorf("1 error(s) decoding:\n\n* '' has invalid keys: unknown_attribute")
- expectedError := fmt.Errorf("cannot unmarshal config: %w", unmarshalErr)
- assert.Equal(t, expectedError.Error(), err.Error())
- }
- func TestConfigUseDefaults(t *testing.T) {
- f := NewFactory()
- config := f.CreateDefaultConfig().(*Config)
- configMap := confmap.NewFromStringMap(map[string]any{
- "dataset_url": "https://example.com",
- "api_key": "secret",
- })
- err := config.Unmarshal(configMap)
- assert.Nil(t, err)
- assert.Equal(t, "https://example.com", config.DatasetURL)
- assert.Equal(t, "secret", string(config.APIKey))
- assert.Equal(t, bufferMaxLifetime, config.MaxLifetime)
- assert.Equal(t, logsExportResourceInfoDefault, config.LogsSettings.ExportResourceInfo)
- assert.Equal(t, logsExportResourcePrefixDefault, config.LogsSettings.ExportResourcePrefix)
- assert.Equal(t, logsExportScopeInfoDefault, config.LogsSettings.ExportScopeInfo)
- assert.Equal(t, logsExportScopePrefixDefault, config.LogsSettings.ExportScopePrefix)
- assert.Equal(t, logsDecomposeComplexMessageFieldDefault, config.LogsSettings.DecomposeComplexMessageField)
- assert.Equal(t, exportSeparatorDefault, config.LogsSettings.exportSettings.ExportSeparator)
- assert.Equal(t, exportDistinguishingSuffix, config.LogsSettings.exportSettings.ExportDistinguishingSuffix)
- assert.Equal(t, exportSeparatorDefault, config.TracesSettings.exportSettings.ExportSeparator)
- assert.Equal(t, exportDistinguishingSuffix, config.TracesSettings.exportSettings.ExportDistinguishingSuffix)
- }
- func TestConfigValidate(t *testing.T) {
- tests := []struct {
- name string
- config Config
- expected error
- }{
- {
- name: "valid config",
- config: Config{
- DatasetURL: "https://example.com",
- APIKey: "secret",
- BufferSettings: BufferSettings{
- MaxLifetime: 123 * time.Millisecond,
- },
- },
- expected: nil,
- },
- {
- name: "missing api_key",
- config: Config{
- DatasetURL: "https://example.com",
- BufferSettings: BufferSettings{
- MaxLifetime: bufferMaxLifetime,
- },
- },
- expected: fmt.Errorf("api_key is required"),
- },
- {
- name: "missing dataset_url",
- config: Config{
- APIKey: "1234",
- BufferSettings: BufferSettings{
- MaxLifetime: bufferMaxLifetime,
- },
- },
- expected: fmt.Errorf("dataset_url is required"),
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- err := tt.config.Validate()
- if err == nil {
- assert.Nil(t, tt.expected, tt.name)
- } else {
- assert.Equal(t, tt.expected.Error(), err.Error(), tt.name)
- }
- })
- }
- }
- func TestConfigString(t *testing.T) {
- config := Config{
- DatasetURL: "https://example.com",
- APIKey: "secret",
- Debug: true,
- BufferSettings: BufferSettings{
- MaxLifetime: 123,
- GroupBy: []string{"field1", "field2"},
- },
- TracesSettings: TracesSettings{
- exportSettings: exportSettings{
- ExportSeparator: "TTT",
- ExportDistinguishingSuffix: "UUU",
- },
- },
- LogsSettings: LogsSettings{
- ExportResourceInfo: true,
- ExportResourcePrefix: "AAA",
- ExportScopeInfo: true,
- ExportScopePrefix: "BBB",
- DecomposeComplexMessageField: true,
- DecomposedComplexMessagePrefix: "EEE",
- exportSettings: exportSettings{
- ExportSeparator: "CCC",
- ExportDistinguishingSuffix: "DDD",
- },
- },
- ServerHostSettings: ServerHostSettings{
- ServerHost: "foo-bar",
- UseHostName: false,
- },
- RetrySettings: exporterhelper.NewDefaultRetrySettings(),
- QueueSettings: exporterhelper.NewDefaultQueueSettings(),
- TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
- }
- assert.Equal(t,
- "DatasetURL: https://example.com; APIKey: [REDACTED] (6); Debug: true; BufferSettings: {MaxLifetime:123ns GroupBy:[field1 field2] RetryInitialInterval:0s RetryMaxInterval:0s RetryMaxElapsedTime:0s RetryShutdownTimeout:0s}; LogsSettings: {ExportResourceInfo:true ExportResourcePrefix:AAA ExportScopeInfo:true ExportScopePrefix:BBB DecomposeComplexMessageField:true DecomposedComplexMessagePrefix:EEE exportSettings:{ExportSeparator:CCC ExportDistinguishingSuffix:DDD}}; TracesSettings: {exportSettings:{ExportSeparator:TTT ExportDistinguishingSuffix:UUU}}; ServerHostSettings: {UseHostName:false ServerHost:foo-bar}; RetrySettings: {Enabled:true InitialInterval:5s RandomizationFactor:0.5 Multiplier:1.5 MaxInterval:30s MaxElapsedTime:5m0s}; QueueSettings: {Enabled:true NumConsumers:10 QueueSize:1000 StorageID:<nil>}; TimeoutSettings: {Timeout:5s}",
- config.String(),
- )
- }
- func TestConfigUseProvidedExportResourceInfoValue(t *testing.T) {
- f := NewFactory()
- config := f.CreateDefaultConfig().(*Config)
- configMap := confmap.NewFromStringMap(map[string]any{
- "dataset_url": "https://example.com",
- "api_key": "secret",
- "logs": map[string]any{
- "export_resource_info_on_event": true,
- },
- })
- err := config.Unmarshal(configMap)
- assert.Nil(t, err)
- assert.Equal(t, true, config.LogsSettings.ExportResourceInfo)
- }
- func TestConfigUseProvidedExportScopeInfoValue(t *testing.T) {
- f := NewFactory()
- config := f.CreateDefaultConfig().(*Config)
- configMap := confmap.NewFromStringMap(map[string]any{
- "dataset_url": "https://example.com",
- "api_key": "secret",
- "logs": map[string]any{
- "export_scope_info_on_event": false,
- },
- })
- err := config.Unmarshal(configMap)
- assert.Nil(t, err)
- assert.Equal(t, false, config.LogsSettings.ExportScopeInfo)
- }
|