Skip to content

Commit

Permalink
Expose cache ttl for es span writer index+service (jaegertracing#2737)
Browse files Browse the repository at this point in the history
  • Loading branch information
necrolyte2 authored Jan 21, 2021
1 parent fe2a8ab commit 2ff0a3d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
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

0 comments on commit 2ff0a3d

Please sign in to comment.