-
Notifications
You must be signed in to change notification settings - Fork 99
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
Backend: Adding OTLP Configuration #655
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package otlp | ||
|
||
import ( | ||
"errors" | ||
"runtime" | ||
|
||
"github.com/spf13/viper" | ||
"go.uber.org/multierr" | ||
) | ||
|
||
const ( | ||
ConversionAsGauge = "AsGauge" | ||
ConversionAsHistogram = "AsHistogram" | ||
) | ||
|
||
type Config struct { | ||
// Endpoint (Required) is the FQDN with path for the metrics ingestion endpoint | ||
Endpoint string `mapstructure:"endpoint"` | ||
// MaxRequests (Optional, default: cpu.count * 2) is the upper limit on the number of inflight requests | ||
MaxRequests int `mapstructure:"max_requests"` | ||
// ResourceKeys (Optional) is used to extract values from provided tags | ||
// to apply to all values within a resource instead within each attribute. | ||
// Strongly encouraged to allow down stream consumers to | ||
// process based on values defined at the top level resource. | ||
ResourceKeys []string `mapstructure:"resource_keys"` | ||
// TimerConversion (Optional, Default: AsGauge) determines if a timers are emitted as one of the following: | ||
// - AsGauges emits each calcauted value (min, max, sum, count, upper, upper_99, etc...) as a guage value | ||
// - AsHistograms each timer is set as an empty bucket histogram with only Min, Max, Sum, and Count set | ||
TimerConversion string `mapstructure:"timer_conversion"` | ||
// HistogramConversion (Optional, Default: AsGauge) determines how a GoStatsD histogram should be converted into OTLP. | ||
// The allowed values for this are: | ||
// - AsGauges: sends each bucket count (including +inf) as a guage value and the bucket boundry as a tag | ||
// - AsHistogram: sends each histogram as a single Histogram value with bucket and statistics calculated. | ||
HistogramConversion string `mapstructure:"histogram_conversion"` | ||
// Transport (Optional, Default: "default") is used to reference to configured transport | ||
// to be used for this backend | ||
Transport string `mapstructure:"transport"` | ||
// UserAgent (Optional, default: "gostatsd") allows you to set the | ||
// user agent header when making requests. | ||
UserAgent string `mapstructure:"user_agent"` | ||
} | ||
|
||
func newDefaultConfig() Config { | ||
return Config{ | ||
Transport: "default", | ||
MaxRequests: runtime.NumCPU() * 2, | ||
TimerConversion: "AsGauge", | ||
HistogramConversion: "AsGauge", | ||
UserAgent: "gostatsd", | ||
} | ||
} | ||
|
||
func NewConfig(v *viper.Viper) (*Config, error) { | ||
cfg := newDefaultConfig() | ||
if err := v.Unmarshal(&cfg); err != nil { | ||
return nil, err | ||
} | ||
if err := cfg.Validate(); err != nil { | ||
return nil, err | ||
} | ||
return &cfg, nil | ||
} | ||
|
||
func (c Config) Validate() (errs error) { | ||
if c.Endpoint == "" { | ||
errs = multierr.Append(errs, errors.New("no endpoint defined")) | ||
} | ||
if c.MaxRequests <= 0 { | ||
errs = multierr.Append(errs, errors.New("max request must be a positive value")) | ||
} | ||
if c.Transport == "" { | ||
errs = multierr.Append(errs, errors.New("no transport defined")) | ||
} | ||
|
||
conversion := map[string]struct{}{ | ||
ConversionAsGauge: {}, | ||
ConversionAsHistogram: {}, | ||
} | ||
|
||
if _, ok := conversion[c.TimerConversion]; !ok { | ||
errs = multierr.Append(errs, errors.New(`time conversion must be one of ["AsGauge", "AsHistogram"]`)) | ||
} | ||
|
||
if _, ok := conversion[c.HistogramConversion]; !ok { | ||
errs = multierr.Append(errs, errors.New(`histogram conversion must be one of ["AsGauge", "AsHistogram"]`)) | ||
} | ||
|
||
return errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package otlp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewConfig(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range []struct { | ||
name string | ||
v *viper.Viper | ||
expect *Config | ||
errVal string | ||
}{ | ||
{ | ||
name: "empty configuration", | ||
v: viper.New(), | ||
expect: nil, | ||
errVal: "no endpoint defined", | ||
}, | ||
{ | ||
name: "min configuration defined", | ||
v: func() *viper.Viper { | ||
v := viper.New() | ||
v.SetDefault("endpoint", "http://local") | ||
v.SetDefault("max_requests", 1) | ||
return v | ||
}(), | ||
expect: &Config{ | ||
Endpoint: "http://local", | ||
MaxRequests: 1, | ||
TimerConversion: "AsGauge", | ||
HistogramConversion: "AsGauge", | ||
Transport: "default", | ||
UserAgent: "gostatsd", | ||
}, | ||
errVal: "", | ||
}, | ||
{ | ||
name: "invalid options", | ||
v: func() *viper.Viper { | ||
v := viper.New() | ||
v.SetDefault("max_requests", 0) | ||
v.SetDefault("transport", "") | ||
v.SetDefault("timer_conversion", "values") | ||
v.SetDefault("histogram_conversion", "values") | ||
return v | ||
}(), | ||
expect: nil, | ||
errVal: "no endpoint defined; max request must be a positive value; no transport defined; time conversion must be one of [\"AsGauge\", \"AsHistogram\"]; histogram conversion must be one of [\"AsGauge\", \"AsHistogram\"]", | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
cfg, err := NewConfig(tc.v) | ||
assert.Equal(t, tc.expect, cfg, "Must match the expected configuration") | ||
if tc.errVal != "" { | ||
assert.EqualError(t, err, tc.errVal, "Must match the expected error") | ||
} else { | ||
assert.NoError(t, err, "Must not error") | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we type def AsGauge and AsHistograms since they're fixed constants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They will change into single value within the next PR, so I am not too worried