Skip to content

Commit

Permalink
add tests; remove periodic.go
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacd committed Jun 2, 2022
1 parent 8e0afb9 commit d1ff7b3
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 157 deletions.
23 changes: 17 additions & 6 deletions examples/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,29 @@ func main() {
ctx := context.Background()
meter := metricglobal.Meter("testing")

// This example shows 1 instrument each for (UpDown)?(SumObserver|Counter)
// The two monotonic instruments of these have a fixed rate of +1.
// The two non-monotonic instruments of these have a fixed rate of -1.
// There are 2 example ValueObserver instruments (one a Gaussian, one a Sine wave).
//
// TODO: ValueRecorder examples.
// This example shows 1 instrument each for synchronous and
// asynchronous Counter and UpDownCounter. The two monotonic
// instruments of these have a fixed rate of +1. The two
// non-monotonic instruments of these have a fixed rate of -1.
// There are 2 example Gauge instruments (one a Gaussian, one
// a Sine wave), and one Histogram.

c1, _ := meter.SyncInt64().Counter(prefix + "counter")
c2, _ := meter.SyncInt64().UpDownCounter(prefix + "updowncounter")
hist, _ := meter.SyncFloat64().Histogram(prefix + "histogram")
go func() {
for {
c1.Add(ctx, 1)
c2.Add(ctx, -1)

secs := float64(time.Now().UnixNano()) / float64(time.Second)
mult := math.Sin(secs / (40 * math.Pi))
mult *= mult

for i := 0; i < 10000; i++ {
hist.Record(ctx, mult*(100+rand.NormFloat64()*100))
}

time.Sleep(time.Second)
}
}()
Expand Down Expand Up @@ -114,6 +124,7 @@ func main() {
func(ctx context.Context) {
secs := float64(time.Now().UnixNano()) / float64(time.Second)

sineWave.Observe(ctx, math.Sin(secs/(50*math.Pi)), attribute.String("period", "fastest"))
sineWave.Observe(ctx, math.Sin(secs/(200*math.Pi)), attribute.String("period", "fast"))
sineWave.Observe(ctx, math.Sin(secs/(1000*math.Pi)), attribute.String("period", "regular"))
sineWave.Observe(ctx, math.Sin(secs/(5000*math.Pi)), attribute.String("period", "slow"))
Expand Down
21 changes: 16 additions & 5 deletions launcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ func WithMetricReportingPeriod(p time.Duration) Option {
}
}

// WithMetricTemporalityPreference controls the temporality preference
// used for Counter and Histogram (only not for UpDownCounter, which
// ignores this preference for specified reasons).
func WithMetricTemporalityPreference(prefName string) Option {
return func(c *Config) {
c.MetricTemporalityPreference = prefName
}
}

// WithMetricEnabled configures whether metrics should be enabled
func WithMetricsEnabled(enabled bool) Option {
return func(c *Config) {
Expand Down Expand Up @@ -199,6 +208,7 @@ type Config struct {
LogLevel string `env:"OTEL_LOG_LEVEL,default=info"`
Propagators []string `env:"OTEL_PROPAGATORS,default=b3"`
MetricReportingPeriod string `env:"OTEL_EXPORTER_OTLP_METRIC_PERIOD,default=30s"`
MetricTemporalityPreference string `env:"OTEL_EXPORTER_OTLP_METRIC_TEMPORALITY_PREFERENCE,default=cumulative"`
ResourceAttributes map[string]string
Resource *resource.Resource
logger Logger
Expand Down Expand Up @@ -361,11 +371,12 @@ func setupMetrics(c Config) (func() error, error) {
return nil, nil
}
return pipelines.NewMetricsPipeline(pipelines.PipelineConfig{
Endpoint: c.MetricExporterEndpoint,
Insecure: c.MetricExporterEndpointInsecure,
Headers: c.Headers,
Resource: c.Resource,
ReportingPeriod: c.MetricReportingPeriod,
Endpoint: c.MetricExporterEndpoint,
Insecure: c.MetricExporterEndpointInsecure,
Headers: c.Headers,
Resource: c.Resource,
ReportingPeriod: c.MetricReportingPeriod,
TemporalityPreference: c.MetricTemporalityPreference,
})
}

Expand Down
6 changes: 6 additions & 0 deletions launcher/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func TestDefaultConfig(t *testing.T) {
MetricExporterEndpointInsecure: false,
MetricReportingPeriod: "30s",
MetricsEnabled: true,
MetricTemporalityPreference: "cumulative",
LogLevel: "info",
Propagators: []string{"b3"},
Resource: resource.NewWithAttributes(semconv.SchemaURL, attributes...),
Expand Down Expand Up @@ -349,6 +350,7 @@ func TestEnvironmentVariables(t *testing.T) {
MetricExporterEndpoint: "metrics-url",
MetricExporterEndpointInsecure: true,
MetricReportingPeriod: "30s",
MetricTemporalityPreference: "delta",
LogLevel: "debug",
Propagators: []string{"b3", "w3c"},
Resource: resource.NewWithAttributes(semconv.SchemaURL, attributes...),
Expand All @@ -372,6 +374,7 @@ func TestConfigurationOverrides(t *testing.T) {
WithSpanExporterInsecure(false),
WithMetricExporterEndpoint("override-metrics-url"),
WithMetricExporterInsecure(false),
WithMetricTemporalityPreference("stateless"),
WithLogLevel("info"),
WithLogger(logger),
WithErrorHandler(handler),
Expand All @@ -395,6 +398,7 @@ func TestConfigurationOverrides(t *testing.T) {
MetricExporterEndpoint: "override-metrics-url",
MetricExporterEndpointInsecure: false,
MetricReportingPeriod: "30s",
MetricTemporalityPreference: "stateless",
Headers: map[string]string{"lightstep-access-token": "override-access-token"},
LogLevel: "info",
Propagators: []string{"b3"},
Expand Down Expand Up @@ -600,6 +604,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("OTEL_EXPORTER_OTLP_METRIC_TEMPORALITY_PREFERENCE", "delta")
os.Setenv("LS_METRICS_ENABLED", "false")
}

Expand All @@ -616,6 +621,7 @@ func unsetEnvironment() {
"OTEL_PROPAGATORS",
"OTEL_RESOURCE_ATTRIBUTES",
"OTEL_EXPORTER_OTLP_METRIC_PERIOD",
"OTEL_EXPORTER_OTLP_METRIC_TEMPORALITY_PREFERENCE",
"LS_METRICS_ENABLED",
}
for _, envvar := range vars {
Expand Down
121 changes: 0 additions & 121 deletions lightstep/sdk/metric/periodic.go

This file was deleted.

7 changes: 6 additions & 1 deletion pipelines/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package pipelines

import "go.opentelemetry.io/otel/sdk/resource"
import (
"go.opentelemetry.io/otel/sdk/resource"
)

type PipelineConfig struct {
Endpoint string
Expand All @@ -23,6 +25,9 @@ type PipelineConfig struct {
Resource *resource.Resource
ReportingPeriod string
Propagators []string

// TemporalityPreference is one of "cumulative", "delta", or "stateless"
TemporalityPreference string
}

type PipelineSetupFunc func(PipelineConfig) (func() error, error)
Loading

0 comments on commit d1ff7b3

Please sign in to comment.