Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

label matchers in labels API actually not work #5469

Closed
cssivision opened this issue Jul 5, 2022 · 1 comment
Closed

label matchers in labels API actually not work #5469

cssivision opened this issue Jul 5, 2022 · 1 comment

Comments

@cssivision
Copy link

cssivision commented Jul 5, 2022

I am using thanos 0.22.0 in work, using the prometheus api /api/v1/label/<label_name>/values query label values, but thanos query always return all label values no matter what i set the match[] field, source code below.

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"os"
	"time"
)

func main() {
	q := url.Values{
		"match[]": []string{`kube_node_labels{cluster="cluster1"}`},
		"start":   []string{fmt.Sprintf("%d", time.Now().Add(time.Hour*-1).Unix())},
		"end":     []string{fmt.Sprintf("%d", time.Now().Unix())},
	}
	fmt.Println(`http://127.0.0.1:8080/api/v1/label/node/values` + "?" + q.Encode())
	req, err := http.NewRequest(http.MethodPost, `http://127.0.0.1:8080/api/v1/label/node/values`+"?"+q.Encode(), nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	io.Copy(os.Stdout, resp.Body)
}

after spending sometime digging into the source code.

according to this PR,we can use matcher to select the label values. the implementation use http.Request.From to get field match[] without calling ParseForm. http.Request.From only available after ParseForm is called. which meaning the r.Form always be empty map.

type Request struct {
        // Form contains the parsed form data, including both the URL
	// field's query parameters and the PATCH, POST, or PUT form data.
	// This field is `only available after ParseForm is called`.
	// The HTTP client ignores Form and uses Body instead.
	Form url.Values
}

maybe we can just call ParseFrom before get match[] to fix it.

@yeya24
Copy link
Contributor

yeya24 commented Jul 7, 2022

Umm, I just tested this and it is working well in my environment.

For the code you mentioned, during https://github.com/thanos-io/thanos/blob/main/pkg/api/query/v1.go#L516, when we parse time params https://github.com/thanos-io/thanos/blob/main/pkg/api/query/v1.go#L938, we will call r.FormValue so the form should be parsed anyway.

I will close this issue as this is not a bug. Please check out your code and environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants