Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2][storage] Implement reverse adapter to translate v2 storage api to v1 #6485

Merged
merged 14 commits into from
Jan 5, 2025
5 changes: 3 additions & 2 deletions cmd/query/app/querysvc/query_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ type TraceQueryParameters struct {
func NewQueryService(traceReader tracestore.Reader, dependencyReader depstore.Reader, options QueryServiceOptions) *QueryService {
spanReader, err := v1adapter.GetV1Reader(traceReader)
if err != nil {
// TODO: implement a reverse adapter to convert v2 reader to v1 reader
panic(err)
// if the spanstore.Reader is not available, downgrade the native tracestore.Reader to
// a spanstore.Reader
spanReader = v1adapter.NewSpanReader(traceReader)
}
qsvc := &QueryService{
spanReader: spanReader,
Expand Down
34 changes: 29 additions & 5 deletions cmd/query/app/querysvc/query_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
spanstoremocks "github.com/jaegertracing/jaeger/storage/spanstore/mocks"
"github.com/jaegertracing/jaeger/storage_v2/depstore"
depsmocks "github.com/jaegertracing/jaeger/storage_v2/depstore/mocks"
"github.com/jaegertracing/jaeger/storage_v2/tracestore"
tracestoremocks "github.com/jaegertracing/jaeger/storage_v2/tracestore/mocks"
"github.com/jaegertracing/jaeger/storage_v2/v1adapter"
)
Expand Down Expand Up @@ -513,9 +514,32 @@ func TestMain(m *testing.M) {
testutils.VerifyGoLeaks(m)
}

func TestNewQueryService_PanicsForNonV1AdapterReader(t *testing.T) {
reader := &tracestoremocks.Reader{}
dependencyReader := &depsmocks.Reader{}
options := QueryServiceOptions{}
require.PanicsWithError(t, v1adapter.ErrV1ReaderNotAvailable.Error(), func() { NewQueryService(reader, dependencyReader, options) })
func TestNewQueryService_UsesCorrectTypeForSpanReader(t *testing.T) {
tests := []struct {
name string
reader tracestore.Reader
expectedType spanstore.Reader
}{
{
name: "wrapped spanstore.Reader gets extracted",
reader: func() tracestore.Reader {
reader := &spanstoremocks.Reader{}
return v1adapter.NewTraceReader(reader)
}(),
expectedType: &spanstoremocks.Reader{},
},
{
name: "tracestore.Reader gets downgraded to v1 spanstore.Reader",
reader: &tracestoremocks.Reader{},
expectedType: &v1adapter.SpanReader{},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dependencyReader := &depsmocks.Reader{}
options := QueryServiceOptions{}
qs := NewQueryService(test.reader, dependencyReader, options)
assert.IsType(t, test.expectedType, qs.spanReader)
})
}
}
101 changes: 101 additions & 0 deletions storage_v2/v1adapter/spanreader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) 2025 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package v1adapter

import (
"context"

"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/storage/spanstore"
"github.com/jaegertracing/jaeger/storage_v2/tracestore"
)

var _ spanstore.Reader = (*SpanReader)(nil)

// SpanReader wraps a tracestore.Reader so that it can be downgraded to implement
// the v1 spanstore.Reader interface.
type SpanReader struct {
traceReader tracestore.Reader
}

func NewSpanReader(traceReader tracestore.Reader) *SpanReader {
return &SpanReader{
traceReader: traceReader,
}
}

func (sr *SpanReader) GetTrace(ctx context.Context, query spanstore.GetTraceParameters) (*model.Trace, error) {
getTracesIter := sr.traceReader.GetTraces(ctx, tracestore.GetTraceParams{
TraceID: query.TraceID.ToOTELTraceID(),
Start: query.StartTime,
End: query.EndTime,
})
traces, err := V1TracesFromSeq2(getTracesIter)
if err != nil {
return nil, err
}
if len(traces) == 0 {
return nil, spanstore.ErrTraceNotFound
}
return traces[0], nil
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
}

func (sr *SpanReader) GetServices(ctx context.Context) ([]string, error) {
return sr.traceReader.GetServices(ctx)
}

func (sr *SpanReader) GetOperations(
ctx context.Context,
query spanstore.OperationQueryParameters,
) ([]spanstore.Operation, error) {
o, err := sr.traceReader.GetOperations(ctx, tracestore.OperationQueryParams{
ServiceName: query.ServiceName,
SpanKind: query.SpanKind,
})
if err != nil || o == nil {
return nil, err
}
operations := []spanstore.Operation{}
for _, operation := range o {
operations = append(operations, spanstore.Operation{
Name: operation.Name,
SpanKind: operation.SpanKind,
})
}
return operations, nil
}

func (sr *SpanReader) FindTraces(
ctx context.Context,
query *spanstore.TraceQueryParameters,
) ([]*model.Trace, error) {
getTracesIter := sr.traceReader.FindTraces(ctx, tracestore.TraceQueryParams{
ServiceName: query.ServiceName,
OperationName: query.OperationName,
Tags: query.Tags,
StartTimeMin: query.StartTimeMin,
StartTimeMax: query.StartTimeMax,
DurationMin: query.DurationMin,
DurationMax: query.DurationMax,
NumTraces: query.NumTraces,
})
return V1TracesFromSeq2(getTracesIter)
}

func (sr *SpanReader) FindTraceIDs(
ctx context.Context,
query *spanstore.TraceQueryParameters,
) ([]model.TraceID, error) {
traceIDsIter := sr.traceReader.FindTraceIDs(ctx, tracestore.TraceQueryParams{
ServiceName: query.ServiceName,
OperationName: query.OperationName,
Tags: query.Tags,
StartTimeMin: query.StartTimeMin,
StartTimeMax: query.StartTimeMax,
DurationMin: query.DurationMin,
DurationMax: query.DurationMax,
NumTraces: query.NumTraces,
})
return V1TraceIDsFromSeq2(traceIDsIter)
}
Loading
Loading