Tempo: Add support for /api/v2/traces/ and partial traces in Trace View (#94267)

* Request trace by id using v2, fallback for v1 when 404

* Show partial traces badge in Trace View

* update go work sum

* Fix tests

* some linting

* Fix tests and try to ignore linter

* Move no lint

* nolint:bodyclose

* merge main

* Fix null tags in array

* Fix test

* Update go.sum

* Update go.sum
This commit is contained in:
Andre Pereira
2024-10-18 15:10:22 +03:00
committed by GitHub
parent 75fe42876a
commit bbe7029d0a
13 changed files with 1785 additions and 204 deletions
+3 -3
View File
@@ -108,12 +108,12 @@ require (
github.com/sosodev/duration v1.2.0 // indirect
github.com/vektah/gqlparser/v2 v2.5.11 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240518090000-14441aefdf88 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.2.0-alpha // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.31.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.31.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 // indirect
go.opentelemetry.io/otel/log v0.2.0-alpha // indirect
go.opentelemetry.io/otel/sdk/log v0.2.0-alpha // indirect
go.opentelemetry.io/otel/log v0.4.0 // indirect
go.opentelemetry.io/otel/sdk/log v0.4.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e // indirect
gotest.tools/v3 v3.5.1 // indirect
+6 -6
View File
@@ -233,22 +233,22 @@ go.opentelemetry.io/otel v1.31.0 h1:NsJcKPIW0D0H3NgzPDHmo0WW6SptzPdqg/L1zsIm2hY=
go.opentelemetry.io/otel v1.31.0/go.mod h1:O0C14Yl9FgkjqcCZAsE053C13OaddMYr/hz6clDkEJE=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240518090000-14441aefdf88 h1:oM0GTNKGlc5qHctWeIGTVyda4iFFalOzMZ3Ehj5rwB4=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240518090000-14441aefdf88/go.mod h1:JGG8ebaMO5nXOPnvKEl+DiA4MGwFjCbjsxT1WHIEBPY=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.2.0-alpha h1:z2s6Zba+OUyayRv5m1AXWNUTGh57K1iMhy6emU5QT5Y=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.2.0-alpha/go.mod h1:paOXXyUgPW6jYxYkP0pB47H2zHE1fPvMJ4E4G9LHOi0=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0 h1:zBPZAISA9NOc5cE8zydqDiS0itvg/P/0Hn9m72a5gvM=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0/go.mod h1:gcj2fFjEsqpV3fXuzAA+0Ze1p2/4MJ4T7d77AmkvueQ=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.31.0 h1:K0XaT3DwHAcV4nKLzcQvwAgSyisUghWoY20I7huthMk=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.31.0/go.mod h1:B5Ki776z/MBnVha1Nzwp5arlzBbE3+1jk+pGmaP5HME=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.31.0 h1:FFeLy03iVTXP6ffeN2iXrxfGsZGCjVx0/4KlizjyBwU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.31.0/go.mod h1:TMu73/k1CP8nBUpDLc71Wj/Kf7ZS9FK5b53VapRsP9o=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 h1:JAv0Jwtl01UFiyWZEMiJZBiTlv5A50zNs8lsthXqIio=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0/go.mod h1:QNKLmUEAq2QUbPQUfvw4fmv0bgbK7UlOSFCnXyfvSNc=
go.opentelemetry.io/otel/log v0.2.0-alpha h1:ixOPvMzserpqA07SENHvRzkZOsnG0XbPr74hv1AQ+n0=
go.opentelemetry.io/otel/log v0.2.0-alpha/go.mod h1:vbFZc65yq4c4ssvXY43y/nIqkNJLxORrqw0L85P59LA=
go.opentelemetry.io/otel/log v0.4.0 h1:/vZ+3Utqh18e8TPjuc3ecg284078KWrR8BRz+PQAj3o=
go.opentelemetry.io/otel/log v0.4.0/go.mod h1:DhGnQvky7pHy82MIRV43iXh3FlKN8UUKftn0KbLOq6I=
go.opentelemetry.io/otel/metric v1.31.0 h1:FSErL0ATQAmYHUIzSezZibnyVlft1ybhy4ozRPcF2fE=
go.opentelemetry.io/otel/metric v1.31.0/go.mod h1:C3dEloVbLuYoX41KpmAhOqNriGbA+qqH6PQ5E5mUfnY=
go.opentelemetry.io/otel/sdk v1.31.0 h1:xLY3abVHYZ5HSfOg3l2E5LUj2Cwva5Y7yGxnSW9H5Gk=
go.opentelemetry.io/otel/sdk v1.31.0/go.mod h1:TfRbMdhvxIIr/B2N2LQW2S5v9m3gOQ/08KsbbO5BPT0=
go.opentelemetry.io/otel/sdk/log v0.2.0-alpha h1:jGTkL/jroJ31jnP6jDl34N/mDOfRGGYZHcHsCM+5kWA=
go.opentelemetry.io/otel/sdk/log v0.2.0-alpha/go.mod h1:Hd8Lw9FPGUM3pfY7iGMRvFaC2Nyau4Ajb5WnQ9OdIho=
go.opentelemetry.io/otel/sdk/log v0.4.0 h1:1mMI22L82zLqf6KtkjrRy5BbagOTWdJsqMY/HSqILAA=
go.opentelemetry.io/otel/sdk/log v0.4.0/go.mod h1:AYJ9FVF0hNOgAVzUG/ybg/QttnXhUePWAupmCqtdESo=
go.opentelemetry.io/otel/trace v1.31.0 h1:ffjsj1aRouKewfr85U2aGagJ46+MvodynlQ1HYdmJys=
go.opentelemetry.io/otel/trace v1.31.0/go.mod h1:TXZkRk7SM2ZQLtR6eoAWQFIHPvzQ06FJAsO1tJg480A=
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
+2 -2
View File
@@ -19,7 +19,7 @@ require (
require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 // indirect
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
github.com/apache/arrow/go/v15 v15.0.2 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aws/aws-sdk-go v1.55.5 // indirect
@@ -57,7 +57,7 @@ require (
github.com/gorilla/mux v1.8.1 // indirect
github.com/grafana/otel-profiling-go v0.5.1 // indirect
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect
github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db // indirect
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
+4 -5
View File
@@ -2,8 +2,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9 h1:ez/4by2iGztzR4L0zgAOR8lTQK9VlyBVVd7G4omaOQs=
github.com/alecthomas/units v0.0.0-20231202071711-9a357b53e9c9/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 h1:t3eaIm0rUkzbrIewtiFmMK5RXHej2XnoXNhxVsAYUfg=
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30/go.mod h1:fvzegU4vN3H1qMT+8wDmzjAcDONcgo2/SZ/TyfdUOFs=
github.com/apache/arrow/go/v15 v15.0.2 h1:60IliRbiyTWCWjERBCkO1W4Qun9svcYoZrSLcyOsMLE=
github.com/apache/arrow/go/v15 v15.0.2/go.mod h1:DGXsR3ajT524njufqf95822i+KTh+yea1jass9YXgjA=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
@@ -104,8 +104,8 @@ github.com/grafana/otel-profiling-go v0.5.1 h1:stVPKAFZSa7eGiqbYuG25VcqYksR6iWvF
github.com/grafana/otel-profiling-go v0.5.1/go.mod h1:ftN/t5A/4gQI19/8MoWurBEtC6gFw8Dns1sJZ9W4Tls=
github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg=
github.com/grafana/pyroscope-go/godeltaprof v0.1.8/go.mod h1:2+l7K7twW49Ct4wFluZD3tZ6e0SjanjcUUBPVD/UuGU=
github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db h1:7aN5cccjIqCLTzedH7MZzRZt5/lsAHch6Z3L2ZGn5FA=
github.com/grafana/regexp v0.0.0-20221123153739-15dc172cd2db/go.mod h1:M5qHK+eWfAv8VR/265dIuEpL3fNfeC21tXXp9itM24A=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc h1:GN2Lv3MGO7AS6PrRoT6yV5+wkrOpcszoIsO4+4ds248=
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc/go.mod h1:+JKpmjMGhpgPL+rXZ5nsZieVzvarn86asRlBg4uNGnk=
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1 h1:qnpSQwGEnkcRpTqNOIR6bJbR0gAorgP9CSALpRcKoAA=
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1/go.mod h1:lXGCsh6c22WGtjr+qGHj1otzZpV/1kwTMAqkwZsnWRU=
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 h1:pRhl55Yx1eC7BZ1N+BBWwnKaMyD8uC+34TLdndZMAKk=
@@ -233,7 +233,6 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Binary file not shown.
File diff suppressed because it is too large Load Diff
+106 -41
View File
@@ -7,11 +7,13 @@ import (
"io"
"net/http"
"github.com/gogo/protobuf/proto"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/tracing"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/tsdb/tempo/kinds/dataquery"
"go.opentelemetry.io/collector/pdata/ptrace"
"github.com/grafana/tempo/pkg/tempopb"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
@@ -48,12 +50,92 @@ func (s *Service) getTrace(ctx context.Context, pCtx backend.PluginContext, quer
return result, err
}
request, err := s.createRequest(ctx, dsInfo, *model.Query, query.TimeRange.From.Unix(), query.TimeRange.To.Unix())
var apiVersion = TraceRequestApiVersionV2
//nolint:bodyclose
resp, traceBody, err := s.performTraceRequest(ctx, dsInfo, apiVersion, model, query, span)
if err != nil {
return result, err
}
// If the endpoint is not found, try the v1 endpoint, we might be communicating with an older Tempo version
if resp.StatusCode == http.StatusNotFound {
apiVersion = TraceRequestApiVersionV1
//nolint:bodyclose
resp, traceBody, err = s.performTraceRequest(ctx, dsInfo, apiVersion, model, query, span)
if err != nil {
return result, err
}
}
if resp.StatusCode != http.StatusOK {
ctxLogger.Error("Failed to get trace", "error", err, "function", logEntrypoint())
result.Error = fmt.Errorf("failed to get trace with id: %s Status: %s Body: %s", *model.Query, resp.Status, string(traceBody))
span.RecordError(result.Error)
span.SetStatus(codes.Error, result.Error.Error())
return result, nil
}
var frame *data.Frame
if apiVersion == TraceRequestApiVersionV1 {
var otTrace tempopb.Trace
err = proto.Unmarshal(traceBody, &otTrace)
if err != nil {
ctxLogger.Error("Failed to convert tempo response to Otlp", "error", err, "function", logEntrypoint())
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return &backend.DataResponse{}, fmt.Errorf("failed to convert tempo response to Otlp: %w", err)
}
frame, err = TraceToFrame(otTrace.GetResourceSpans())
if err != nil {
ctxLogger.Error("Failed to transform trace to data frame", "error", err, "function", logEntrypoint())
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return &backend.DataResponse{}, fmt.Errorf("failed to transform trace %v to data frame: %w", model.Query, err)
}
} else {
var tr tempopb.TraceByIDResponse
err = proto.Unmarshal(traceBody, &tr)
if err != nil {
ctxLogger.Error("Failed to convert tempo response to Otlp", "error", err, "function", logEntrypoint())
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return &backend.DataResponse{}, fmt.Errorf("failed to convert tempo response to Otlp: %w", err)
}
frame, err = TraceToFrame(tr.Trace.ResourceSpans)
if err != nil {
ctxLogger.Error("Failed to transform trace to data frame", "error", err, "function", logEntrypoint())
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return &backend.DataResponse{}, fmt.Errorf("failed to transform trace %v to data frame: %w", model.Query, err)
}
frame.Meta.Custom = map[string]interface{}{
"partial": tr.GetStatus() == tempopb.TraceByIDResponse_PARTIAL,
"message": tr.GetMessage(),
}
}
frame.RefID = refID
frames := []*data.Frame{frame}
result.Frames = frames
ctxLogger.Debug("Successfully got trace", "function", logEntrypoint())
return result, nil
}
func (s *Service) performTraceRequest(ctx context.Context, dsInfo *Datasource, apiVersion TraceRequestApiVersion, model *dataquery.TempoQuery, query backend.DataQuery, span trace.Span) (*http.Response, []byte, error) {
ctxLogger := s.logger.FromContext(ctx)
request, err := s.createRequest(ctx, dsInfo, apiVersion, *model.Query, query.TimeRange.From.Unix(), query.TimeRange.To.Unix())
if err != nil {
ctxLogger.Error("Failed to create request", "error", err, "function", logEntrypoint())
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return result, err
return nil, nil, err
}
resp, err := dsInfo.HTTPClient.Do(request)
@@ -61,7 +143,7 @@ func (s *Service) getTrace(ctx context.Context, pCtx backend.PluginContext, quer
ctxLogger.Error("Failed to send request to Tempo", "error", err, "function", logEntrypoint())
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return result, fmt.Errorf("failed get to tempo: %w", err)
return nil, nil, fmt.Errorf("failed get to tempo: %w", err)
}
defer func() {
@@ -73,50 +155,33 @@ func (s *Service) getTrace(ctx context.Context, pCtx backend.PluginContext, quer
body, err := io.ReadAll(resp.Body)
if err != nil {
ctxLogger.Error("Failed to read response body", "error", err, "function", logEntrypoint())
return &backend.DataResponse{}, err
return nil, nil, err
}
if resp.StatusCode != http.StatusOK {
ctxLogger.Error("Failed to get trace", "error", err, "function", logEntrypoint())
result.Error = fmt.Errorf("failed to get trace with id: %s Status: %s Body: %s", *model.Query, resp.Status, string(body))
span.RecordError(result.Error)
span.SetStatus(codes.Error, result.Error.Error())
return result, nil
}
pbUnmarshaler := ptrace.ProtoUnmarshaler{}
otTrace, err := pbUnmarshaler.UnmarshalTraces(body)
if err != nil {
ctxLogger.Error("Failed to convert tempo response to Otlp", "error", err, "function", logEntrypoint())
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return &backend.DataResponse{}, fmt.Errorf("failed to convert tempo response to Otlp: %w", err)
}
frame, err := TraceToFrame(otTrace)
if err != nil {
ctxLogger.Error("Failed to transform trace to data frame", "error", err, "function", logEntrypoint())
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
return &backend.DataResponse{}, fmt.Errorf("failed to transform trace %v to data frame: %w", model.Query, err)
}
frame.RefID = refID
frames := []*data.Frame{frame}
result.Frames = frames
ctxLogger.Debug("Successfully got trace", "function", logEntrypoint())
return result, nil
return resp, body, nil
}
func (s *Service) createRequest(ctx context.Context, dsInfo *Datasource, traceID string, start int64, end int64) (*http.Request, error) {
type TraceRequestApiVersion int
const (
TraceRequestApiVersionV1 TraceRequestApiVersion = iota
TraceRequestApiVersionV2
)
func (s *Service) createRequest(ctx context.Context, dsInfo *Datasource, apiVersion TraceRequestApiVersion, traceID string, start int64, end int64) (*http.Request, error) {
ctxLogger := s.logger.FromContext(ctx)
var baseUrl string
var tempoQuery string
if start == 0 || end == 0 {
tempoQuery = fmt.Sprintf("%s/api/traces/%s", dsInfo.URL, traceID)
if apiVersion == TraceRequestApiVersionV1 {
baseUrl = fmt.Sprintf("%s/api/traces/%s", dsInfo.URL, traceID)
} else {
tempoQuery = fmt.Sprintf("%s/api/traces/%s?start=%d&end=%d", dsInfo.URL, traceID, start, end)
baseUrl = fmt.Sprintf("%s/api/v2/traces/%s", dsInfo.URL, traceID)
}
if start == 0 || end == 0 {
tempoQuery = baseUrl
} else {
tempoQuery = fmt.Sprintf("%s?start=%d&end=%d", baseUrl, start, end)
}
req, err := http.NewRequestWithContext(ctx, "GET", tempoQuery, nil)
+21 -4
View File
@@ -10,18 +10,35 @@ import (
)
func TestTempo(t *testing.T) {
t.Run("createRequest without time range - success", func(t *testing.T) {
t.Run("createRequest v1 without time range - success", func(t *testing.T) {
service := &Service{logger: backend.NewLoggerWith("logger", "tempo-test")}
req, err := service.createRequest(context.Background(), &Datasource{}, "traceID", 0, 0)
req, err := service.createRequest(context.Background(), &Datasource{}, TraceRequestApiVersionV1, "traceID", 0, 0)
require.NoError(t, err)
assert.Equal(t, 1, len(req.Header))
assert.Equal(t, "/api/traces/traceID", req.URL.String())
})
t.Run("createRequest with time range - success", func(t *testing.T) {
t.Run("createRequest v1 with time range - success", func(t *testing.T) {
service := &Service{logger: backend.NewLoggerWith("logger", "tempo-test")}
req, err := service.createRequest(context.Background(), &Datasource{}, "traceID", 1, 2)
req, err := service.createRequest(context.Background(), &Datasource{}, TraceRequestApiVersionV1, "traceID", 1, 2)
require.NoError(t, err)
assert.Equal(t, 1, len(req.Header))
assert.Equal(t, "/api/traces/traceID?start=1&end=2", req.URL.String())
})
t.Run("createRequest v2 without time range - success", func(t *testing.T) {
service := &Service{logger: backend.NewLoggerWith("logger", "tempo-test")}
req, err := service.createRequest(context.Background(), &Datasource{}, TraceRequestApiVersionV2, "traceID", 0, 0)
require.NoError(t, err)
assert.Equal(t, 1, len(req.Header))
assert.Equal(t, "/api/v2/traces/traceID", req.URL.String())
})
t.Run("createRequest v2 with time range - success", func(t *testing.T) {
service := &Service{logger: backend.NewLoggerWith("logger", "tempo-test")}
req, err := service.createRequest(context.Background(), &Datasource{}, TraceRequestApiVersionV2, "traceID", 1, 2)
require.NoError(t, err)
assert.Equal(t, 1, len(req.Header))
assert.Equal(t, "/api/v2/traces/traceID?start=1&end=2", req.URL.String())
})
}
+88 -97
View File
@@ -7,8 +7,9 @@ import (
"strings"
"github.com/grafana/grafana-plugin-sdk-go/data"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
commonv11 "github.com/grafana/tempo/pkg/tempopb/common/v1"
v1 "github.com/grafana/tempo/pkg/tempopb/resource/v1"
tracev11 "github.com/grafana/tempo/pkg/tempopb/trace/v1"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
)
@@ -31,13 +32,11 @@ type TraceReference struct {
Tags []*KeyValue `json:"tags"`
}
func TraceToFrame(td ptrace.Traces) (*data.Frame, error) {
func TraceToFrame(resourceSpans []*tracev11.ResourceSpans) (*data.Frame, error) {
// In open telemetry format the spans are grouped first by resource/service they originated in and inside that
// resource they are grouped by the instrumentation library which created them.
resourceSpans := td.ResourceSpans()
if resourceSpans.Len() == 0 {
if len(resourceSpans) == 0 {
return nil, nil
}
@@ -68,8 +67,8 @@ func TraceToFrame(td ptrace.Traces) (*data.Frame, error) {
},
}
for i := 0; i < resourceSpans.Len(); i++ {
rs := resourceSpans.At(i)
for i := 0; i < len(resourceSpans); i++ {
rs := resourceSpans[i]
rows, err := resourceSpansToRows(rs)
if err != nil {
return nil, err
@@ -84,27 +83,27 @@ func TraceToFrame(td ptrace.Traces) (*data.Frame, error) {
}
// resourceSpansToRows processes all the spans for a particular resource/service
func resourceSpansToRows(rs ptrace.ResourceSpans) ([][]any, error) {
resource := rs.Resource()
ilss := rs.ScopeSpans()
func resourceSpansToRows(rs *tracev11.ResourceSpans) ([][]any, error) {
resource := rs.Resource
ilss := rs.ScopeSpans
if resource.Attributes().Len() == 0 || ilss.Len() == 0 {
if len(resource.Attributes) == 0 || len(ilss) == 0 {
return [][]any{}, nil
}
// Approximate the number of the spans as the number of the spans in the first
// instrumentation library info.
rows := make([][]any, 0, ilss.At(0).Spans().Len())
rows := make([][]any, 0, len(ilss[0].Spans))
for i := 0; i < ilss.Len(); i++ {
ils := ilss.At(i)
for i := 0; i < len(ilss); i++ {
ils := ilss[i]
// These are finally the actual spans
spans := ils.Spans()
spans := ils.Spans
for j := 0; j < spans.Len(); j++ {
span := spans.At(j)
row, err := spanToSpanRow(span, ils.Scope(), resource)
for j := 0; j < len(spans); j++ {
span := spans[j]
row, err := spanToSpanRow(span, ils.Scope, resource)
if err != nil {
return nil, err
}
@@ -117,28 +116,28 @@ func resourceSpansToRows(rs ptrace.ResourceSpans) ([][]any, error) {
return rows, nil
}
func spanToSpanRow(span ptrace.Span, libraryTags pcommon.InstrumentationScope, resource pcommon.Resource) ([]any, error) {
func spanToSpanRow(span *tracev11.Span, libraryTags *commonv11.InstrumentationScope, resource *v1.Resource) ([]any, error) {
// If the id representation changed from hexstring to something else we need to change the transformBase64IDToHexString in the frontend code
traceID := span.TraceID()
traceID := span.TraceId
traceIDHex := hex.EncodeToString(traceID[:])
traceIDHex = strings.TrimPrefix(traceIDHex, strings.Repeat("0", 16))
spanID := span.SpanID()
spanID := span.SpanId
spanIDHex := hex.EncodeToString(spanID[:])
parentSpanID := span.ParentSpanID()
parentSpanID := span.ParentSpanId
parentSpanIDHex := hex.EncodeToString(parentSpanID[:])
startTime := float64(span.StartTimestamp()) / 1_000_000
startTime := float64(span.StartTimeUnixNano) / 1_000_000
serviceName, serviceTags := resourceToProcess(resource)
status := span.Status()
statusCode := int64(status.Code())
statusMessage := status.Message()
status := span.Status
statusCode := int64(status.Code)
statusMessage := status.Message
libraryName := libraryTags.Name()
libraryVersion := libraryTags.Version()
traceState := getTraceState(span.TraceState())
libraryName := libraryTags.Name
libraryVersion := libraryTags.Version
traceState := span.TraceState
serviceTagsJson, err := json.Marshal(serviceTags)
if err != nil {
@@ -150,12 +149,12 @@ func spanToSpanRow(span ptrace.Span, libraryTags pcommon.InstrumentationScope, r
return nil, fmt.Errorf("failed to marshal span tags: %w", err)
}
logs, err := json.Marshal(spanEventsToLogs(span.Events()))
logs, err := json.Marshal(spanEventsToLogs(span.Events))
if err != nil {
return nil, fmt.Errorf("failed to marshal span logs: %w", err)
}
references, err := json.Marshal(spanLinksToReferences(span.Links()))
references, err := json.Marshal(spanLinksToReferences(span.Links))
if err != nil {
return nil, fmt.Errorf("failed to marshal span links: %w", err)
@@ -166,9 +165,9 @@ func spanToSpanRow(span ptrace.Span, libraryTags pcommon.InstrumentationScope, r
traceIDHex,
spanIDHex,
parentSpanIDHex,
span.Name(),
span.Name,
serviceName,
getSpanKind(span.Kind()),
getSpanKind(span.Kind),
statusCode,
statusMessage,
libraryName,
@@ -176,70 +175,68 @@ func spanToSpanRow(span ptrace.Span, libraryTags pcommon.InstrumentationScope, r
traceState,
json.RawMessage(serviceTagsJson),
startTime,
float64(span.EndTimestamp()-span.StartTimestamp()) / 1_000_000,
float64(span.EndTimeUnixNano-span.StartTimeUnixNano) / 1_000_000,
json.RawMessage(logs),
json.RawMessage(references),
json.RawMessage(spanTags),
}, nil
}
func resourceToProcess(resource pcommon.Resource) (string, []*KeyValue) {
attrs := resource.Attributes()
func resourceToProcess(resource *v1.Resource) (string, []*KeyValue) {
attrs := resource.Attributes
serviceName := ResourceNoServiceName
if attrs.Len() == 0 {
if len(attrs) == 0 {
return serviceName, nil
}
tags := make([]*KeyValue, 0, attrs.Len()-1)
attrs.Range(func(key string, attr pcommon.Value) bool {
if attribute.Key(key) == semconv.ServiceNameKey {
serviceName = attr.Str()
tags := make([]*KeyValue, 0, len(attrs)-1)
for _, attr := range attrs {
if attribute.Key(attr.Key) == semconv.ServiceNameKey {
serviceName = attr.GetValue().GetStringValue()
}
tags = append(tags, &KeyValue{Key: key, Value: getAttributeVal(attr)})
return true
})
tags = append(tags, &KeyValue{Key: attr.Key, Value: getAttributeVal(attr.Value)})
}
return serviceName, tags
}
func getAttributeVal(attr pcommon.Value) any {
switch attr.Type() {
case pcommon.ValueTypeStr:
return attr.Str()
case pcommon.ValueTypeInt:
return attr.Int()
case pcommon.ValueTypeBool:
return attr.Bool()
case pcommon.ValueTypeDouble:
return attr.Double()
case pcommon.ValueTypeMap, pcommon.ValueTypeSlice:
return attr.AsString()
func getAttributeVal(attr *commonv11.AnyValue) any {
switch attr.GetValue().(type) {
case *commonv11.AnyValue_StringValue:
return attr.GetStringValue()
case *commonv11.AnyValue_IntValue:
return attr.GetIntValue()
case *commonv11.AnyValue_BoolValue:
return attr.GetBoolValue()
case *commonv11.AnyValue_DoubleValue:
return attr.GetDoubleValue()
case *commonv11.AnyValue_KvlistValue, *commonv11.AnyValue_ArrayValue:
return attr.GetStringValue()
default:
return nil
}
}
func getSpanTags(span ptrace.Span) []*KeyValue {
var tags []*KeyValue
span.Attributes().Range(func(key string, attr pcommon.Value) bool {
tags = append(tags, &KeyValue{Key: key, Value: getAttributeVal(attr)})
return true
})
func getSpanTags(span *tracev11.Span) []*KeyValue {
tags := make([]*KeyValue, len(span.Attributes))
for i, attr := range span.Attributes {
tags[i] = &KeyValue{Key: attr.Key, Value: getAttributeVal(attr.Value)}
}
return tags
}
func getSpanKind(spanKind ptrace.SpanKind) string {
func getSpanKind(spanKind tracev11.Span_SpanKind) string {
var tagStr string
switch spanKind {
case ptrace.SpanKindClient:
case tracev11.Span_SPAN_KIND_CLIENT:
tagStr = string(OpenTracingSpanKindClient)
case ptrace.SpanKindServer:
case tracev11.Span_SPAN_KIND_SERVER:
tagStr = string(OpenTracingSpanKindServer)
case ptrace.SpanKindProducer:
case tracev11.Span_SPAN_KIND_PRODUCER:
tagStr = string(OpenTracingSpanKindProducer)
case ptrace.SpanKindConsumer:
case tracev11.Span_SPAN_KIND_CONSUMER:
tagStr = string(OpenTracingSpanKindConsumer)
case ptrace.SpanKindInternal:
case tracev11.Span_SPAN_KIND_INTERNAL:
tagStr = string(OpenTracingSpanKindInternal)
default:
return ""
@@ -248,54 +245,48 @@ func getSpanKind(spanKind ptrace.SpanKind) string {
return tagStr
}
func getTraceState(traceState pcommon.TraceState) string {
return traceState.AsRaw()
}
func spanEventsToLogs(events ptrace.SpanEventSlice) []*TraceLog {
if events.Len() == 0 {
func spanEventsToLogs(events []*tracev11.Span_Event) []*TraceLog {
if len(events) == 0 {
return nil
}
logs := make([]*TraceLog, 0, events.Len())
for i := 0; i < events.Len(); i++ {
event := events.At(i)
fields := make([]*KeyValue, 0, event.Attributes().Len()+1)
event.Attributes().Range(func(key string, attr pcommon.Value) bool {
fields = append(fields, &KeyValue{Key: key, Value: getAttributeVal(attr)})
return true
})
logs := make([]*TraceLog, 0, len(events))
for i := 0; i < len(events); i++ {
event := events[i]
fields := make([]*KeyValue, 0, len(event.Attributes)+1)
for _, attr := range event.Attributes {
fields = append(fields, &KeyValue{Key: attr.Key, Value: getAttributeVal(attr.Value)})
}
logs = append(logs, &TraceLog{
Timestamp: float64(event.Timestamp()) / 1_000_000,
Timestamp: float64(event.TimeUnixNano) / 1_000_000,
Fields: fields,
Name: event.Name(),
Name: event.Name,
})
}
return logs
}
func spanLinksToReferences(links ptrace.SpanLinkSlice) []*TraceReference {
if links.Len() == 0 {
func spanLinksToReferences(links []*tracev11.Span_Link) []*TraceReference {
if len(links) == 0 {
return nil
}
references := make([]*TraceReference, 0, links.Len())
for i := 0; i < links.Len(); i++ {
link := links.At(i)
references := make([]*TraceReference, 0, len(links))
for i := 0; i < len(links); i++ {
link := links[i]
traceID := link.TraceID()
traceID := link.TraceId
traceIDHex := hex.EncodeToString(traceID[:])
traceIDHex = strings.TrimLeft(traceIDHex, "0")
spanID := link.SpanID()
spanID := link.SpanId
spanIDHex := hex.EncodeToString(spanID[:])
tags := make([]*KeyValue, 0, link.Attributes().Len())
link.Attributes().Range(func(key string, attr pcommon.Value) bool {
tags = append(tags, &KeyValue{Key: key, Value: getAttributeVal(attr)})
return true
})
tags := make([]*KeyValue, 0, len(link.Attributes))
for _, attr := range link.Attributes {
tags = append(tags, &KeyValue{Key: attr.Key, Value: getAttributeVal(attr.Value)})
}
references = append(references, &TraceReference{
TraceID: traceIDHex,
+32 -39
View File
@@ -1,39 +1,35 @@
package tempo
import (
"encoding/hex"
"encoding/json"
"os"
"testing"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
//nolint:all
"github.com/golang/protobuf/jsonpb"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/tempo/pkg/tempopb"
v1 "github.com/grafana/tempo/pkg/tempopb/trace/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTraceToFrame(t *testing.T) {
t.Run("should transform tempo protobuf response into dataframe", func(t *testing.T) {
// For what ever reason you cannot easily create ptrace.Traces for the TraceToFrame from something more readable
// like json. You could tediously create the structures manually using all the setters for everything or use
// https://github.com/grafana/tempo/tree/master/pkg/tempopb to create the protobuf structs from something like
// json. At the moment just saving some real tempo proto response into file and loading was the easiest and
// as my patience was diminished trying to figure this out, I say it's good enough. You can also just modify
// the trace afterward as you wish.
proto, err := os.ReadFile("testData/tempo_proto_response")
jsonResponse, err := os.Open("testData/trace.json")
require.NoError(t, err)
pbUnmarshaler := ptrace.ProtoUnmarshaler{}
otTrace, err := pbUnmarshaler.UnmarshalTraces(proto)
var otTrace tempopb.Trace
err = jsonpb.Unmarshal(jsonResponse, &otTrace)
require.NoError(t, err)
// For some reason the trace does not have named events (probably was generated some time ago) so we just set
// one here for testing
origSpan := findSpan(otTrace, "7198307df9748606")
origSpan.Events().At(0).SetName("test event")
origSpan.GetEvents()[0].Name = "test event"
frame, err := TraceToFrame(otTrace)
frame, err := TraceToFrame(otTrace.GetResourceSpans())
require.NoError(t, err)
require.Equal(t, 30, frame.Rows())
@@ -49,7 +45,7 @@ func TestTraceToFrame(t *testing.T) {
require.Equal(t, 1616072924070.497, root["startTime"])
require.Equal(t, 8.421, root["duration"])
require.Equal(t, json.RawMessage("null"), root["logs"])
require.Equal(t, json.RawMessage("[{\"value\":\"const\",\"key\":\"sampler.type\"},{\"value\":true,\"key\":\"sampler.param\"},{\"value\":200,\"key\":\"http.status_code\"},{\"value\":\"GET\",\"key\":\"http.method\"},{\"value\":\"/loki/api/v1/query_range?direction=BACKWARD\\u0026limit=1000\\u0026query=%7Bcompose_project%3D%22devenv%22%7D%20%7C%3D%22traceID%22\\u0026start=1616070921000000000\\u0026end=1616072722000000000\\u0026step=2\",\"key\":\"http.url\"},{\"value\":\"net/http\",\"key\":\"component\"}]"), root["tags"])
require.Equal(t, json.RawMessage("[{\"value\":\"const\",\"key\":\"sampler.type\"},{\"value\":true,\"key\":\"sampler.param\"},{\"value\":200,\"key\":\"http.status_code\"},{\"value\":\"GET\",\"key\":\"http.method\"},{\"value\":\"/loki/api/v1/query_range?direction=BACKWARD\\u0026limit=1000\\u0026query=%7Bcompose_project%3D%22devenv%22%7D%20%7C%3D%22trace_id%22\\u0026start=1616070921000000000\\u0026end=1616072722000000000\\u0026step=2\",\"key\":\"http.url\"},{\"value\":\"net/http\",\"key\":\"component\"}]"), root["tags"])
span := bFrame.FindRowWithValue("spanID", "7198307df9748606")
@@ -85,31 +81,29 @@ func TestTraceToFrame(t *testing.T) {
})
t.Run("should transform correct traceID", func(t *testing.T) {
proto, err := os.ReadFile("testData/tempo_proto_response")
jsonResponse, err := os.Open("testData/trace.json")
require.NoError(t, err)
pbUnmarshaler := ptrace.ProtoUnmarshaler{}
otTrace, err := pbUnmarshaler.UnmarshalTraces(proto)
var otTrace tempopb.Trace
err = jsonpb.Unmarshal(jsonResponse, &otTrace)
require.NoError(t, err)
var index int
otTrace.ResourceSpans().RemoveIf(func(rsp ptrace.ResourceSpans) bool {
rsp.ScopeSpans().RemoveIf(func(sp ptrace.ScopeSpans) bool {
sp.Spans().RemoveIf(func(span ptrace.Span) bool {
for _, resourceSpans := range otTrace.GetResourceSpans() {
for _, scopeSpans := range resourceSpans.GetScopeSpans() {
for _, span := range scopeSpans.GetSpans() {
if index == 0 {
span.SetTraceID(pcommon.TraceID([16]byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7}))
span.TraceId = []byte{0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7}
}
if index == 1 {
span.SetTraceID(pcommon.TraceID([16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7}))
span.TraceId = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7}
}
index++
return false
})
return false
})
return false
})
frame, err := TraceToFrame(otTrace)
}
}
}
frame, err := TraceToFrame(otTrace.GetResourceSpans())
require.NoError(t, err)
bFrame := &BetterFrame{frame}
@@ -168,15 +162,14 @@ func fieldNames(frame *data.Frame) []string {
return names
}
func findSpan(trace ptrace.Traces, spanId string) *ptrace.Span {
for i := 0; i < trace.ResourceSpans().Len(); i++ {
scope := trace.ResourceSpans().At(i).ScopeSpans()
for j := 0; j < scope.Len(); j++ {
spans := scope.At(j).Spans()
for k := 0; k < spans.Len(); k++ {
if spans.At(k).SpanID().String() == spanId {
found := spans.At(k)
return &found
func findSpan(trace tempopb.Trace, spanId string) *v1.Span {
for i := 0; i < len(trace.GetResourceSpans()); i++ {
scope := trace.GetResourceSpans()[i].GetScopeSpans()
for j := 0; j < len(scope); j++ {
spans := scope[j].GetSpans()
for k := 0; k < len(spans); k++ {
if hex.EncodeToString(spans[k].GetSpanId()[:]) == spanId {
return spans[k]
}
}
}