Like Prometheus, but for logs.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
loki/clients/pkg/promtail/client/batch.go

157 lines
3.7 KiB

package client
import (
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
"fmt"
"strconv"
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
"strings"
"time"
"github.com/gogo/protobuf/proto"
"github.com/golang/snappy"
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
"github.com/prometheus/common/model"
"golang.org/x/exp/slices"
"github.com/grafana/loki/v3/clients/pkg/promtail/api"
"github.com/grafana/loki/v3/pkg/logproto"
)
const (
errMaxStreamsLimitExceeded = "streams limit exceeded, streams: %d exceeds limit: %d, stream: '%s'"
)
// batch holds pending log streams waiting to be sent to Loki, and it's used
// to reduce the number of push requests to Loki aggregating multiple log streams
// and entries in a single batch request. In case of multi-tenant Promtail, log
// streams for each tenant are stored in a dedicated batch.
type batch struct {
streams map[string]*logproto.Stream
bytes int
createdAt time.Time
maxStreams int
}
func newBatch(maxStreams int, entries ...api.Entry) *batch {
b := &batch{
streams: map[string]*logproto.Stream{},
bytes: 0,
createdAt: time.Now(),
maxStreams: maxStreams,
}
// Add entries to the batch
for _, entry := range entries {
//never error here
_ = b.add(entry)
}
return b
}
// add an entry to the batch
func (b *batch) add(entry api.Entry) error {
b.bytes += entrySize(entry)
// Append the entry to an already existing stream (if any)
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
labels := labelsMapToString(entry.Labels, ReservedLabelTenantID)
if stream, ok := b.streams[labels]; ok {
stream.Entries = append(stream.Entries, entry.Entry)
return nil
}
streams := len(b.streams)
if b.maxStreams > 0 && streams >= b.maxStreams {
return fmt.Errorf(errMaxStreamsLimitExceeded, streams, b.maxStreams, labels)
}
// Add the entry as a new stream
b.streams[labels] = &logproto.Stream{
Labels: labels,
Entries: []logproto.Entry{entry.Entry},
}
return nil
}
func labelsMapToString(ls model.LabelSet, without model.LabelName) string {
var b strings.Builder
totalSize := 2
lstrs := make([]model.LabelName, 0, len(ls))
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
for l, v := range ls {
if l == without {
continue
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
}
lstrs = append(lstrs, l)
// guess size increase: 2 for `, ` between labels and 3 for the `=` and quotes around label value
totalSize += len(l) + 2 + len(v) + 3
}
b.Grow(totalSize)
b.WriteByte('{')
slices.Sort(lstrs)
for i, l := range lstrs {
if i > 0 {
b.WriteString(", ")
}
b.WriteString(string(l))
b.WriteString(`=`)
b.WriteString(strconv.Quote(string(ls[l])))
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
}
b.WriteByte('}')
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
return b.String()
Fixes a race when using specific tenant and multi-client. (#3573) * Fixes a race when using specific tenant and multi-client. This was because each client would try to mutate the original set of labels. Instead of cloning the map which can be expensive, I created a little helper that generate the string from the labelset without a set of label name. Fixes #3571 I also added a test to see if this was reproducible and it was indeed: ``` ~/go/src/github.com/grafana/loki master* ❯ go test -timeout 30s -tags dev,gofuzz -race -run ^TestMultiClient_Handle_Race$ github.com/grafana/loki/pkg/promtail/client -v -count=1 -timeout=0s === RUN TestMultiClient_Handle_Race ================== WARNING: DATA RACE Read at 0x00c0000b77d0 by goroutine 22: runtime.mapaccess2_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:107 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).getTenantID() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:376 +0xcb github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:407 +0x9a github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Previous write at 0x00c0000b77d0 by goroutine 21: runtime.mapdelete_faststr() /usr/local/Cellar/go/1.16.2/libexec/src/runtime/map_faststr.go:297 +0x0 github.com/grafana/loki/pkg/promtail/client.(*client).processEntry() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:408 +0x144 github.com/grafana/loki/pkg/promtail/client.(*client).run() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:235 +0x36a Goroutine 22 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:126 +0x2c7 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 Goroutine 21 (running) created at: github.com/grafana/loki/pkg/promtail/client.New() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/client.go:198 +0x7dc github.com/grafana/loki/pkg/promtail/client.TestMultiClient_Handle_Race() /Users/ctovena/go/src/github.com/grafana/loki/pkg/promtail/client/multi_test.go:124 +0x187 testing.tRunner() /usr/local/Cellar/go/1.16.2/libexec/src/testing/testing.go:1194 +0x202 ================== testing.go:1093: race detected during execution of test --- FAIL: TestMultiClient_Handle_Race (0.00s) === CONT testing.go:1093: race detected during execution of test FAIL FAIL github.com/grafana/loki/pkg/promtail/client 0.022s FAIL ``` Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * don't overload variable name Co-authored-by: Edward Welch <edward.welch@grafana.com>
4 years ago
}
// sizeBytes returns the current batch size in bytes
func (b *batch) sizeBytes() int {
return b.bytes
}
// sizeBytesAfter returns the size of the batch after the input entry
// will be added to the batch itself
Asynchronous Promtail stages (#2996) * Introducing go pipelines to promtail. Based off @jeschkies idea. WIP Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fixes all tests. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * More tests and code cleanup. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Wip breaking things. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fixing tests, adding Stop to the interface. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fixes all tests. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fixes more test. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Moar fixes for tests. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Close correctly client before reading. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Use defer. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Add some comments. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fixes lint issues Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Attempt to fix journald without linux. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fixes journald json test. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Add missing stop in the filetarget. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Update pkg/logentry/stages/match_test.go Co-authored-by: Karsten Jeschkies <k@jeschkies.xyz> * First set of feeback review fixes. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Use newEntry as suggested by Karsten. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * Fix tests. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> * lint. Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com> Co-authored-by: Karsten Jeschkies <k@jeschkies.xyz>
5 years ago
func (b *batch) sizeBytesAfter(entry api.Entry) int {
return b.bytes + entrySize(entry)
}
// age of the batch since its creation
func (b *batch) age() time.Duration {
return time.Since(b.createdAt)
}
// encode the batch as snappy-compressed push request, and returns
// the encoded bytes and the number of encoded entries
func (b *batch) encode() ([]byte, int, error) {
req, entriesCount := b.createPushRequest()
buf, err := proto.Marshal(req)
if err != nil {
return nil, 0, err
}
buf = snappy.Encode(nil, buf)
return buf, entriesCount, nil
}
// creates push request and returns it, together with number of entries
func (b *batch) createPushRequest() (*logproto.PushRequest, int) {
req := logproto.PushRequest{
Streams: make([]logproto.Stream, 0, len(b.streams)),
}
entriesCount := 0
for _, stream := range b.streams {
req.Streams = append(req.Streams, *stream)
entriesCount += len(stream.Entries)
}
return &req, entriesCount
}
func entrySize(entry api.Entry) int {
structuredMetadataSize := 0
for _, label := range entry.StructuredMetadata {
structuredMetadataSize += label.Size()
}
return len(entry.Line) + structuredMetadataSize
}