Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: zirain <[email protected]>
  • Loading branch information
zirain committed Jul 19, 2024
1 parent febe4c6 commit a63b19d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 81 deletions.
81 changes: 0 additions & 81 deletions test/e2e/tests/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,11 @@ package tests
import (
"context"
"fmt"
"net"
"net/http"
"net/url"
"strings"
"testing"
"time"

"github.com/go-logfmt/logfmt"
"github.com/gogo/protobuf/jsonpb" // nolint: depguard // tempopb use gogo/protobuf
"github.com/grafana/tempo/pkg/tempopb"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
httputils "sigs.k8s.io/gateway-api/conformance/utils/http"
"sigs.k8s.io/gateway-api/conformance/utils/kubernetes"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
Expand Down Expand Up @@ -149,75 +140,3 @@ var ZipkinTracingTest = suite.ConformanceTest{
})
},
}

// QueryTraceFromTempo queries span count from tempo
// TODO: move to utils package if needed
func QueryTraceFromTempo(t *testing.T, c client.Client, tags map[string]string) (int, error) {
svc := corev1.Service{}
if err := c.Get(context.Background(), types.NamespacedName{
Namespace: "monitoring",
Name: "tempo",
}, &svc); err != nil {
return -1, err
}
host := ""
for _, ing := range svc.Status.LoadBalancer.Ingress {
if ing.IP != "" {
host = ing.IP
break
}
}

tagsQueryParam, err := createTagsQueryParam(tags)
if err != nil {
return -1, err
}

tempoURL := url.URL{
Scheme: "http",
Host: net.JoinHostPort(host, "3100"),
Path: "/api/search",
}
query := tempoURL.Query()
query.Add("start", fmt.Sprintf("%d", time.Now().Add(-10*time.Minute).Unix())) // query traces from last 10 minutes
query.Add("end", fmt.Sprintf("%d", time.Now().Unix()))
query.Add("tags", tagsQueryParam)
tempoURL.RawQuery = query.Encode()

req, err := http.NewRequest("GET", tempoURL.String(), nil)
if err != nil {
return -1, err
}

t.Logf("send request to %s", tempoURL.String())
res, err := http.DefaultClient.Do(req)
if err != nil {
return -1, err
}

if res.StatusCode != http.StatusOK {
return -1, fmt.Errorf("failed to query tempo, url=%s, status=%s", tempoURL.String(), res.Status)
}

tempoResponse := &tempopb.SearchResponse{}
if err := jsonpb.Unmarshal(res.Body, tempoResponse); err != nil {
return -1, err
}

total := len(tempoResponse.Traces)
t.Logf("get response from tempo, url=%s, response=%v, total=%d", tempoURL.String(), tempoResponse, total)
return total, nil
}

// copy from https://github.com/grafana/tempo/blob/c0127c78c368319433c7c67ca8967adbfed2259e/cmd/tempo-query/tempo/plugin.go#L361
func createTagsQueryParam(tags map[string]string) (string, error) {
tagsBuilder := &strings.Builder{}
tagsEncoder := logfmt.NewEncoder(tagsBuilder)
for k, v := range tags {
err := tagsEncoder.EncodeKeyval(k, v)
if err != nil {
return "", err
}
}
return tagsBuilder.String(), nil
}
73 changes: 73 additions & 0 deletions test/e2e/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/go-logfmt/logfmt"
"github.com/grafana/tempo/pkg/tempopb"
"io"
"net"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -494,3 +497,73 @@ type LokiQueryResponse struct {
}
}
}

// QueryTraceFromTempo queries span count from tempo
func QueryTraceFromTempo(t *testing.T, c client.Client, tags map[string]string) (int, error) {
svc := corev1.Service{}
if err := c.Get(context.Background(), types.NamespacedName{
Namespace: "monitoring",
Name: "tempo",
}, &svc); err != nil {
return -1, err
}
host := ""
for _, ing := range svc.Status.LoadBalancer.Ingress {
if ing.IP != "" {
host = ing.IP
break
}
}

tagsQueryParam, err := createTagsQueryParam(tags)
if err != nil {
return -1, err
}

tempoURL := url.URL{
Scheme: "http",
Host: net.JoinHostPort(host, "3100"),
Path: "/api/search",
}
query := tempoURL.Query()
query.Add("start", fmt.Sprintf("%d", time.Now().Add(-10*time.Minute).Unix())) // query traces from last 10 minutes
query.Add("tags", tagsQueryParam)
tempoURL.RawQuery = query.Encode()

req, err := http.NewRequest("GET", tempoURL.String(), nil)
if err != nil {
return -1, err
}

t.Logf("send request to %s", tempoURL.String())
res, err := http.DefaultClient.Do(req)
if err != nil {
return -1, err
}

if res.StatusCode != http.StatusOK {
return -1, fmt.Errorf("failed to query tempo, url=%s, status=%s", tempoURL.String(), res.Status)
}

tempoResponse := &tempopb.SearchResponse{}
if err := jsonpb.Unmarshal(res.Body, tempoResponse); err != nil {

Check failure on line 549 in test/e2e/tests/utils.go

View workflow job for this annotation

GitHub Actions / coverage-test

undefined: jsonpb

Check failure on line 549 in test/e2e/tests/utils.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jsonpb (typecheck)

Check failure on line 549 in test/e2e/tests/utils.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jsonpb) (typecheck)

Check failure on line 549 in test/e2e/tests/utils.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jsonpb) (typecheck)

Check failure on line 549 in test/e2e/tests/utils.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jsonpb) (typecheck)

Check failure on line 549 in test/e2e/tests/utils.go

View workflow job for this annotation

GitHub Actions / lint

undefined: jsonpb) (typecheck)
return -1, err
}

total := len(tempoResponse.Traces)
t.Logf("get response from tempo, url=%s, response=%v, total=%d", tempoURL.String(), tempoResponse, total)
return total, nil
}

// copy from https://github.com/grafana/tempo/blob/c0127c78c368319433c7c67ca8967adbfed2259e/cmd/tempo-query/tempo/plugin.go#L361
func createTagsQueryParam(tags map[string]string) (string, error) {
tagsBuilder := &strings.Builder{}
tagsEncoder := logfmt.NewEncoder(tagsBuilder)
for k, v := range tags {
err := tagsEncoder.EncodeKeyval(k, v)
if err != nil {
return "", err
}
}
return tagsBuilder.String(), nil
}

0 comments on commit a63b19d

Please sign in to comment.