Repository: lightstep/otel-launcher-go
Pull request #34 (2020-11-19 00:02:02)

Update launcher to ensure consistency
In this change:
- Adding LS_METRICS_ENABLED to control host/runtime metrics
- Enabling metrics by default


codecov-io: # [Codecov](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34?src=pr&el=h1) Report
> Merging [#34](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34?src=pr&el=desc) (d8f8aac) into [main](https://codecov.io/gh/lightstep/otel-launcher-go/commit/23310e11210710dabb0411a2726b0935a39e3f3b?el=desc) (23310e1) will **increase** coverage by `0.05%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34/graphs/tree.svg?width=650&height=150&src=pr&token=bKAtQGZKIp)](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##             main      #34      +/-   ##
==========================================
+ Coverage   93.36%   93.41%   +0.05%     
==========================================
  Files           3        3              
  Lines         241      243       +2     
==========================================
+ Hits          225      227       +2     
  Misses          8        8              
  Partials        8        8              
```


| [Impacted Files](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [examples/tracing/trace.go](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34/diff?src=pr&el=tree#diff-ZXhhbXBsZXMvdHJhY2luZy90cmFjZS5nbw==) | `100.00% <ø> (ø)` | |
| [launcher/config.go](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34/diff?src=pr&el=tree#diff-bGF1bmNoZXIvY29uZmlnLmdv) | `91.66% <100.00%> (+0.08%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34?src=pr&el=footer). Last update [23310e1...4218cca](https://codecov.io/gh/lightstep/otel-launcher-go/pull/34?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6738fdc..425a296 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,7 +1,8 @@
 ## Unreleased
 
 - Add support for `tracecontext` propagator (#32)
-- Update configuration for baggage propagator from `"cc"` to `"baggage"`
+- Update configuration for baggage propagator from `"cc"` to `"baggage"` (#30)
+- Adding LS_METRICS_ENABLED env variable to control host/runtime metrics (#34)
 
 ## 0.13.0
 
diff --git a/README.md b/README.md
index 9faa12b..c113c36 100644
--- a/README.md
+++ b/README.md
@@ -42,15 +42,14 @@ Additional options
 |WithServiceVersion         |LS_SERVICE_VERSION                 |n       |unknown                         |
 |WithSpanExporterEndpoint   |OTEL_EXPORTER_OTLP_SPAN_ENDPOINT   |n       |ingest.lightstep.com:443        |
 |WithSpanExporterInsecure   |OTEL_EXPORTER_OTLP_SPAN_INSECURE   |n       |false                           |
-|WithMetricExporterEndpoint |OTEL_EXPORTER_OTLP_METRIC_ENDPOINT |n       |-                               |
+|WithMetricExporterEndpoint |OTEL_EXPORTER_OTLP_METRIC_ENDPOINT |n       |ingest.lightstep.com:443        |
 |WithMetricExporterInsecure |OTEL_EXPORTER_OTLP_METRIC_INSECURE |n       |false                           |
 |WithAccessToken            |LS_ACCESS_TOKEN                    |n       |-                               |
 |WithLogLevel               |OTEL_LOG_LEVEL                     |n       |info                            |
 |WithPropagators            |OTEL_PROPAGATORS                   |n       |b3                              |
 |WithResourceAttributes     |OTEL_RESOURCE_ATTRIBUTES           |n       |-                               |
 |WithMetricReportingPeriod  |OTEL_EXPORTER_OTLP_METRIC_PERIOD   |n       |30s                             |
-
-Note that metrics functionality is disabled by default.  Metrics funcionality can be enabled by setting OTEL_EXPORTER_OTLP_METRIC_ENDPOINT to a valid endpoint (e.g. ingest.lightstep.com:443/metrics).
+|WithMetricsEnabled         |LS_METRICS_ENABLED                 |n       |True                            |
 
 ### Principles behind Launcher
 
diff --git a/examples/tracing/trace.go b/examples/tracing/trace.go
index df94351..83ca3bf 100644
--- a/examples/tracing/trace.go
+++ b/examples/tracing/trace.go
@@ -89,7 +89,6 @@ func recordException(ctx context.Context) {
 
 // example of creating a child span
 func createChild(ctx context.Context, tracer trace.Tracer) {
-	// span := otel.SpanFromContext(ctx)
 	_, childSpan := tracer.Start(ctx, "child")
 	defer childSpan.End()
 	fmt.Printf("child span: %v\n", childSpan)
diff --git a/launcher/config.go b/launcher/config.go
index c86cc6b..873a75e 100644
--- a/launcher/config.go
+++ b/launcher/config.go
@@ -130,6 +130,13 @@ func WithMetricReportingPeriod(p time.Duration) Option {
 	}
 }
 
+// WithMetricEnabled configures whether metrics should be enabled
+func WithMetricsEnabled(enabled bool) Option {
+	return func(c *Config) {
+		c.MetricsEnabled = enabled
+	}
+}
+
 type Logger interface {
 	Fatalf(format string, v ...interface{})
 	Debugf(format string, v ...interface{})
@@ -176,6 +183,7 @@ type Config struct {
 	ServiceVersion                 string   `env:"LS_SERVICE_VERSION,default=unknown"`
 	MetricExporterEndpoint         string   `env:"OTEL_EXPORTER_OTLP_METRIC_ENDPOINT"`
 	MetricExporterEndpointInsecure bool     `env:"OTEL_EXPORTER_OTLP_METRIC_INSECURE,default=false"`
+	MetricsEnabled                 bool     `env:"LS_METRICS_ENABLED,default=true"`
 	AccessToken                    string   `env:"LS_ACCESS_TOKEN"`
 	LogLevel                       string   `env:"OTEL_LOG_LEVEL,default=info"`
 	Propagators                    []string `env:"OTEL_PROPAGATORS,default=b3"`
@@ -383,7 +391,7 @@ func setupTracing(c Config) (func() error, error) {
 type setupFunc func(Config) (func() error, error)
 
 func setupMetrics(c Config) (func() error, error) {
-	if c.MetricExporterEndpoint == "" {
+	if !c.MetricsEnabled {
 		c.logger.Debugf("metrics are disabled by configuration: no endpoint set")
 		return nil, nil
 	}


Test patch:
diff --git a/launcher/config_test.go b/launcher/config_test.go
index 766266e..6e79b67 100644
--- a/launcher/config_test.go
+++ b/launcher/config_test.go
@@ -166,6 +166,7 @@ func testEndpointDisabled(t *testing.T, expected string, opts ...Option) {
 		append(opts,
 			WithLogger(logger),
 			WithServiceName("test-service"),
+			WithMetricsEnabled(false),
 		)...,
 	)
 	defer lsOtel.Shutdown()
@@ -202,7 +203,6 @@ func TestValidConfig(t *testing.T) {
 	)
 	defer lsOtel.Shutdown()
 
-	logger.requireContains(t, expectedMetricsDisabledMessage)
 	logger.reset()
 
 	lsOtel = ConfigureOpentelemetry(
@@ -312,6 +312,7 @@ func TestDefaultConfig(t *testing.T) {
 		MetricExporterEndpoint:         "",
 		MetricExporterEndpointInsecure: false,
 		MetricReportingPeriod:          "30s",
+		MetricsEnabled:                 true,
 		AccessToken:                    "",
 		LogLevel:                       "info",
 		Propagators:                    []string{"b3"},
@@ -568,6 +569,7 @@ func setEnvironment() {
 	os.Setenv("OTEL_LOG_LEVEL", "debug")
 	os.Setenv("OTEL_PROPAGATORS", "b3,w3c")
 	os.Setenv("OTEL_RESOURCE_ATTRIBUTES", "service.name=test-service-name-b")
+	os.Setenv("LS_METRICS_ENABLED", "false")
 }
 
 func unsetEnvironment() {
@@ -583,6 +585,7 @@ func unsetEnvironment() {
 		"OTEL_PROPAGATORS",
 		"OTEL_RESOURCE_ATTRIBUTES",
 		"OTEL_EXPORTER_OTLP_METRIC_PERIOD",
+		"LS_METRICS_ENABLED",
 	}
 	for _, envvar := range vars {
 		os.Unsetenv(envvar)