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

apmotel: follow APM OTel spec and prefer delta temporality #1437

Merged
merged 8 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions module/apmotel/gatherer_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package apmotel // import "go.elastic.co/apm/module/apmotel/v2"
import (
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)

var customHistogramBoundaries = []float64{
Expand All @@ -36,13 +37,15 @@ var customHistogramBoundaries = []float64{

type gathererConfig struct {
aggregation metric.AggregationSelector
temporality metric.TemporalitySelector
}

type GathererOption func(gathererConfig) gathererConfig

func newGathererConfig(opts ...GathererOption) gathererConfig {
cfg := gathererConfig{
aggregation: customAggregationSelector,
aggregation: defaultAggregationSelector,
temporality: defaultTemporalitySelector,
}
for _, opt := range opts {
cfg = opt(cfg)
Expand All @@ -52,8 +55,13 @@ func newGathererConfig(opts ...GathererOption) gathererConfig {
}

func (cfg gathererConfig) manualReaderOptions() []metric.ManualReaderOption {
opts := []metric.ManualReaderOption{}
opts = append(opts, metric.WithAggregationSelector(cfg.aggregation))
var opts []metric.ManualReaderOption
if cfg.aggregation != nil {
opts = append(opts, metric.WithAggregationSelector(cfg.aggregation))
}
if cfg.temporality != nil {
opts = append(opts, metric.WithTemporalitySelector(cfg.temporality))
}
return opts
}

Expand All @@ -67,7 +75,7 @@ func WithAggregationSelector(agg metric.AggregationSelector) GathererOption {
}
}

func customAggregationSelector(ik metric.InstrumentKind) aggregation.Aggregation {
func defaultAggregationSelector(ik metric.InstrumentKind) aggregation.Aggregation {
switch ik {
case metric.InstrumentKindHistogram:
return aggregation.ExplicitBucketHistogram{
Expand All @@ -78,3 +86,17 @@ func customAggregationSelector(ik metric.InstrumentKind) aggregation.Aggregation
return metric.DefaultAggregationSelector(ik)
}
}

// WithTemporalitySelector configure the Aggregation Selector the exporter will
// use. If no AggregationSelector is provided the DefaultAggregationSelector is
// used.
func WithTemporalitySelector(temporality metric.TemporalitySelector) GathererOption {
return func(cfg gathererConfig) gathererConfig {
cfg.temporality = temporality
return cfg
}
}

func defaultTemporalitySelector(_ metric.InstrumentKind) metricdata.Temporality {
return metricdata.DeltaTemporality
}
24 changes: 22 additions & 2 deletions module/apmotel/gatherer_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ import (
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/aggregation"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
)

func TestNewGathererConfig(t *testing.T) {
aggregationSelector := func(metric.InstrumentKind) aggregation.Aggregation { return nil }
temporalitySelector := func(metric.InstrumentKind) metricdata.Temporality { return metricdata.CumulativeTemporality }

testCases := []struct {
name string
Expand All @@ -48,12 +50,20 @@ func TestNewGathererConfig(t *testing.T) {
},
wantConfig: gathererConfig{},
},
{
name: "WithTemporalitySelector",
options: []GathererOption{
WithTemporalitySelector(temporalitySelector),
},
wantConfig: gathererConfig{},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
cfg := newGathererConfig(tt.options...)
// tested by TestConfigManualReaderOptions
cfg.aggregation = nil
cfg.temporality = nil

assert.Equal(t, tt.wantConfig, cfg)
})
Expand All @@ -62,6 +72,7 @@ func TestNewGathererConfig(t *testing.T) {

func TestConfigManualReaderOptions(t *testing.T) {
aggregationSelector := func(metric.InstrumentKind) aggregation.Aggregation { return nil }
temporalitySelector := func(metric.InstrumentKind) metricdata.Temporality { return metricdata.CumulativeTemporality }

testCases := []struct {
name string
Expand All @@ -71,14 +82,23 @@ func TestConfigManualReaderOptions(t *testing.T) {
{
name: "Default",
config: gathererConfig{},
wantOptionCount: 1,
wantOptionCount: 0,
},

{
name: "WithAggregationSelector",
config: gathererConfig{aggregation: aggregationSelector},
wantOptionCount: 1,
},
{
name: "WithTemporalitySelector",
config: gathererConfig{temporality: temporalitySelector},
wantOptionCount: 1,
},
{
name: "WithAggregationSelectorWithTemporalitySelector",
config: gathererConfig{aggregation: aggregationSelector, temporality: temporalitySelector},
wantOptionCount: 2,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
Expand Down