forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: add support for parsing env variables in configuration
This replaces the last bit of functionality that was opened in open-telemetry#4826 to support env variable replacement. Pulled the envprovider.go code from https://github.com/open-telemetry/opentelemetry-collector/blob/main/confmap/provider/envprovider/provider.go Signed-off-by: Alex Boten <[email protected]>
- Loading branch information
Showing
4 changed files
with
515 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package config // import "go.opentelemetry.io/contrib/config" | ||
|
||
import ( | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
const ValidationPattern = `^[a-zA-Z_][a-zA-Z0-9_]*$` | ||
|
||
var validationRegexp = regexp.MustCompile(ValidationPattern) | ||
|
||
func replaceEnvVar(uri string) []byte { | ||
envVarName, defaultValuePtr := parseEnvVarURI(uri) | ||
if !validationRegexp.MatchString(envVarName) { | ||
return nil | ||
} | ||
|
||
val, exists := os.LookupEnv(envVarName) | ||
if !exists { | ||
if defaultValuePtr != nil { | ||
val = *defaultValuePtr | ||
} | ||
} | ||
return []byte(val) | ||
} | ||
|
||
func parseEnvVarURI(uri string) (string, *string) { | ||
const defaultSuffix = ":-" | ||
if strings.Contains(uri, defaultSuffix) { | ||
parts := strings.SplitN(uri, defaultSuffix, 2) | ||
return parts[0], &parts[1] | ||
} | ||
return uri, nil | ||
} |
Oops, something went wrong.