Skip to content

Commit 7be2d7a

Browse files
Manik2708yurishkuro
authored andcommitted
[ES] Separate the CoreSpanWriter from SpanWriter (jaegertracing#6883)
## Which problem is this PR solving? Fixes a part of: jaegertracing#6458 ## Description of the changes - We have to prepeare the writer to move to `v2` and for that we need to separate `SpanWriter` ## How was this change tested? - Unit Tests ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality - [x] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `npm run lint` and `npm run test` --------- Signed-off-by: Manik2708 <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]> Signed-off-by: amol-verma-allen <[email protected]>
1 parent eaf4f77 commit 7be2d7a

File tree

8 files changed

+240
-127
lines changed

8 files changed

+240
-127
lines changed

.mockery.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,5 @@ packages:
8282
github.com/jaegertracing/jaeger/internal/storage/v1/elasticsearch/spanstore:
8383
interfaces:
8484
CoreSpanReader:
85+
CoreSpanWriter:
8586

internal/storage/v1/elasticsearch/spanstore/internal/dbmodel/from_domain_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,57 @@ func TestConvertKeyValueValue(t *testing.T) {
146146
})
147147
}
148148
}
149+
150+
func TestNewSpanTags(t *testing.T) {
151+
testCases := []struct {
152+
spanConverter FromDomain
153+
expected Span
154+
name string
155+
}{
156+
{
157+
spanConverter: NewFromDomain(true, []string{}, ""),
158+
expected: Span{
159+
Tag: map[string]any{"foo": "bar"}, Tags: []KeyValue{},
160+
Process: Process{Tag: map[string]any{"bar": "baz"}, Tags: []KeyValue{}},
161+
},
162+
name: "allTagsAsFields",
163+
},
164+
{
165+
spanConverter: NewFromDomain(false, []string{"foo", "bar", "rere"}, ""),
166+
expected: Span{
167+
Tag: map[string]any{"foo": "bar"}, Tags: []KeyValue{},
168+
Process: Process{Tag: map[string]any{"bar": "baz"}, Tags: []KeyValue{}},
169+
},
170+
name: "definedTagNames",
171+
},
172+
{
173+
spanConverter: NewFromDomain(false, []string{}, ""),
174+
expected: Span{
175+
Tags: []KeyValue{{
176+
Key: "foo",
177+
Type: StringType,
178+
Value: "bar",
179+
}},
180+
Process: Process{Tags: []KeyValue{{
181+
Key: "bar",
182+
Type: StringType,
183+
Value: "baz",
184+
}}},
185+
},
186+
name: "noAllTagsAsFields",
187+
},
188+
}
189+
for _, test := range testCases {
190+
t.Run(test.name, func(t *testing.T) {
191+
s := &model.Span{
192+
Tags: []model.KeyValue{{Key: "foo", VStr: "bar"}},
193+
Process: &model.Process{Tags: []model.KeyValue{{Key: "bar", VStr: "baz"}}},
194+
}
195+
mSpan := test.spanConverter.FromDomainEmbedProcess(s)
196+
assert.Equal(t, test.expected.Tag, mSpan.Tag)
197+
assert.Equal(t, test.expected.Tags, mSpan.Tags)
198+
assert.Equal(t, test.expected.Process.Tag, mSpan.Process.Tag)
199+
assert.Equal(t, test.expected.Process.Tags, mSpan.Process.Tags)
200+
})
201+
}
202+
}

internal/storage/v1/elasticsearch/spanstore/mocks/CoreSpanWriter.go

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/storage/v1/elasticsearch/spanstore/service_operation_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func TestWriteService(t *testing.T) {
4141
},
4242
}
4343

44-
w.writer.spanWriter.writeService(indexName, jsonSpan)
44+
w.writer.writeService(indexName, jsonSpan)
4545

4646
indexService.AssertNumberOfCalls(t, "Add", 1)
4747
assert.Equal(t, "", w.logBuffer.String())
4848

4949
// test that cache works, will call the index service only once.
50-
w.writer.spanWriter.writeService(indexName, jsonSpan)
50+
w.writer.writeService(indexName, jsonSpan)
5151
indexService.AssertNumberOfCalls(t, "Add", 1)
5252
})
5353
}
@@ -76,7 +76,7 @@ func TestWriteServiceError(*testing.T) {
7676
},
7777
}
7878

79-
w.writer.spanWriter.writeService(indexName, jsonSpan)
79+
w.writer.writeService(indexName, jsonSpan)
8080
})
8181
}
8282

internal/storage/v1/elasticsearch/spanstore/writer.go

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import (
1111

1212
"go.uber.org/zap"
1313

14-
"github.com/jaegertracing/jaeger-idl/model/v1"
1514
"github.com/jaegertracing/jaeger/internal/cache"
16-
"github.com/jaegertracing/jaeger/internal/storage/v1/api/spanstore/spanstoremetrics"
1715
"github.com/jaegertracing/jaeger/internal/storage/v1/elasticsearch/spanstore/internal/dbmodel"
1816
"github.com/jaegertracing/jaeger/pkg/es"
1917
cfg "github.com/jaegertracing/jaeger/pkg/es/config"
@@ -27,25 +25,25 @@ const (
2725
indexCacheTTLDefault = 48 * time.Hour
2826
)
2927

30-
type spanWriterMetrics struct {
31-
indexCreate *spanstoremetrics.WriteMetrics
32-
}
33-
3428
type serviceWriter func(string, *dbmodel.Span)
3529

3630
// SpanWriter is a wrapper around elastic.Client
3731
type SpanWriter struct {
38-
client func() es.Client
39-
logger *zap.Logger
40-
writerMetrics spanWriterMetrics // TODO: build functions to wrap around each Do fn
32+
client func() es.Client
33+
logger *zap.Logger
4134
// indexCache cache.Cache
4235
serviceWriter serviceWriter
43-
spanConverter dbmodel.FromDomain
4436
spanServiceIndex spanAndServiceIndexFn
4537
}
4638

47-
type SpanWriterV1 struct {
48-
spanWriter *SpanWriter
39+
// CoreSpanWriter is a DB-Level abstraction which directly deals with database level operations
40+
type CoreSpanWriter interface {
41+
// CreateTemplates creates index templates.
42+
CreateTemplates(spanTemplate, serviceTemplate string, indexPrefix cfg.IndexPrefix) error
43+
// WriteSpan writes a span and its corresponding service:operation in ElasticSearch
44+
WriteSpan(spanStartTime time.Time, span *dbmodel.Span)
45+
// Close closes CoreSpanWriter
46+
Close() error
4947
}
5048

5149
// SpanWriterParams holds constructor parameters for NewSpanWriter
@@ -64,13 +62,6 @@ type SpanWriterParams struct {
6462
ServiceCacheTTL time.Duration
6563
}
6664

67-
// NewSpanWriterV1 returns the SpanWriterV1 for use
68-
func NewSpanWriterV1(p SpanWriterParams) *SpanWriterV1 {
69-
return &SpanWriterV1{
70-
spanWriter: NewSpanWriter(p),
71-
}
72-
}
73-
7465
// NewSpanWriter creates a new SpanWriter for use
7566
func NewSpanWriter(p SpanWriterParams) *SpanWriter {
7667
serviceCacheTTL := p.ServiceCacheTTL
@@ -89,22 +80,13 @@ func NewSpanWriter(p SpanWriterParams) *SpanWriter {
8980

9081
serviceOperationStorage := NewServiceOperationStorage(p.Client, p.Logger, serviceCacheTTL)
9182
return &SpanWriter{
92-
client: p.Client,
93-
logger: p.Logger,
94-
writerMetrics: spanWriterMetrics{
95-
indexCreate: spanstoremetrics.NewWriter(p.MetricsFactory, "index_create"),
96-
},
83+
client: p.Client,
84+
logger: p.Logger,
9785
serviceWriter: serviceOperationStorage.Write,
98-
spanConverter: dbmodel.NewFromDomain(p.AllTagsAsFields, p.TagKeysAsFields, p.TagDotReplacement),
9986
spanServiceIndex: getSpanAndServiceIndexFn(p, writeAliasSuffix),
10087
}
10188
}
10289

103-
// CreateTemplates creates index templates.
104-
func (s *SpanWriterV1) CreateTemplates(spanTemplate, serviceTemplate string, indexPrefix cfg.IndexPrefix) error {
105-
return s.spanWriter.CreateTemplates(spanTemplate, serviceTemplate, indexPrefix)
106-
}
107-
10890
// CreateTemplates creates index templates.
10991
func (s *SpanWriter) CreateTemplates(spanTemplate, serviceTemplate string, indexPrefix cfg.IndexPrefix) error {
11092
jaegerSpanIdx := indexPrefix.Apply("jaeger-span")
@@ -146,23 +128,11 @@ func (s *SpanWriter) WriteSpan(spanStartTime time.Time, span *dbmodel.Span) {
146128
s.logger.Debug("Wrote span to ES index", zap.String("index", spanIndexName))
147129
}
148130

149-
// WriteSpan writes a span and its corresponding service:operation in ElasticSearch
150-
func (s *SpanWriterV1) WriteSpan(_ context.Context, span *model.Span) error {
151-
jsonSpan := s.spanWriter.spanConverter.FromDomainEmbedProcess(span)
152-
s.spanWriter.WriteSpan(span.StartTime, jsonSpan)
153-
return nil
154-
}
155-
156131
// Close closes SpanWriter
157132
func (s *SpanWriter) Close() error {
158133
return s.client().Close()
159134
}
160135

161-
// Close closes SpanWriter
162-
func (s *SpanWriterV1) Close() error {
163-
return s.spanWriter.Close()
164-
}
165-
166136
func keyInCache(key string, c cache.Cache) bool {
167137
return c.Get(key) != nil
168138
}

0 commit comments

Comments
 (0)