Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vlastahajek committed Jun 19, 2020
1 parent e3e9401 commit ac62f03
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 54 deletions.
24 changes: 16 additions & 8 deletions api/write/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type Options struct {
precision time.Duration
// Whether to use GZip compression in requests. Default false
useGZip bool
// Point settings, default tags
pointSettings *PointSettings
// Tags added to each point during writing. If a point already has a tag with the same key, it is left unchanged.
defaultTags map[string]string
}

// BatchSize returns size of batch
Expand Down Expand Up @@ -106,15 +106,23 @@ func (o *Options) SetUseGZip(useGZip bool) *Options {
return o
}

// PointSettings returns point settings
func (o *Options) PointSettings() *PointSettings {
if o.pointSettings == nil {
o.pointSettings = NewPointSettings()
// AddDefaultTag adds a default tag. DefaultTags are added to each written point.
// If a tag with the same key already exist it is overwritten.
// If a point already defines such a tag, it is left unchanged.
func (o *Options) AddDefaultTag(key, value string) *Options {
o.DefaultTags()[key] = value
return o
}

// DefaultTags returns set of default tags
func (o *Options) DefaultTags() map[string]string {
if o.defaultTags == nil {
o.defaultTags = make(map[string]string)
}
return o.pointSettings
return o.defaultTags
}

// DefaultOptions returns Options object with default values
func DefaultOptions() *Options {
return &Options{batchSize: 5000, maxRetries: 3, retryInterval: 1000, flushInterval: 1000, precision: time.Nanosecond, useGZip: false, retryBufferLimit: 10000, pointSettings: NewPointSettings()}
return &Options{batchSize: 5000, maxRetries: 3, retryInterval: 1000, flushInterval: 1000, precision: time.Nanosecond, useGZip: false, retryBufferLimit: 10000, defaultTags: make(map[string]string)}
}
6 changes: 5 additions & 1 deletion api/write/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestDefaultOptions(t *testing.T) {
assert.Equal(t, uint(10000), opts.RetryBufferLimit())
assert.Equal(t, uint(1000), opts.RetryInterval())
assert.Equal(t, uint(3), opts.MaxRetries())
assert.Len(t, opts.DefaultTags(), 0)
}

func TestSettingsOptions(t *testing.T) {
Expand All @@ -31,12 +32,15 @@ func TestSettingsOptions(t *testing.T) {
SetPrecision(time.Millisecond).
SetRetryBufferLimit(5).
SetRetryInterval(5000).
SetMaxRetries(7)
SetMaxRetries(7).
AddDefaultTag("a", "1").
AddDefaultTag("b", "2")
assert.Equal(t, uint(5), opts.BatchSize())
assert.Equal(t, true, opts.UseGZip())
assert.Equal(t, uint(5000), opts.FlushInterval())
assert.Equal(t, time.Millisecond, opts.Precision())
assert.Equal(t, uint(5), opts.RetryBufferLimit())
assert.Equal(t, uint(5000), opts.RetryInterval())
assert.Equal(t, uint(7), opts.MaxRetries())
assert.Len(t, opts.DefaultTags(), 2)
}
28 changes: 0 additions & 28 deletions api/write/pointSettings.go

This file was deleted.

2 changes: 1 addition & 1 deletion api/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestWriteApiWriteDefaultTag(t *testing.T) {
service := newTestService(t, "http://localhost:8888")
opts := write.DefaultOptions().
SetBatchSize(1)
opts.PointSettings().AddDefaultTag("dft", "a")
opts.AddDefaultTag("dft", "a")
writeApi := NewWriteApiImpl("my-org", "my-bucket", service, opts)
point := write.NewPoint("test",
map[string]string{
Expand Down
24 changes: 14 additions & 10 deletions internal/write/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,24 +201,28 @@ func (w *Service) EncodePoints(points ...*write.Point) (string, error) {
e.SetFieldTypeSupport(lp.UintSupport)
e.FailOnFieldErr(true)
e.SetPrecision(w.writeOptions.Precision())
var m lp.Metric
for _, point := range points {
if len(w.writeOptions.PointSettings().DefaultTags()) > 0 {
m = &pointWithDefaultTags{
point: point,
defaultTags: w.writeOptions.PointSettings().DefaultTags(),
}
} else {
m = point
}
_, err := e.Encode(m)
_, err := e.Encode(w.pointToEncode(point))
if err != nil {
return "", err
}
}
return buffer.String(), nil
}

func (w *Service) pointToEncode(point *write.Point) lp.Metric {
var m lp.Metric
if len(w.writeOptions.DefaultTags()) > 0 {
m = &pointWithDefaultTags{
point: point,
defaultTags: w.writeOptions.DefaultTags(),
}
} else {
m = point
}
return m
}

func (w *Service) WriteUrl() (string, error) {
if w.url == "" {
u, err := url.Parse(w.httpService.ServerApiUrl())
Expand Down
6 changes: 3 additions & 3 deletions internal/write/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

func TestAddDefaultTags(t *testing.T) {
opts := write.DefaultOptions()
assert.Len(t, opts.PointSettings().DefaultTags(), 0)
assert.Len(t, opts.DefaultTags(), 0)

opts.PointSettings().AddDefaultTag("dt1", "val1")
opts.PointSettings().AddDefaultTag("zdt", "val2")
opts.AddDefaultTag("dt1", "val1")
opts.AddDefaultTag("zdt", "val2")
srv := NewService("org", "buc", nil, opts)

p := write.NewPointWithMeasurement("test")
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ func (o *Options) HttpOptions() *http.Options {

// AddDefaultTag adds a default tag. DefaultTags are added to each written point.
// If a tag with the same key already exist it is overwritten.
// If a point already defines a such tag, it is left unchanged
// If a point already defines such a tag, it is left unchanged
func (o *Options) AddDefaultTag(key, value string) *Options {
o.WriteOptions().PointSettings().AddDefaultTag(key, value)
o.WriteOptions().AddDefaultTag(key, value)
return o
}

Expand Down
2 changes: 1 addition & 1 deletion options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestSettingsOptions(t *testing.T) {
assert.Equal(t, tlsConfig, opts.TlsConfig())
assert.Equal(t, uint(50), opts.HttpRequestTimeout())
assert.Equal(t, uint(3), opts.LogLevel())
assert.Len(t, opts.WriteOptions().PointSettings().DefaultTags(), 1)
assert.Len(t, opts.WriteOptions().DefaultTags(), 1)
}

func TestTimeout(t *testing.T) {
Expand Down

0 comments on commit ac62f03

Please sign in to comment.