-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose a Traces api V2 (#3912)
* v2 first implementation * remove tempo config file * fix linting * added new trace_by_id combiner * fix linting * changelog * fix flaky test * pass the combiner to the handler to reuse code * refactor code by using helper function * do not overwrite the marshalling format of the struct * rename marshaling property * init combiner * added documentatioN
- Loading branch information
1 parent
6e9d93a
commit 195936e
Showing
15 changed files
with
482 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package combiner | ||
|
||
import ( | ||
"github.com/grafana/tempo/pkg/model/trace" | ||
"github.com/grafana/tempo/pkg/tempopb" | ||
) | ||
|
||
func NewTraceByIDV2(maxBytes int, marshalingFormat string) Combiner { | ||
combiner := trace.NewCombiner(maxBytes) | ||
gc := &genericCombiner[*tempopb.TraceByIDResponse]{ | ||
combine: func(partial *tempopb.TraceByIDResponse, _ *tempopb.TraceByIDResponse, _ PipelineResponse) error { | ||
_, err := combiner.Consume(partial.Trace) | ||
return err | ||
}, | ||
finalize: func(resp *tempopb.TraceByIDResponse) (*tempopb.TraceByIDResponse, error) { | ||
traceResult, _ := combiner.Result() | ||
if traceResult == nil { | ||
traceResult = &tempopb.Trace{} | ||
} | ||
|
||
// dedupe duplicate span ids | ||
deduper := newDeduper() | ||
traceResult = deduper.dedupe(traceResult) | ||
|
||
resp.Trace = traceResult | ||
return resp, nil | ||
}, | ||
new: func() *tempopb.TraceByIDResponse { return &tempopb.TraceByIDResponse{} }, | ||
current: &tempopb.TraceByIDResponse{}, | ||
} | ||
initHTTPCombiner(gc, marshalingFormat) | ||
return gc | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package combiner | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/gogo/protobuf/jsonpb" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/grafana/tempo/pkg/api" | ||
"github.com/grafana/tempo/pkg/tempopb" | ||
"github.com/grafana/tempo/pkg/util/test" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type MockResponse struct { | ||
resp *http.Response | ||
} | ||
|
||
func (m MockResponse) HTTPResponse() *http.Response { | ||
return m.resp | ||
} | ||
|
||
func (m MockResponse) RequestData() any { | ||
return nil | ||
} | ||
|
||
func TestNewTraceByIDV2(t *testing.T) { | ||
traceResponse := &tempopb.TraceByIDResponse{ | ||
Trace: test.MakeTrace(2, []byte{0x01, 0x02}), | ||
Metrics: &tempopb.TraceByIDMetrics{}, | ||
} | ||
resBytes, err := proto.Marshal(traceResponse) | ||
require.NoError(t, err) | ||
response := http.Response{ | ||
StatusCode: 200, | ||
Header: map[string][]string{ | ||
"Content-Type": {"application/protobuf"}, | ||
}, | ||
Body: io.NopCloser(bytes.NewReader(resBytes)), | ||
} | ||
|
||
t.Run("returns a combined trace response as JSON", func(t *testing.T) { | ||
combiner := NewTraceByIDV2(100_000, api.HeaderAcceptJSON) | ||
err = combiner.AddResponse(MockResponse{&response}) | ||
require.NoError(t, err) | ||
|
||
res, err := combiner.HTTPFinal() | ||
require.NoError(t, err) | ||
assert.Equal(t, "application/json", res.Header.Get("Content-Type")) | ||
|
||
actualResp := &tempopb.TraceByIDResponse{} | ||
err = new(jsonpb.Unmarshaler).Unmarshal(res.Body, actualResp) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("returns a combined trace response as protobuff", func(t *testing.T) { | ||
combiner := NewTraceByIDV2(100_000, api.HeaderAcceptProtobuf) | ||
err = combiner.AddResponse(MockResponse{&response}) | ||
require.NoError(t, err) | ||
|
||
res, err := combiner.HTTPFinal() | ||
require.NoError(t, err) | ||
require.NotNil(t, res) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.