Skip to content
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

add support for env var configuration to otlp/gRPC #1811

Merged
merged 13 commits into from
Apr 16, 2021
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Adds test to check BatchSpanProcessor ignores `OnEnd` and `ForceFlush` post `Shutdown`. (#1772)
- Option `ExportTimeout` was added to batch span processor. (#1755)
- Adds semantic conventions for exceptions. (#1492)
- Added support for configuring OTLP/HTTP Endpoints, Headers, Compression and Timeout via the Environment Variables. (#1758)
- Added support for configuring OTLP/HTTP and OTLP/gRPC Endpoints, TLS Certificates, Headers, Compression and Timeout via Environment Variables. (#1758, #1769 and #1811)
- `OTEL_EXPORTER_OTLP_ENDPOINT`
- `OTEL_EXPORTER_OTLP_TRACES_ENDPOINT`
- `OTEL_EXPORTER_OTLP_METRICS_ENDPOINT`
Expand All @@ -29,7 +29,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `OTEL_EXPORTER_OTLP_TIMEOUT`
- `OTEL_EXPORTER_OTLP_TRACES_TIMEOUT`
- `OTEL_EXPORTER_OTLP_METRICS_TIMEOUT`
- Added support for configuring OTLP/HTTP TLS Certificates via the Environment Variables. (#1769)
- `OTEL_EXPORTER_OTLP_CERTIFICATE`
- `OTEL_EXPORTER_OTLP_TRACES_CERTIFICATE`
- `OTEL_EXPORTER_OTLP_METRICS_CERTIFICATE`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package otlphttp
package otlpconfig

import (
"crypto/tls"
Expand All @@ -24,37 +24,50 @@ import (
"strings"
"time"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp"

"go.opentelemetry.io/otel/exporters/otlp/internal/otlpconfig"
"go.opentelemetry.io/otel"
)

func applyEnvConfigs(cfg *config) {
e := envOptionsReader{
getEnv: os.Getenv,
readFile: ioutil.ReadFile,
func ApplyGRPCEnvConfigs(cfg *Config) {
e := EnvOptionsReader{
GetEnv: os.Getenv,
ReadFile: ioutil.ReadFile,
}

opts := e.getOptionsFromEnv()
for _, opt := range opts {
opt.Apply(cfg)
e.ApplyGRPCEnvConfigs(cfg)
}

func ApplyHTTPEnvConfigs(cfg *Config) {
e := EnvOptionsReader{
GetEnv: os.Getenv,
ReadFile: ioutil.ReadFile,
}

e.ApplyHTTPEnvConfigs(cfg)
}

type EnvOptionsReader struct {
GetEnv func(string) string
ReadFile func(filename string) ([]byte, error)
}

type envOptionsReader struct {
getEnv func(string) string
readFile func(filename string) ([]byte, error)
func (e *EnvOptionsReader) ApplyHTTPEnvConfigs(cfg *Config) {
opts := e.GetOptionsFromEnv()
for _, opt := range opts {
opt.ApplyHTTPOption(cfg)
}
}

func (e *envOptionsReader) applyEnvConfigs(cfg *config) {
opts := e.getOptionsFromEnv()
func (e *EnvOptionsReader) ApplyGRPCEnvConfigs(cfg *Config) {
opts := e.GetOptionsFromEnv()
for _, opt := range opts {
opt.Apply(cfg)
opt.ApplyGRPCOption(cfg)
}
}

func (e *envOptionsReader) getOptionsFromEnv() []Option {
var opts []Option
func (e *EnvOptionsReader) GetOptionsFromEnv() []GenericOption {
var opts []GenericOption

// Endpoint
if v, ok := e.getEnvValue("ENDPOINT"); ok {
Expand Down Expand Up @@ -132,28 +145,28 @@ func (e *envOptionsReader) getOptionsFromEnv() []Option {
return opts
}

// getEnvValue gets an OTLP environment variable value of the specified key using the getEnv function.
// getEnvValue gets an OTLP environment variable value of the specified key using the GetEnv function.
// This function already prepends the OTLP prefix to all key lookup.
func (e *envOptionsReader) getEnvValue(key string) (string, bool) {
v := strings.TrimSpace(e.getEnv(fmt.Sprintf("OTEL_EXPORTER_OTLP_%s", key)))
func (e *EnvOptionsReader) getEnvValue(key string) (string, bool) {
v := strings.TrimSpace(e.GetEnv(fmt.Sprintf("OTEL_EXPORTER_OTLP_%s", key)))
return v, v != ""
}

func (e *envOptionsReader) readTLSConfig(path string) (*tls.Config, error) {
b, err := e.readFile(path)
func (e *EnvOptionsReader) readTLSConfig(path string) (*tls.Config, error) {
b, err := e.ReadFile(path)
if err != nil {
return nil, err
}
return otlpconfig.CreateTLSConfig(b)
return CreateTLSConfig(b)
}

func stringToCompression(value string) Compression {
func stringToCompression(value string) otlp.Compression {
switch value {
case "gzip":
return GzipCompression
return otlp.GzipCompression
}

return NoCompression
return otlp.NoCompression
}

func stringToHeader(value string) map[string]string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package otlphttp
package otlpconfig

import (
"reflect"
Expand Down
Loading