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

Expose cache ttl for es span writer index+service #2737

Merged
merged 3 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions plugin/storage/es/spanstore/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import (
)

const (
spanType = "span"
serviceType = "service"
spanType = "span"
serviceType = "service"
serviceCacheTTLDefault = 12 * time.Hour
indexCacheTTLDefault = 48 * time.Hour
)

type spanWriterMetrics struct {
Expand Down Expand Up @@ -63,12 +65,23 @@ type SpanWriterParams struct {
TagDotReplacement string
Archive bool
UseReadWriteAliases bool
ServiceCacheTTL time.Duration
IndexCacheTTL time.Duration
}

// NewSpanWriter creates a new SpanWriter for use
func NewSpanWriter(p SpanWriterParams) *SpanWriter {
// TODO: Configurable TTL
serviceOperationStorage := NewServiceOperationStorage(p.Client, p.Logger, time.Hour*12)
serviceCacheTTL := p.ServiceCacheTTL
if p.ServiceCacheTTL == 0 {
serviceCacheTTL = serviceCacheTTLDefault
}

indexCacheTTL := p.IndexCacheTTL
if p.ServiceCacheTTL == 0 {
indexCacheTTL = indexCacheTTLDefault
}

serviceOperationStorage := NewServiceOperationStorage(p.Client, p.Logger, serviceCacheTTL)
return &SpanWriter{
client: p.Client,
logger: p.Logger,
Expand All @@ -79,7 +92,7 @@ func NewSpanWriter(p SpanWriterParams) *SpanWriter {
indexCache: cache.NewLRUWithOptions(
5,
&cache.Options{
TTL: 48 * time.Hour,
TTL: indexCacheTTL,
},
),
spanConverter: dbmodel.NewFromDomain(p.AllTagsAsFields, p.TagKeysAsFields, p.TagDotReplacement),
Expand Down
68 changes: 68 additions & 0 deletions plugin/storage/es/spanstore/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,74 @@ func TestNewSpanTags(t *testing.T) {
}
}

func TestSpanWriterParamsTTL(t *testing.T) {
logger, _ := testutils.NewLogger()
metricsFactory := metricstest.NewFactory(0)
testCases := []struct {
indexTTL time.Duration
serviceTTL time.Duration
name string
expectedAddCalls int
}{
{
indexTTL: 0,
serviceTTL: 0,
name: "uses defaults",
expectedAddCalls: 1,
},
{
indexTTL: 1 * time.Nanosecond,
serviceTTL: 1 * time.Nanosecond,
name: "uses provided values",
expectedAddCalls: 3,
},
}

for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
client := &mocks.Client{}
params := SpanWriterParams{
Client: client,
Logger: logger,
MetricsFactory: metricsFactory,
ServiceCacheTTL: test.serviceTTL,
IndexCacheTTL: test.indexTTL,
}
w := NewSpanWriter(params)

svc := dbmodel.Service{
ServiceName: "foo",
OperationName: "bar",
}
serviceHash := hashCode(svc)

serviceIndexName := "jaeger-service-1995-04-21"

indexService := &mocks.IndexService{}

indexService.On("Index", stringMatcher(serviceIndexName)).Return(indexService)
indexService.On("Type", stringMatcher(serviceType)).Return(indexService)
indexService.On("Id", stringMatcher(serviceHash)).Return(indexService)
indexService.On("BodyJson", mock.AnythingOfType("dbmodel.Service")).Return(indexService)
indexService.On("Add")

client.On("Index").Return(indexService)

jsonSpan := &dbmodel.Span{
Process: dbmodel.Process{ServiceName: "foo"},
OperationName: "bar",
}

w.writeService(serviceIndexName, jsonSpan)
time.Sleep(1 * time.Nanosecond)
w.writeService(serviceIndexName, jsonSpan)
time.Sleep(1 * time.Nanosecond)
w.writeService(serviceIndexName, jsonSpan)
indexService.AssertNumberOfCalls(t, "Add", test.expectedAddCalls)
})
}
}

// stringMatcher can match a string argument when it contains a specific substring q
func stringMatcher(q string) interface{} {
matchFunc := func(s string) bool {
Expand Down