Skip to content

Commit 63e84b4

Browse files
authored
fix(aggregated-metrics): correctly create logfmt string (#14124)
1 parent 53cfef3 commit 63e84b4

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

‎pkg/logql/metrics.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/c2h5oh/datasize"
11-
"github.com/dustin/go-humanize"
1211
"github.com/go-kit/log"
1312
"github.com/go-kit/log/level"
1413
"github.com/prometheus/client_golang/prometheus"
@@ -156,9 +155,9 @@ func RecordRangeAndInstantQueryMetrics(
156155
"status", status,
157156
"limit", p.Limit(),
158157
"returned_lines", returnedLines,
159-
"throughput", humanizeBytes(uint64(stats.Summary.BytesProcessedPerSecond)),
160-
"total_bytes", humanizeBytes(uint64(stats.Summary.TotalBytesProcessed)),
161-
"total_bytes_structured_metadata", humanizeBytes(uint64(stats.Summary.TotalStructuredMetadataBytesProcessed)),
158+
"throughput", util.HumanizeBytes(uint64(stats.Summary.BytesProcessedPerSecond)),
159+
"total_bytes", util.HumanizeBytes(uint64(stats.Summary.TotalBytesProcessed)),
160+
"total_bytes_structured_metadata", util.HumanizeBytes(uint64(stats.Summary.TotalStructuredMetadataBytesProcessed)),
162161
"lines_per_second", stats.Summary.LinesProcessedPerSecond,
163162
"total_lines", stats.Summary.TotalLinesProcessed,
164163
"post_filter_lines", stats.Summary.TotalPostFilterLines,
@@ -197,11 +196,11 @@ func RecordRangeAndInstantQueryMetrics(
197196
// Total ingester reached for this query.
198197
"ingester_requests", stats.Ingester.GetTotalReached(),
199198
// Total bytes processed but was already in memory (found in the headchunk). Includes structured metadata bytes.
200-
"ingester_chunk_head_bytes", humanizeBytes(uint64(stats.Ingester.Store.Chunk.GetHeadChunkBytes())),
199+
"ingester_chunk_head_bytes", util.HumanizeBytes(uint64(stats.Ingester.Store.Chunk.GetHeadChunkBytes())),
201200
// Total bytes of compressed chunks (blocks) processed.
202-
"ingester_chunk_compressed_bytes", humanizeBytes(uint64(stats.Ingester.Store.Chunk.GetCompressedBytes())),
201+
"ingester_chunk_compressed_bytes", util.HumanizeBytes(uint64(stats.Ingester.Store.Chunk.GetCompressedBytes())),
203202
// Total bytes decompressed and processed from chunks. Includes structured metadata bytes.
204-
"ingester_chunk_decompressed_bytes", humanizeBytes(uint64(stats.Ingester.Store.Chunk.GetDecompressedBytes())),
203+
"ingester_chunk_decompressed_bytes", util.HumanizeBytes(uint64(stats.Ingester.Store.Chunk.GetDecompressedBytes())),
205204
// Total lines post filtering.
206205
"ingester_post_filter_lines", stats.Ingester.Store.Chunk.GetPostFilterLines(),
207206
// Time spent being blocked on congestion control.
@@ -243,10 +242,6 @@ func RecordRangeAndInstantQueryMetrics(
243242
recordUsageStats(queryType, stats)
244243
}
245244

246-
func humanizeBytes(val uint64) string {
247-
return strings.Replace(humanize.Bytes(val), " ", "", 1)
248-
}
249-
250245
func RecordLabelQueryMetrics(
251246
ctx context.Context,
252247
log log.Logger,

‎pkg/pattern/aggregation/push.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"sync"
1212
"time"
1313

14-
"github.com/dustin/go-humanize"
1514
"github.com/go-kit/log"
1615
"github.com/go-kit/log/level"
1716
"github.com/golang/snappy"
@@ -22,6 +21,7 @@ import (
2221
"github.com/grafana/loki/v3/pkg/loghttp/push"
2322
"github.com/grafana/loki/v3/pkg/logproto"
2423
"github.com/grafana/loki/v3/pkg/logql/syntax"
24+
"github.com/grafana/loki/v3/pkg/util"
2525
"github.com/grafana/loki/v3/pkg/util/build"
2626

2727
"github.com/grafana/dskit/backoff"
@@ -312,17 +312,17 @@ func AggregatedMetricEntry(
312312
service string,
313313
lbls labels.Labels,
314314
) string {
315-
byteString := humanize.Bytes(totalBytes)
315+
byteString := util.HumanizeBytes(totalBytes)
316316
base := fmt.Sprintf(
317-
"ts=%d bytes=%s count=%d %s=%s",
317+
"ts=%d bytes=%s count=%d %s=\"%s\"",
318318
ts.UnixNano(),
319319
byteString,
320320
totalCount,
321321
push.LabelServiceName, service,
322322
)
323323

324324
for _, l := range lbls {
325-
base += fmt.Sprintf(" %s=%s", l.Name, l.Value)
325+
base += fmt.Sprintf(" %s=\"%s\"", l.Name, l.Value)
326326
}
327327

328328
return base

‎pkg/pattern/aggregation/push_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ func Test_Push(t *testing.T) {
229229
stream2.Entries[2].Line,
230230
)
231231

232+
// sanity check that bytes are logged in humanized form without whitespaces
233+
assert.Contains(t, stream1.Entries[0].Line, "bytes=1B")
234+
232235
case <-time.After(5 * time.Second):
233236
t.Fatal("timeout")
234237
}

‎pkg/util/metrics_helper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"errors"
66
"fmt"
77
"math"
8+
"strings"
89
"sync"
910

11+
humanize "github.com/dustin/go-humanize"
1012
"github.com/go-kit/log/level"
1113
"github.com/prometheus/client_golang/prometheus"
1214
dto "github.com/prometheus/client_model/go"
@@ -841,3 +843,8 @@ func RegisterCounterVec(registerer prometheus.Registerer, namespace, name, help
841843
}
842844
return vec
843845
}
846+
847+
// HumanizeBytes returns a human readable string representation of the given byte value and removes all whitespaces.
848+
func HumanizeBytes(val uint64) string {
849+
return strings.Replace(humanize.Bytes(val), " ", "", 1)
850+
}

‎pkg/util/metrics_helper_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,3 +1158,18 @@ func verifyLabels(t *testing.T, m prometheus.Collector, filter map[string]string
11581158

11591159
require.Equal(t, expectedLabels, result)
11601160
}
1161+
1162+
func TestHumanizeBytes(t *testing.T) {
1163+
tests := map[uint64]string{
1164+
1024: "1.0kB",
1165+
1024 * 1000: "1.0MB",
1166+
1024 * 1000 * 1000: "1.0GB",
1167+
10: "10B",
1168+
}
1169+
1170+
for bytes, humanizedBytes := range tests {
1171+
t.Run(fmt.Sprintf("%d", bytes), func(t *testing.T) {
1172+
require.Equal(t, humanizedBytes, HumanizeBytes(bytes))
1173+
})
1174+
}
1175+
}

0 commit comments

Comments
 (0)