mirror of
https://github.com/grafana/grafana.git
synced 2025-01-27 08:47:12 -06:00
Tempo: Add test for backend data source (#31835)
This commit is contained in:
parent
e7757b0175
commit
3b6168eb4a
@ -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
|
||||
}
|
||||
|
23
pkg/tsdb/tempo/tempo_test.go
Normal file
23
pkg/tsdb/tempo/tempo_test.go
Normal 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"), "")
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user