Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
## Unreleased
- [#3259](https://github.com/thanos-io/thanos/pull/3259) Thanos BlockViewer: Added a button in the blockviewer that allows users to download the metadata of a block.
- [#3261](https://github.com/thanos-io/thanos/pull/3261) Thanos Store: Use segment files specified in meta.json file, if present. If not present, Store does the LIST operation as before.
- [#3276](https://github.com/thanos-io/thanos/pull/3276) Query Frontend: Support query splitting and retry for labels and series requests.
- [#3276](https://github.com/thanos-io/thanos/pull/3276) Query Frontend: Support query splitting and retry for label names, label values and series requests.
- [#3315](https://github.com/thanos-io/thanos/pull/3315) Query Frontend: Support results caching for label names, label values and series requests.

### Fixed
- [#3257](https://github.com/thanos-io/thanos/pull/3257) Ruler: Prevent Ruler from crashing when using default DNS to lookup hosts that results in "No such hosts" errors.
Expand Down
17 changes: 17 additions & 0 deletions cmd/thanos/query_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ func registerQueryFrontend(app *extkingpin.App) {
cmd.Flag("labels.default-time-range", "The default metadata time range duration for retrieving labels through Labels and Series API when the range parameters are not specified.").
Default("24h").DurationVar(&cfg.DefaultTimeRange)

cfg.LabelsConfig.CachePathOrContent = *extflag.RegisterPathOrContent(cmd, "labels.response-cache-config", "YAML file that contains response cache configuration.", false)

cmd.Flag("cache-compression-type", "Use compression in results cache. Supported values are: 'snappy' and '' (disable compression).").
Default("").StringVar(&cfg.CacheCompression)

Expand Down Expand Up @@ -138,6 +140,21 @@ func runQueryFrontend(
}
}

labelsCacheConfContentYaml, err := cfg.LabelsConfig.CachePathOrContent.Content()
if err != nil {
return err
}
if len(labelsCacheConfContentYaml) > 0 {
cacheConfig, err := queryfrontend.NewCacheConfig(logger, queryRangeCacheConfContentYaml)
if err != nil {
return errors.Wrap(err, "initializing the labels cache config")
}
cfg.LabelsConfig.ResultsCacheConfig = &queryrange.ResultsCacheConfig{
Compression: cfg.CacheCompression,
CacheConfig: *cacheConfig,
}
}

if err := cfg.Validate(); err != nil {
return errors.Wrap(err, "error validating the config")
}
Expand Down
8 changes: 8 additions & 0 deletions docs/components/query-frontend.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ Flags:
The default metadata time range duration for
retrieving labels through Labels and Series API
when the range parameters are not specified.
--labels.response-cache-config-file=<file-path>
Path to YAML file that contains response cache
configuration.
--labels.response-cache-config=<content>
Alternative to
'labels.response-cache-config-file' flag (lower
priority). Content of YAML file that contains
response cache configuration.
--cache-compression-type=""
Use compression in results cache. Supported
values are: 'snappy' and ” (disable
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/cespare/xxhash v1.1.0
github.com/chromedp/cdproto v0.0.0-20200424080200-0de008e41fa0
github.com/chromedp/chromedp v0.5.3
github.com/cortexproject/cortex v1.3.1-0.20200923145333-8587ea61fe17
github.com/cortexproject/cortex v1.4.1-0.20201013144911-21bad57b346c
github.com/davecgh/go-spew v1.1.1
github.com/facette/natsort v0.0.0-20181210072756-2cd4dd1e2dcb
github.com/fatih/structtag v1.1.0
Expand Down
20 changes: 11 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,9 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f h1:lBNOc5arjvs8E5mO2tbp
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cortexproject/cortex v0.6.1-0.20200228110116-92ab6cbe0995/go.mod h1:3Xa3DjJxtpXqxcMGdk850lcIRb81M0fyY1MQ6udY134=
github.com/cortexproject/cortex v1.2.1-0.20200805064754-d8edc95e2c91/go.mod h1:PVPxNLrxKH+yc8asaJOxuz7TiRmMizFfnSMOnRzM6oM=
github.com/cortexproject/cortex v1.3.1-0.20200923145333-8587ea61fe17 h1:69LF7OuwaAS/h3GGJUBcI1Y9ZAFEcUpBZSpbHLK1eyc=
github.com/cortexproject/cortex v1.3.1-0.20200923145333-8587ea61fe17/go.mod h1:dJ9gpW7dzQ7z09cKtNN9PfebumgyO4dtNdFQ6eQEed0=
github.com/cortexproject/cortex v1.3.1-0.20200901115931-255ff3306960/go.mod h1:ub8BpRZrRa02BOM8NJTnI2YklxW/mGhEkJDrhsDfcfg=
github.com/cortexproject/cortex v1.4.1-0.20201013144911-21bad57b346c h1:4Qcgdgk3cFAFKufkcRQBn82ngaij97+M+C7jBifPt/A=
github.com/cortexproject/cortex v1.4.1-0.20201013144911-21bad57b346c/go.mod h1:bDhuzYyoL2kC0taY6nMdfv13kKYlUVoiQuy6QnkAIZw=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/cznic/b v0.0.0-20180115125044-35e9bbe41f07/go.mod h1:URriBxXwVq5ijiJ12C7iIZqlA69nTlI+LgI6/pwftG8=
Expand Down Expand Up @@ -375,8 +376,8 @@ github.com/go-openapi/validate v0.19.2/go.mod h1:1tRCw7m3jtI8eNWEEliiAqUIcBztB2K
github.com/go-openapi/validate v0.19.3/go.mod h1:90Vh6jjkTn+OT1Eefm0ZixWNFjhtOH7vS9k0lo6zwJo=
github.com/go-openapi/validate v0.19.8 h1:YFzsdWIDfVuLvIOF+ZmKjVg1MbPJ1QgY9PihMwei1ys=
github.com/go-openapi/validate v0.19.8/go.mod h1:8DJv2CVJQ6kGNpFW6eV9N3JviE1C85nY1c2z52x1Gk4=
github.com/go-redis/redis/v8 v8.0.0-beta.10.0.20200905143926-df7fe4e2ce72 h1:HJkWCywZsCtt//EFYNtHAOQglik0kzonhiilQCrQEgs=
github.com/go-redis/redis/v8 v8.0.0-beta.10.0.20200905143926-df7fe4e2ce72/go.mod h1:CJP1ZIHwhosNYwIdaHPZK9vHsM3+roNBaZ7U9Of1DXc=
github.com/go-redis/redis/v8 v8.2.3 h1:eNesND+DWt/sjQOtPFxAbQkTIXaXX00qNLxjVWkZ70k=
github.com/go-redis/redis/v8 v8.2.3/go.mod h1:ysgGY09J/QeDYbu3HikWEIPCwaeOkuNoTgKayTEaEOw=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
Expand Down Expand Up @@ -809,14 +810,15 @@ github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk=
github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA=
github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/ginkgo v1.14.1 h1:jMU0WaQrP0a/YAEq8eJmJKjBoMs+pClEr1vDMlM/Do4=
github.com/onsi/ginkgo v1.14.1/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY=
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE=
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.10.2 h1:aY/nuoWlKJud2J6U0E3NWsjlg+0GtwXxgEqthRdzlcs=
github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/opencontainers/go-digest v1.0.0-rc1/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
Expand Down Expand Up @@ -1002,6 +1004,7 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/thanos-io/thanos v0.8.1-0.20200109203923-552ffa4c1a0d/go.mod h1:usT/TxtJQ7DzinTt+G9kinDQmRS5sxwu0unVKZ9vdcw=
github.com/thanos-io/thanos v0.13.1-0.20200731083140-69b87607decf/go.mod h1:G8caR6G7pSDreRDvFm9wFuyjEBztmr8Ag3kBYpa/fEc=
github.com/thanos-io/thanos v0.13.1-0.20200807203500-9b578afb4763/go.mod h1:KyW0a93tsh7v4hXAwo2CVAIRYuZT1Kkf4e04gisQjAg=
github.com/thanos-io/thanos v0.13.1-0.20200923175059-57035bf8f843/go.mod h1:U7HVxfAHYptOk9xCuxr8WoILGL1wWdXVqZD3t6JifNA=
github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab h1:7ZR3hmisBWw77ZpO1/o86g+JV3VKlk3d48jopJxzTjU=
github.com/themihai/gomemcache v0.0.0-20180902122335-24332e2d58ab/go.mod h1:eheTFp954zcWZXCU8d0AT76ftsQOTo4DTqkN/h3k1MY=
github.com/tidwall/pretty v0.0.0-20180105212114-65a9db5fad51/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
Expand All @@ -1025,6 +1028,7 @@ github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtX
github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw=
github.com/weaveworks/common v0.0.0-20200206153930-760e36ae819a/go.mod h1:6enWAqfQBFrE8X/XdJwZr8IKgh1chStuFR0mjU/UOUw=
github.com/weaveworks/common v0.0.0-20200625145055-4b1847531bc9/go.mod h1:c98fKi5B9u8OsKGiWHLRKus6ToQ1Tubeow44ECO1uxY=
github.com/weaveworks/common v0.0.0-20200820123129-280614068c5e/go.mod h1:hz10LOsAdzC3K/iXaKoFxOKTDRgxJl+BTGX1GY+TzO4=
github.com/weaveworks/common v0.0.0-20200914083218-61ffdd448099 h1:MS5M2antM8wzMUqVxIfAi+yb6yjXvDINRFvLnmNXeIw=
github.com/weaveworks/common v0.0.0-20200914083218-61ffdd448099/go.mod h1:hz10LOsAdzC3K/iXaKoFxOKTDRgxJl+BTGX1GY+TzO4=
github.com/weaveworks/promrus v1.2.0 h1:jOLf6pe6/vss4qGHjXmGz4oDJQA+AOCqEL3FvvZGz7M=
Expand Down Expand Up @@ -1135,8 +1139,6 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20200821190819-94841d0725da h1:vfV2BR+q1+/jmgJR30Ms3RHbryruQ3Yd83lLAAue9cs=
golang.org/x/exp v0.0.0-20200821190819-94841d0725da/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ func newThanosCacheKeyGenerator(interval time.Duration) thanosCacheKeyGenerator
// GenerateCacheKey generates a cache key based on the Request and interval.
func (t thanosCacheKeyGenerator) GenerateCacheKey(_ string, r queryrange.Request) string {
currentInterval := r.GetStart() / t.interval.Milliseconds()
if tr, ok := r.(*ThanosQueryRangeRequest); ok {
switch tr := r.(type) {
case *ThanosQueryRangeRequest:
i := 0
for ; i < len(t.resolutions) && t.resolutions[i] > tr.MaxSourceResolution; i++ {
}
return fmt.Sprintf("%s:%d:%d:%d", tr.Query, tr.Step, currentInterval, i)
case *ThanosLabelsRequest:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about label names?

Copy link
Contributor Author

@yeya24 yeya24 Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThanosLabelsRequest already covers both label_values query and label_names query. If it is a label_names query, then tr.Label is just an empty string. For label_values query, that's the queried label name.

I am using the same struct for them since their query response is the same []string

return fmt.Sprintf("%s:%d", tr.Label, currentInterval)
case *ThanosSeriesRequest:
return fmt.Sprintf("%s:%d", tr.Matchers, currentInterval)
}
return fmt.Sprintf("%s:%d:%d", r.GetQuery(), r.GetStep(), currentInterval)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we error here?

Copy link
Contributor Author

@yeya24 yeya24 Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure but this shouldn't happen. It would error out in the first place when decoding the HTTP request if the request is not valid.

}
13 changes: 11 additions & 2 deletions pkg/queryfrontend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,19 @@ type LabelsConfig struct {
func (cfg *Config) Validate() error {
if cfg.QueryRangeConfig.ResultsCacheConfig != nil {
if cfg.QueryRangeConfig.SplitQueriesByInterval <= 0 {
return errors.New("split queries interval should be greater than 0")
return errors.New("split queries interval should be greater than 0 when caching is enabled")
}
if err := cfg.QueryRangeConfig.ResultsCacheConfig.Validate(); err != nil {
return errors.Wrap(err, "invalid ResultsCache config")
return errors.Wrap(err, "invalid ResultsCache config for query_range tripperware")
}
}

if cfg.LabelsConfig.ResultsCacheConfig != nil {
if cfg.LabelsConfig.SplitQueriesByInterval <= 0 {
return errors.New("split queries interval should be greater than 0 when caching is enabled")
}
if err := cfg.LabelsConfig.ResultsCacheConfig.Validate(); err != nil {
return errors.Wrap(err, "invalid ResultsCache config for labels tripperware")
}
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/queryfrontend/labels_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,18 @@ func (c labelsCodec) DecodeResponse(ctx context.Context, r *http.Response, req q
if err := json.Unmarshal(buf, &resp); err != nil {
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error decoding response: %v", err)
}
for h, hv := range r.Header {
resp.Headers = append(resp.Headers, &ResponseHeader{Name: h, Values: hv})
}
return &resp, nil
case *ThanosSeriesRequest:
var resp ThanosSeriesResponse
if err := json.Unmarshal(buf, &resp); err != nil {
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error decoding response: %v", err)
}
for h, hv := range r.Header {
resp.Headers = append(resp.Headers, &ResponseHeader{Name: h, Values: hv})
}
return &resp, nil
default:
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "invalid request type")
Expand Down
10 changes: 6 additions & 4 deletions pkg/queryfrontend/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ func (r *ThanosLabelsRequest) GetStart() int64 { return r.Start }
// GetEnd returns the end timestamp of the request in milliseconds.
func (r *ThanosLabelsRequest) GetEnd() int64 { return r.End }

// GetStep returns the step of the request in milliseconds.
func (r *ThanosLabelsRequest) GetStep() int64 { return 0 }
// GetStep returns the step of the request in milliseconds. Returns 1 is a trick to avoid panic in
// https://github.com/cortexproject/cortex/blob/master/pkg/querier/queryrange/results_cache.go#L447.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(: Yea it's getting very tricky without Cortex actually support our case, and it would be nice to help them as well 🤔
This is fine for now, but wonder how long term solution of truly shared code with cortex would looks like

Copy link
Contributor Author

@yeya24 yeya24 Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I will take a look later. Would be good to have these query APIs natively supported on Cortex then we can re-use.

func (r *ThanosLabelsRequest) GetStep() int64 { return 1 }

// GetQuery returns the query of the request.
func (r *ThanosLabelsRequest) GetQuery() string { return "" }
Expand Down Expand Up @@ -183,8 +184,9 @@ func (r *ThanosSeriesRequest) GetStart() int64 { return r.Start }
// GetEnd returns the end timestamp of the request in milliseconds.
func (r *ThanosSeriesRequest) GetEnd() int64 { return r.End }

// GetStep returns the step of the request in milliseconds.
func (r *ThanosSeriesRequest) GetStep() int64 { return 0 }
// GetStep returns the step of the request in milliseconds. Returns 1 is a trick to avoid panic in
// https://github.com/cortexproject/cortex/blob/master/pkg/querier/queryrange/results_cache.go#L447.
func (r *ThanosSeriesRequest) GetStep() int64 { return 1 }

// GetQuery returns the query of the request.
func (r *ThanosSeriesRequest) GetQuery() string { return "" }
Expand Down
46 changes: 46 additions & 0 deletions pkg/queryfrontend/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.

package queryfrontend

import (
"unsafe"

"github.com/cortexproject/cortex/pkg/querier/queryrange"
)

// ThanosResponseExtractor helps extracting specific info from Query Response.
type ThanosResponseExtractor struct{}

// Extract extracts response for specific a range from a response.
// This interface is not used for labels and series responses.
func (ThanosResponseExtractor) Extract(_, _ int64, resp queryrange.Response) queryrange.Response {
return resp
}

// ResponseWithoutHeaders returns the response without HTTP headers.
func (ThanosResponseExtractor) ResponseWithoutHeaders(resp queryrange.Response) queryrange.Response {
switch tr := resp.(type) {
case *ThanosLabelsResponse:
return &ThanosLabelsResponse{Status: queryrange.StatusSuccess, Data: tr.Data}
case *ThanosSeriesResponse:
return &ThanosSeriesResponse{Status: queryrange.StatusSuccess, Data: tr.Data}
}
return resp
}

// headersToQueryRangeHeaders convert slice of ResponseHeader to Cortex queryrange.PrometheusResponseHeader in an
// unsafe manner. It reuses the same memory.
func headersToQueryRangeHeaders(headers []*ResponseHeader) []*queryrange.PrometheusResponseHeader {
return *(*[]*queryrange.PrometheusResponseHeader)(unsafe.Pointer(&headers))
}

// GetHeaders returns the HTTP headers in the response.
func (m *ThanosLabelsResponse) GetHeaders() []*queryrange.PrometheusResponseHeader {
return headersToQueryRangeHeaders(m.Headers)
}

// GetHeaders returns the HTTP headers in the response.
func (m *ThanosSeriesResponse) GetHeaders() []*queryrange.PrometheusResponseHeader {
return headersToQueryRangeHeaders(m.Headers)
}
Loading