Skip to content

Commit

Permalink
Minor clean-up after PR 4596 (#4605)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?
- Prevent potential future linter errors where errors are ignored

## Short description of the changes
- Handle error from Close

Signed-off-by: Yuri Shkuro <[email protected]>
  • Loading branch information
yurishkuro authored Jul 30, 2023
1 parent de3cf99 commit e0c83b0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 46 deletions.
80 changes: 40 additions & 40 deletions plugin/storage/es/spanstore/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ type spanReaderTest struct {
reader *SpanReader
}

func tracerProvider() (trace.TracerProvider, *tracetest.InMemoryExporter, func() error) {
func tracerProvider(t *testing.T) (trace.TracerProvider, *tracetest.InMemoryExporter, func()) {
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
)
closer := func() error {
return tp.Shutdown(context.Background())
closer := func() {
assert.NoError(t, tp.Shutdown(context.Background()))
}
return tp, exporter, closer
}

func withSpanReader(fn func(r *spanReaderTest)) {
func withSpanReader(t *testing.T, fn func(r *spanReaderTest)) {
client := &mocks.Client{}
tracer, exp, closer := tracerProvider()
tracer, exp, closer := tracerProvider(t)
defer closer()
logger, logBuffer := testutils.NewLogger()
r := &spanReaderTest{
Expand All @@ -128,9 +128,9 @@ func withSpanReader(fn func(r *spanReaderTest)) {
fn(r)
}

func withArchiveSpanReader(readAlias bool, fn func(r *spanReaderTest)) {
func withArchiveSpanReader(t *testing.T, readAlias bool, fn func(r *spanReaderTest)) {
client := &mocks.Client{}
tracer, exp, closer := tracerProvider()
tracer, exp, closer := tracerProvider(t)
defer closer()
logger, logBuffer := testutils.NewLogger()
r := &spanReaderTest{
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestSpanReaderIndices(t *testing.T) {
serviceDataLayoutFormat := date.UTC().Format(serviceDataLayout)
metricsFactory := metricstest.NewFactory(0)
logger, _ := testutils.NewLogger()
tracer, _, closer := tracerProvider()
tracer, _, closer := tracerProvider(t)
defer closer()

testCases := []struct {
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestSpanReaderIndices(t *testing.T) {
}

func TestSpanReader_GetTrace(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
hits := make([]*elastic.SearchHit, 1)
hits[0] = &elastic.SearchHit{
Source: (*json.RawMessage)(&exampleESSpan),
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestSpanReader_GetTrace(t *testing.T) {
}

func TestSpanReader_multiRead_followUp_query(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
date := time.Date(2019, 10, 10, 5, 0, 0, 0, time.UTC)
spanID1 := dbmodel.Span{SpanID: "0", TraceID: "1", StartTime: model.TimeAsEpochMicroseconds(date)}
spanBytesID1, err := json.Marshal(spanID1)
Expand Down Expand Up @@ -422,7 +422,7 @@ func TestSpanReader_multiRead_followUp_query(t *testing.T) {
}

func TestSpanReader_SearchAfter(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
var hits []*elastic.SearchHit

for i := 0; i < 10000; i++ {
Expand Down Expand Up @@ -453,7 +453,7 @@ func TestSpanReader_SearchAfter(t *testing.T) {
}

func TestSpanReader_GetTraceQueryError(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(nil, errors.New("query error occurred"))
mockMultiSearchService(r).
Expand All @@ -468,7 +468,7 @@ func TestSpanReader_GetTraceQueryError(t *testing.T) {
}

func TestSpanReader_GetTraceNilHits(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
var hits []*elastic.SearchHit
searchHits := &elastic.SearchHits{Hits: hits}

Expand All @@ -488,7 +488,7 @@ func TestSpanReader_GetTraceNilHits(t *testing.T) {
}

func TestSpanReader_GetTraceInvalidSpanError(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
data := []byte(`{"TraceID": "123"asdf fadsg}`)
hits := make([]*elastic.SearchHit, 1)
hits[0] = &elastic.SearchHit{
Expand All @@ -512,7 +512,7 @@ func TestSpanReader_GetTraceInvalidSpanError(t *testing.T) {
}

func TestSpanReader_GetTraceSpanConversionError(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
badSpan := []byte(`{"TraceID": "123"}`)

hits := make([]*elastic.SearchHit, 1)
Expand All @@ -537,7 +537,7 @@ func TestSpanReader_GetTraceSpanConversionError(t *testing.T) {
}

func TestSpanReader_esJSONtoJSONSpanModel(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
jsonPayload := (*json.RawMessage)(&exampleESSpan)

esSpanRaw := &elastic.SearchHit{
Expand All @@ -554,7 +554,7 @@ func TestSpanReader_esJSONtoJSONSpanModel(t *testing.T) {
}

func TestSpanReader_esJSONtoJSONSpanModelError(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
data := []byte(`{"TraceID": "123"asdf fadsg}`)
jsonPayload := (*json.RawMessage)(&data)

Expand Down Expand Up @@ -604,7 +604,7 @@ func TestSpanReaderFindIndices(t *testing.T) {
},
},
}
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
for _, testCase := range testCases {
actual := r.reader.timeRangeIndices(spanIndex, dateLayout, testCase.startTime, testCase.endTime, -24*time.Hour)
assert.EqualValues(t, testCase.expected, actual)
Expand All @@ -613,7 +613,7 @@ func TestSpanReaderFindIndices(t *testing.T) {
}

func TestSpanReader_indexWithDate(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
actual := indexWithDate(spanIndex, "2006-01-02", time.Date(1995, time.April, 21, 4, 21, 19, 95, time.UTC))
assert.Equal(t, "jaeger-span-1995-04-21", actual)
})
Expand Down Expand Up @@ -668,7 +668,7 @@ func testGet(typ string, t *testing.T) {
for _, tc := range testCases {
testCase := tc
t.Run(testCase.caption, func(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).Return(testCase.searchResult, testCase.searchError)
actual, err := returnSearchFunc(typ, r)
if testCase.expectedError() != "" {
Expand Down Expand Up @@ -700,7 +700,7 @@ func returnSearchFunc(typ string, r *spanReaderTest) (interface{}, error) {
}

func TestSpanReader_bucketToStringArray(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
buckets := make([]*elastic.AggregationBucketKeyItem, 3)
buckets[0] = &elastic.AggregationBucketKeyItem{Key: "hello"}
buckets[1] = &elastic.AggregationBucketKeyItem{Key: "world"}
Expand All @@ -714,7 +714,7 @@ func TestSpanReader_bucketToStringArray(t *testing.T) {
}

func TestSpanReader_bucketToStringArrayError(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
buckets := make([]*elastic.AggregationBucketKeyItem, 3)
buckets[0] = &elastic.AggregationBucketKeyItem{Key: "hello"}
buckets[1] = &elastic.AggregationBucketKeyItem{Key: "world"}
Expand All @@ -736,7 +736,7 @@ func TestSpanReader_FindTraces(t *testing.T) {
}
searchHits := &elastic.SearchHits{Hits: hits}

withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
// find trace IDs
mockSearchService(r).
Return(&elastic.SearchResult{Aggregations: elastic.Aggregations(goodAggregations), Hits: searchHits}, nil)
Expand Down Expand Up @@ -784,7 +784,7 @@ func TestSpanReader_FindTracesInvalidQuery(t *testing.T) {
}
searchHits := &elastic.SearchHits{Hits: hits}

withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{Aggregations: elastic.Aggregations(goodAggregations), Hits: searchHits}, nil)
mockMultiSearchService(r).
Expand Down Expand Up @@ -820,7 +820,7 @@ func TestSpanReader_FindTracesAggregationFailure(t *testing.T) {
}
searchHits := &elastic.SearchHits{Hits: hits}

withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{Aggregations: elastic.Aggregations(goodAggregations), Hits: searchHits}, nil)
mockMultiSearchService(r).
Expand Down Expand Up @@ -855,7 +855,7 @@ func TestSpanReader_FindTracesNoTraceIDs(t *testing.T) {
}
searchHits := &elastic.SearchHits{Hits: hits}

withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{Aggregations: elastic.Aggregations(goodAggregations), Hits: searchHits}, nil)
mockMultiSearchService(r).
Expand Down Expand Up @@ -891,7 +891,7 @@ func TestSpanReader_FindTracesReadTraceFailure(t *testing.T) {
}
searchHits := &elastic.SearchHits{Hits: hits}

withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{Aggregations: elastic.Aggregations(goodAggregations), Hits: searchHits}, nil)
mockMultiSearchService(r).
Expand Down Expand Up @@ -925,7 +925,7 @@ func TestSpanReader_FindTracesSpanCollectionFailure(t *testing.T) {
}
searchHits := &elastic.SearchHits{Hits: hits}

withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{Aggregations: elastic.Aggregations(goodAggregations), Hits: searchHits}, nil)
mockMultiSearchService(r).
Expand Down Expand Up @@ -1065,7 +1065,7 @@ func TestSpanReader_buildTraceIDAggregation(t *testing.T) {
"aggregations": {
"startTime" : { "max": {"field": "startTime"}}
}}`
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
traceIDAggregation := r.reader.buildTraceIDAggregation(123)
actual, err := traceIDAggregation.Source()
require.NoError(t, err)
Expand All @@ -1079,7 +1079,7 @@ func TestSpanReader_buildTraceIDAggregation(t *testing.T) {
}

func TestSpanReader_buildFindTraceIDsQuery(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
traceQuery := &spanstore.TraceQueryParameters{
DurationMin: time.Second,
DurationMax: time.Second * 2,
Expand Down Expand Up @@ -1117,7 +1117,7 @@ func TestSpanReader_buildDurationQuery(t *testing.T) {
"to": 2000000 }
}
}`
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
durationMin := time.Second
durationMax := time.Second * 2
durationQuery := r.reader.buildDurationQuery(durationMin, durationMax)
Expand All @@ -1142,7 +1142,7 @@ func TestSpanReader_buildStartTimeQuery(t *testing.T) {
"to": 2000000 }
}
}`
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
startTimeMin := time.Time{}.Add(time.Second)
startTimeMax := time.Time{}.Add(2 * time.Second)
durationQuery := r.reader.buildStartTimeQuery(startTimeMin, startTimeMax)
Expand All @@ -1161,7 +1161,7 @@ func TestSpanReader_buildStartTimeQuery(t *testing.T) {

func TestSpanReader_buildServiceNameQuery(t *testing.T) {
expectedStr := `{ "match": { "process.serviceName": { "query": "bat" }}}`
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
serviceNameQuery := r.reader.buildServiceNameQuery("bat")
actual, err := serviceNameQuery.Source()
require.NoError(t, err)
Expand All @@ -1175,7 +1175,7 @@ func TestSpanReader_buildServiceNameQuery(t *testing.T) {

func TestSpanReader_buildOperationNameQuery(t *testing.T) {
expectedStr := `{ "match": { "operationName": { "query": "spook" }}}`
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
operationNameQuery := r.reader.buildOperationNameQuery("spook")
actual, err := operationNameQuery.Source()
require.NoError(t, err)
Expand All @@ -1190,7 +1190,7 @@ func TestSpanReader_buildOperationNameQuery(t *testing.T) {
func TestSpanReader_buildTagQuery(t *testing.T) {
inStr, err := os.ReadFile("fixtures/query_01.json")
require.NoError(t, err)
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
tagQuery := r.reader.buildTagQuery("bat.foo", "spook")
actual, err := tagQuery.Source()
require.NoError(t, err)
Expand All @@ -1205,7 +1205,7 @@ func TestSpanReader_buildTagQuery(t *testing.T) {
func TestSpanReader_buildTagRegexQuery(t *testing.T) {
inStr, err := os.ReadFile("fixtures/query_02.json")
require.NoError(t, err)
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
tagQuery := r.reader.buildTagQuery("bat.foo", "spo.*")
actual, err := tagQuery.Source()
require.NoError(t, err)
Expand All @@ -1220,7 +1220,7 @@ func TestSpanReader_buildTagRegexQuery(t *testing.T) {
func TestSpanReader_buildTagRegexEscapedQuery(t *testing.T) {
inStr, err := os.ReadFile("fixtures/query_03.json")
require.NoError(t, err)
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
tagQuery := r.reader.buildTagQuery("bat.foo", "spo\\*")
actual, err := tagQuery.Source()
require.NoError(t, err)
Expand All @@ -1233,7 +1233,7 @@ func TestSpanReader_buildTagRegexEscapedQuery(t *testing.T) {
}

func TestSpanReader_GetEmptyIndex(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{}, nil)
mockMultiSearchService(r).
Expand All @@ -1259,7 +1259,7 @@ func TestSpanReader_GetEmptyIndex(t *testing.T) {
}

func TestSpanReader_ArchiveTraces(t *testing.T) {
withArchiveSpanReader(false, func(r *spanReaderTest) {
withArchiveSpanReader(t, false, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{}, nil)
mockArchiveMultiSearchService(r, "jaeger-span-archive").
Expand All @@ -1275,7 +1275,7 @@ func TestSpanReader_ArchiveTraces(t *testing.T) {
}

func TestSpanReader_ArchiveTraces_ReadAlias(t *testing.T) {
withArchiveSpanReader(true, func(r *spanReaderTest) {
withArchiveSpanReader(t, true, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{}, nil)
mockArchiveMultiSearchService(r, "jaeger-span-archive-read").
Expand Down
4 changes: 2 additions & 2 deletions plugin/storage/es/spanstore/service_operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestSpanReader_GetOperations(t *testing.T) {
}

func TestSpanReader_GetServicesEmptyIndex(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{}, nil)
mockMultiSearchService(r).
Expand All @@ -115,7 +115,7 @@ func TestSpanReader_GetServicesEmptyIndex(t *testing.T) {
}

func TestSpanReader_GetOperationsEmptyIndex(t *testing.T) {
withSpanReader(func(r *spanReaderTest) {
withSpanReader(t, func(r *spanReaderTest) {
mockSearchService(r).
Return(&elastic.SearchResult{}, nil)
mockMultiSearchService(r).
Expand Down
10 changes: 6 additions & 4 deletions plugin/storage/integration/elasticsearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ type ESStorageIntegration struct {
logger *zap.Logger
}

func tracerProvider() (trace.TracerProvider, *tracetest.InMemoryExporter, func() error) {
func (s *ESStorageIntegration) tracerProvider() (trace.TracerProvider, *tracetest.InMemoryExporter, func()) {
exporter := tracetest.NewInMemoryExporter()
tp := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithSyncer(exporter),
)
closer := func() error {
return tp.Shutdown(context.Background())
closer := func() {
if err := tp.Shutdown(context.Background()); err != nil {
s.logger.Error("failed to close tracer", zap.Error(err))
}
}
return tp, exporter, closer
}
Expand Down Expand Up @@ -156,7 +158,7 @@ func (s *ESStorageIntegration) initSpanstore(allTagsAsFields, archive bool) erro
if err != nil {
return err
}
tracer, _, closer := tracerProvider()
tracer, _, closer := s.tracerProvider()
defer closer()
s.SpanWriter = w
s.SpanReader = spanstore.NewSpanReader(spanstore.SpanReaderParams{
Expand Down

0 comments on commit e0c83b0

Please sign in to comment.