| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package confmap_test |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/confmap" |
| 12 |
) |
| 13 |
|
| 14 |
// mockFileProvider simulates the standard file provider behavior. |
| 15 |
// In typical usage, it reads a single file as the root configuration. |
| 16 |
// This mock implementation always returns a fixed configuration: |
| 17 |
// { "my-config": "${expand:to-expand}" } |
| 18 |
type mockFileProvider struct{} |
| 19 |
|
| 20 |
func (d mockFileProvider) Retrieve(_ context.Context, uri string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 21 |
expectedURI := "file:mock-file" |
| 22 |
if uri != expectedURI { |
| 23 |
panic("should not happen, the uri is expected to be " + expectedURI + " for mockFileProvider") |
| 24 |
} |
| 25 |
return confmap.NewRetrieved(map[string]any{ |
| 26 |
"my-config": "${expand:to-expand}", |
| 27 |
}) |
| 28 |
} |
| 29 |
|
| 30 |
func (d mockFileProvider) Scheme() string { |
| 31 |
return "file" |
| 32 |
} |
| 33 |
|
| 34 |
func (d mockFileProvider) Shutdown(_ context.Context) error { |
| 35 |
return nil |
| 36 |
} |
| 37 |
|
| 38 |
// mockExpandProvider simulates a typical inline expansion provider. |
| 39 |
// In configurations, you can use expressions like ${SCHEMA:VALUE}, |
| 40 |
// where the provider associated with SCHEMA is responsible for resolving the value. |
| 41 |
type mockExpandProvider struct{} |
| 42 |
|
| 43 |
func (m mockExpandProvider) Retrieve(_ context.Context, uri string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 44 |
expectedURI := "expand:to-expand" |
| 45 |
if uri != expectedURI { |
| 46 |
panic("should not happen, the uri is expected to be " + expectedURI + " for mockExpandProvider") |
| 47 |
} |
| 48 |
return confmap.NewRetrieved("expanded") |
| 49 |
} |
| 50 |
|
| 51 |
func (m mockExpandProvider) Scheme() string { |
| 52 |
return "expand" |
| 53 |
} |
| 54 |
|
| 55 |
func (m mockExpandProvider) Shutdown(_ context.Context) error { |
| 56 |
return nil |
| 57 |
} |
| 58 |
|
| 59 |
// mockUpperCaseConverter transforms the value of the `my-config` field in the configuration to uppercase. |
| 60 |
type mockUpperCaseConverter struct{} |
| 61 |
|
| 62 |
func (m mockUpperCaseConverter) Convert(_ context.Context, conf *confmap.Conf) error { |
| 63 |
currentValue := conf.Get("my-config") |
| 64 |
expectedValue := "expanded" |
| 65 |
if currentValue != expectedValue { |
| 66 |
panic("should not happen, the value for converter should always be " + expectedValue + " for mockUpperCaseConverter") |
| 67 |
} |
| 68 |
upperCaseConf := confmap.NewFromStringMap(map[string]any{ |
| 69 |
"my-config": strings.ToUpper(currentValue.(string)), |
| 70 |
}) |
| 71 |
if conf.Merge(upperCaseConf) != nil { |
| 72 |
panic("merge failed, this should not happen in this example.") |
| 73 |
} |
| 74 |
return nil |
| 75 |
} |
| 76 |
|
| 77 |
func Example() { |
| 78 |
resolver, err := confmap.NewResolver(confmap.ResolverSettings{ |
| 79 |
URIs: []string{"file:mock-file"}, |
| 80 |
ProviderFactories: []confmap.ProviderFactory{ |
| 81 |
confmap.NewProviderFactory(func(_ confmap.ProviderSettings) confmap.Provider { |
| 82 |
return &mockFileProvider{} |
| 83 |
}), |
| 84 |
confmap.NewProviderFactory(func(_ confmap.ProviderSettings) confmap.Provider { |
| 85 |
return &mockExpandProvider{} |
| 86 |
}), |
| 87 |
}, |
| 88 |
ConverterFactories: []confmap.ConverterFactory{ |
| 89 |
confmap.NewConverterFactory(func(_ confmap.ConverterSettings) confmap.Converter { |
| 90 |
return &mockUpperCaseConverter{} |
| 91 |
}), |
| 92 |
}, |
| 93 |
}) |
| 94 |
if err != nil { |
| 95 |
panic(err) |
| 96 |
} |
| 97 |
|
| 98 |
conf, err := resolver.Resolve(context.Background()) |
| 99 |
if err != nil { |
| 100 |
panic(err) |
| 101 |
} |
| 102 |
fmt.Printf("Configuration contains the following:\nmy-config: %s", conf.Get("my-config")) |
| 103 |
// Output: Configuration contains the following: |
| 104 |
// my-config: EXPANDED |
| 105 |
} |