Tempo: Add test for backend data source (#31835)

This commit is contained in:
Zoltán Bedi 2021-03-11 08:18:24 +01:00 committed by GitHub
parent e7757b0175
commit 3b6168eb4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 12 deletions

View File

@ -23,6 +23,7 @@ type tempoExecutor struct {
httpClient *http.Client
}
// NewExecutor returns a tempoExecutor.
func NewExecutor(dsInfo *models.DataSource) (plugins.DataPlugin, error) {
httpClient, err := dsInfo.GetHttpClient()
if err != nil {
@ -42,22 +43,13 @@ func (e *tempoExecutor) DataQuery(ctx context.Context, dsInfo *models.DataSource
queryContext plugins.DataQuery) (plugins.DataResponse, error) {
refID := queryContext.Queries[0].RefID
queryResult := plugins.DataQueryResult{}
traceID := queryContext.Queries[0].Model.Get("query").MustString("")
tlog.Debug("Querying tempo with traceID", "traceID", traceID)
req, err := http.NewRequestWithContext(ctx, "GET", dsInfo.Url+"/api/traces/"+traceID, nil)
req, err := e.createRequest(ctx, dsInfo, traceID)
if err != nil {
return plugins.DataResponse{}, err
}
if dsInfo.BasicAuth {
req.SetBasicAuth(dsInfo.BasicAuthUser, dsInfo.DecryptedBasicAuthPassword())
}
req.Header.Set("Accept", "application/protobuf")
resp, err := e.httpClient.Do(req)
if err != nil {
return plugins.DataResponse{}, fmt.Errorf("failed get to tempo: %w", err)
@ -75,8 +67,7 @@ func (e *tempoExecutor) DataQuery(ctx context.Context, dsInfo *models.DataSource
}
if resp.StatusCode != http.StatusOK {
queryResult.Error = fmt.Errorf("failed to get trace: %s", traceID)
tlog.Error("Request to tempo failed", "Status", resp.Status, "Body", string(body))
queryResult.ErrorString = fmt.Sprintf("failed to get trace with id: %s Status: %s Body: %s", traceID, resp.Status, string(body))
return plugins.DataResponse{
Results: map[string]plugins.DataQueryResult{
refID: queryResult,
@ -130,3 +121,19 @@ func (e *tempoExecutor) DataQuery(ctx context.Context, dsInfo *models.DataSource
},
}, nil
}
func (e *tempoExecutor) createRequest(ctx context.Context, dsInfo *models.DataSource, traceID string) (*http.Request, error) {
req, err := http.NewRequestWithContext(ctx, "GET", dsInfo.Url+"/api/traces/"+traceID, nil)
if err != nil {
return nil, err
}
if dsInfo.BasicAuth {
req.SetBasicAuth(dsInfo.BasicAuthUser, dsInfo.DecryptedBasicAuthPassword())
}
req.Header.Set("Accept", "application/protobuf")
tlog.Debug("Tempo request", "url", req.URL.String(), "headers", req.Header)
return req, nil
}

View File

@ -0,0 +1,23 @@
package tempo
import (
"context"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTempo(t *testing.T) {
plug, err := NewExecutor(&models.DataSource{})
executor := plug.(*tempoExecutor)
require.NoError(t, err)
t.Run("createRequest should set Auth header when basic auth is true ", func(t *testing.T) {
req, err := executor.createRequest(context.Background(), &models.DataSource{BasicAuth: true, BasicAuthUser: "john", BasicAuthPassword: "pass"}, "traceID")
require.NoError(t, err)
assert.Equal(t, 2, len(req.Header))
assert.NotEqual(t, req.Header.Get("Authorization"), "")
})
}