Skip to content

Commit

Permalink
add support for env var configuration to otlp/gRPC (open-telemetry#1811)
Browse files Browse the repository at this point in the history
* move options to `otlpconfig` internal package

* add support for env configs on otel/gRPC

* remove duplicate code

* refactor options

* format imports

* move marshal option to oltphttp

* clone tls configs and tests grpc certificates

* add more context to http errors

* add todo

* add entry to changelog

* update changelog with pr number

* apply suggestions

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
paivagustavo and MrAlias committed Apr 16, 2021
1 parent d616df6 commit a2cecb6
Show file tree
Hide file tree
Showing 14 changed files with 1,168 additions and 751 deletions.
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

0 comments on commit a2cecb6

Please sign in to comment.