Skip to content

Commit

Permalink
Merge pull request #253 from bonitoo-io/fix/debug_log
Browse files Browse the repository at this point in the history
fix: conditional debug logging of buffers
  • Loading branch information
vlastahajek authored Apr 29, 2021
2 parents 669c9d2 + 97bd11c commit 36181ee
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
command: ./scripts/influxdb-restart.sh
- run:
command: |
go get gotest.tools/gotestsum && gotestsum --junitfile /tmp/test-results/unit-tests.xml -- -race -coverprofile=coverage.txt -covermode=atomic -coverpkg '.,./api/...,./internal/...' -tags e2e ./...
go get gotest.tools/gotestsum && gotestsum --junitfile /tmp/test-results/unit-tests.xml -- -race -coverprofile=coverage.txt -covermode=atomic -coverpkg '.,./api/...,./internal/.../,./log/...' -tags e2e ./...
bash <(curl -s https://codecov.io/bash)
go tool cover -html=coverage.txt -o /tmp/artifacts/coverage.html
- store_artifacts:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 2.3.0 [in progress]
### Breaking change
[#253](https://github.com/influxdata/influxdb-client-go/pull/253) Interface 'Logger' extended with 'LogLevel() uint' getter.

### Features
[#241](https://github.com/influxdata/influxdb-client-go/pull/241),[#248](https://github.com/influxdata/influxdb-client-go/pull/248) Synced with InfluxDB 2.0.5 swagger:
- Setup (onboarding) now sends correctly retentionDuration if specified
Expand All @@ -12,6 +15,7 @@

### Bug fixes
1. [#252](https://github.com/influxdata/influxdb-client-go/pull/252) Fixed panic when getting not present standard Flux columns
1. [#253](https://github.com/influxdata/influxdb-client-go/pull/253) Conditional debug logging of buffers

## 2.2.3 [2021-04-01]
### Bug fixes
Expand Down
2 changes: 0 additions & 2 deletions api/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"fmt"
"github.com/influxdata/influxdb-client-go/v2/domain"
"github.com/influxdata/influxdb-client-go/v2/internal/log"
)

// BucketsAPI provides methods for managing Buckets in a InfluxDB server.
Expand Down Expand Up @@ -94,7 +93,6 @@ func (b *bucketsAPI) getBuckets(ctx context.Context, params *domain.GetBucketsPa
if err != nil {
return nil, err
}
log.Debugf("getbuckets: ", string(response.Body))
if response.JSONDefault != nil {
return nil, domain.DomainErrorToError(response.JSONDefault, response.StatusCode())
}
Expand Down
9 changes: 7 additions & 2 deletions api/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/influxdata/influxdb-client-go/v2/api/query"
"github.com/influxdata/influxdb-client-go/v2/domain"
"github.com/influxdata/influxdb-client-go/v2/internal/log"
ilog "github.com/influxdata/influxdb-client-go/v2/log"
)

const (
Expand Down Expand Up @@ -75,7 +76,9 @@ func (q *queryAPI) QueryRaw(ctx context.Context, query string, dialect *domain.D
if err != nil {
return "", err
}
log.Debugf("Query: %s", string(qrJSON))
if log.LogLevel() >= ilog.DebugLevel {
log.Debugf("Query: %s", qrJSON)
}
var body string
perror := q.httpService.DoPostRequest(ctx, queryURL, bytes.NewReader(qrJSON), func(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
Expand Down Expand Up @@ -125,7 +128,9 @@ func (q *queryAPI) Query(ctx context.Context, query string) (*QueryTableResult,
if err != nil {
return nil, err
}
log.Debugf("Query: %s", string(qrJSON))
if log.LogLevel() >= ilog.DebugLevel {
log.Debugf("Query: %s", qrJSON)
}
perror := q.httpService.DoPostRequest(ctx, queryURL, bytes.NewReader(qrJSON), func(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept-Encoding", "gzip")
Expand Down
7 changes: 7 additions & 0 deletions internal/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ func Error(msg string) {
ilog.Log.Error(msg)
}
}

func LogLevel() uint {
if ilog.Log != nil {
return ilog.Log.LogLevel()
}
return ilog.ErrorLevel
}
2 changes: 2 additions & 0 deletions internal/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func TestLogging(t *testing.T) {
var sb strings.Builder
log.SetOutput(&sb)
dlog.Log.SetLogLevel(dlog.DebugLevel)
assert.Equal(t, dlog.DebugLevel, dlog.Log.LogLevel())
assert.Equal(t, dlog.DebugLevel, ilog.LogLevel())
//test default settings
ilog.Debug("Debug")
ilog.Debugf("Debugf %s %d", "message", 1)
Expand Down
9 changes: 5 additions & 4 deletions internal/write/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/influxdata/influxdb-client-go/v2/api/write"
"github.com/influxdata/influxdb-client-go/v2/internal/gzip"
"github.com/influxdata/influxdb-client-go/v2/internal/log"
ilog "github.com/influxdata/influxdb-client-go/v2/log"
lp "github.com/influxdata/line-protocol"
)

Expand Down Expand Up @@ -149,7 +150,10 @@ func (w *Service) WriteBatch(ctx context.Context, batch *Batch) *http2.Error {
var body io.Reader
var err error
body = strings.NewReader(batch.batch)
log.Debugf("Writing batch: %s", batch.batch)

if log.LogLevel() >= ilog.DebugLevel {
log.Debugf("Writing batch: %s", batch.batch)
}
if w.writeOptions.UseGZip() {
body, err = gzip.CompressWithGzip(body)
if err != nil {
Expand All @@ -164,9 +168,6 @@ func (w *Service) WriteBatch(ctx context.Context, batch *Batch) *http2.Error {
req.Header.Set("Content-Encoding", "gzip")
}
}, func(r *http.Response) error {
// discard body so connection can be reused
// _, _ = io.Copy(ioutil.Discard, r.Body)
// _ = r.Body.Close()
return nil
})
return perror
Expand Down
34 changes: 2 additions & 32 deletions internal/write/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,37 +61,7 @@ func TestAddDefaultTags(t *testing.T) {
assert.Len(t, p.TagList(), 2)
}

func TestDefaultRetryDelay(t *testing.T) {
log.Log.SetLogLevel(log.DebugLevel)
hs := test.NewTestService(t, "http://localhost:8086")
opts := write.DefaultOptions()
ctx := context.Background()
srv := NewService("my-org", "my-bucket", hs, opts)
hs.SetReplyError(&http.Error{
StatusCode: 502,
})
b1 := NewBatch("", opts.RetryInterval())
err := srv.HandleWrite(ctx, b1)
assert.NotNil(t, err)
assert.Equal(t, uint(5000), b1.retryDelay)
assert.Equal(t, 1, srv.retryQueue.list.Len())
//wait retry delay + little more
<-time.After(time.Millisecond*time.Duration(b1.retryDelay) + time.Microsecond*5)
b2 := NewBatch("", opts.RetryInterval())
err = srv.HandleWrite(ctx, b2)
assert.NotNil(t, err)
assert.Equal(t, uint(25000), b1.retryDelay)
assert.Equal(t, 2, srv.retryQueue.list.Len())

<-time.After(time.Millisecond*time.Duration(b1.retryDelay) + time.Microsecond*5)
b3 := NewBatch("", opts.RetryInterval())
err = srv.HandleWrite(ctx, b3)
assert.NotNil(t, err)
assert.Equal(t, uint(125000), b1.retryDelay)
assert.Equal(t, 3, srv.retryQueue.list.Len())
}

func TestCustomRetryDelayWithFLush(t *testing.T) {
func TestRetryStrategy(t *testing.T) {
log.Log.SetLogLevel(log.DebugLevel)
hs := test.NewTestService(t, "http://localhost:8086")
opts := write.DefaultOptions().SetRetryInterval(1)
Expand Down Expand Up @@ -299,7 +269,7 @@ func TestWriteContextCancel(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
<-time.After(time.Second)
<-time.After(10 * time.Millisecond)
err = srv.HandleWrite(ctx, NewBatch(strings.Join(lines, "\n"), opts.RetryInterval()))
wg.Done()
}()
Expand Down
14 changes: 10 additions & 4 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ const (

// Logger defines interface for logging
type Logger interface {
// SetLogLevel sets allowed logging level.
SetLogLevel(logLevel uint)
// SetPrefix sets logging prefix.
SetPrefix(prefix string)
// Writes formatted debug message if debug logLevel is enabled.
Debugf(format string, v ...interface{})
// Writes debug message if debug is enabled.
Expand All @@ -44,6 +40,12 @@ type Logger interface {
Errorf(format string, v ...interface{})
// Writes error message
Error(msg string)
// SetLogLevel sets allowed logging level.
SetLogLevel(logLevel uint)
// LogLevel retrieves current logging level
LogLevel() uint
// SetPrefix sets logging prefix.
SetPrefix(prefix string)
}

// logger provides default implementation for Logger. It logs using Go log API
Expand All @@ -56,6 +58,10 @@ func (l *logger) SetLogLevel(logLevel uint) {
l.logLevel = logLevel
}

func (l *logger) LogLevel() uint {
return l.logLevel
}

func (l *logger) SetPrefix(prefix string) {
l.prefix = prefix
}
Expand Down
4 changes: 4 additions & 0 deletions log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func TestCustomLogger(t *testing.T) {
type testLogger struct {
}

func (l *testLogger) LogLevel() uint {
return 0
}

func (l *testLogger) SetLogLevel(_ uint) {
}

Expand Down

0 comments on commit 36181ee

Please sign in to comment.