Skip to content

Commit 4d3f9f5

Browse files
authored
feat(logcli): add gzip compression option (#14598)
Passing `--compress` to logcli will enable (or more accurately not disable) compression on the `http.Transport`, allowing Loki to return gzip-compressed payloads. This improves overall execution time and reduces data transfer by 10-15x. Signed-off-by: Jason Tackaberry <[email protected]>
1 parent 27b4071 commit 4d3f9f5

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

cmd/logcli/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ func newQueryClient(app *kingpin.Application) client.Client {
483483
app.Flag("max-backoff", "Maximum backoff time between retries. Can also be set using LOKI_CLIENT_MAX_BACKOFF env var.").Default("0").Envar("LOKI_CLIENT_MAX_BACKOFF").IntVar(&client.BackoffConfig.MaxBackoff)
484484
app.Flag("auth-header", "The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.").Default("Authorization").Envar("LOKI_AUTH_HEADER").StringVar(&client.AuthHeader)
485485
app.Flag("proxy-url", "The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.").Default("").Envar("LOKI_HTTP_PROXY_URL").StringVar(&client.ProxyURL)
486+
app.Flag("compress", "Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.").Default("false").Envar("LOKI_HTTP_COMPRESSION").BoolVar(&client.Compression)
486487

487488
return client
488489
}

docs/sources/query/logcli.md

+8
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Flags:
371371
--auth-header="Authorization"
372372
The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.
373373
--proxy-url="" The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.
374+
--compress Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.
374375
--limit=30 Limit on number of entries to print. Setting it to 0 will fetch all entries.
375376
--since=1h Lookback window.
376377
--from=FROM Start looking for logs at this absolute time (inclusive)
@@ -465,6 +466,7 @@ Flags:
465466
--auth-header="Authorization"
466467
The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.
467468
--proxy-url="" The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.
469+
--compress Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.
468470
--limit=30 Limit on number of entries to print. Setting it to 0 will fetch all entries.
469471
--now=NOW Time at which to execute the instant query.
470472
--forward Scan forwards through logs.
@@ -525,6 +527,7 @@ Flags:
525527
--auth-header="Authorization"
526528
The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.
527529
--proxy-url="" The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.
530+
--compress Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.
528531
--since=1h Lookback window.
529532
--from=FROM Start looking for labels at this absolute time (inclusive)
530533
--to=TO Stop looking for labels at this absolute time (exclusive)
@@ -581,6 +584,7 @@ Flags:
581584
--auth-header="Authorization"
582585
The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.
583586
--proxy-url="" The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.
587+
--compress Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.
584588
--since=1h Lookback window.
585589
--from=FROM Start looking for logs at this absolute time (inclusive)
586590
--to=TO Stop looking for logs at this absolute time (exclusive)
@@ -633,6 +637,7 @@ Flags:
633637
--auth-header="Authorization"
634638
The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.
635639
--proxy-url="" The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.
640+
--compress Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.
636641
```
637642

638643
### `stats` command reference
@@ -694,6 +699,7 @@ Flags:
694699
--auth-header="Authorization"
695700
The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.
696701
--proxy-url="" The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.
702+
--compress Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.
697703
--since=1h Lookback window.
698704
--from=FROM Start looking for logs at this absolute time (inclusive)
699705
--to=TO Stop looking for logs at this absolute time (exclusive)
@@ -761,6 +767,7 @@ Flags:
761767
--auth-header="Authorization"
762768
The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.
763769
--proxy-url="" The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.
770+
--compress Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.
764771
--since=1h Lookback window.
765772
--from=FROM Start looking for logs at this absolute time (inclusive)
766773
--to=TO Stop looking for logs at this absolute time (exclusive)
@@ -833,6 +840,7 @@ Flags:
833840
--auth-header="Authorization"
834841
The authorization header used. Can also be set using LOKI_AUTH_HEADER env var.
835842
--proxy-url="" The http or https proxy to use when making requests. Can also be set using LOKI_HTTP_PROXY_URL env var.
843+
--compress Request that Loki compress returned data in transit. Can also be set using LOKI_HTTP_COMPRESSION env var.
836844
--since=1h Lookback window.
837845
--from=FROM Start looking for logs at this absolute time (inclusive)
838846
--to=TO Stop looking for logs at this absolute time (exclusive)

pkg/logcli/client/client.go

+11
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type DefaultClient struct {
8888
AuthHeader string
8989
ProxyURL string
9090
BackoffConfig BackoffConfig
91+
Compression bool
9192
}
9293

9394
// Query uses the /api/v1/query endpoint to execute an instant query
@@ -320,6 +321,16 @@ func (c *DefaultClient) doRequest(path, query string, quiet bool, out interface{
320321
if c.Tripperware != nil {
321322
client.Transport = c.Tripperware(client.Transport)
322323
}
324+
if c.Compression {
325+
// NewClientFromConfig() above returns an http.Client that uses a transport which
326+
// has compression explicitly disabled. Here we re-enable it. If the caller
327+
// defines a custom Tripperware that isn't an http.Transport then this won't work,
328+
// but in that case they control the transport anyway and can configure
329+
// compression that way.
330+
if transport, ok := client.Transport.(*http.Transport); ok {
331+
transport.DisableCompression = false
332+
}
333+
}
323334

324335
var resp *http.Response
325336

0 commit comments

Comments
 (0)