Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 5 additions & 4 deletions plugin/storage/es/spanstore/service_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package spanstore

import (
"context"
"fmt"
"time"

"github.com/jaegertracing/jaeger/model"
"github.com/pkg/errors"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
Expand Down Expand Up @@ -74,10 +74,11 @@ func (s *ServiceOperationStorage) Write(indexName string, jsonSpan *jModel.Span)
ServiceName: jsonSpan.Process.ServiceName,
OperationName: jsonSpan.OperationName,
}
serviceID := fmt.Sprintf("%s|%s", service.ServiceName, service.OperationName)
cacheKey := fmt.Sprintf("%s:%s", indexName, serviceID)

serviceHash, _ := model.HashCode(service)
cacheKey := string(serviceHash)
if !keyInCache(cacheKey, s.serviceCache) {
s.client.Index().Index(indexName).Type(serviceType).Id(serviceID).BodyJson(service).Add()
s.client.Index().Index(indexName).Type(serviceType).Id(cacheKey).BodyJson(service).Add()
writeCache(cacheKey, s.serviceCache)
}
}
Expand Down
21 changes: 19 additions & 2 deletions plugin/storage/es/spanstore/service_operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/olivere/elastic.v5"

"github.com/jaegertracing/jaeger/model"
jModel "github.com/jaegertracing/jaeger/model/json"
"github.com/jaegertracing/jaeger/pkg/es/mocks"
)
Expand All @@ -31,9 +32,17 @@ func TestWriteService(t *testing.T) {
indexService := &mocks.IndexService{}

indexName := "jaeger-1995-04-21"
service := Service{
ServiceName: "service",
OperationName: "operation",
}

serviceHash, err := model.HashCode(service)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this introduces unnecessary public method to Service type. Instead I would simply add something like this:

func (s Service) hashCode() string {
    h := fnv.New64a()
    h.Write(s.ServiceName)
    h.Write(s. OperationName)
    return fmt.Sprintf("%x", h.Sum64())
}

require.NoError(t, err)

indexService.On("Index", stringMatcher(indexName)).Return(indexService)
indexService.On("Type", stringMatcher(serviceType)).Return(indexService)
indexService.On("Id", stringMatcher("service|operation")).Return(indexService)
indexService.On("Id", stringMatcher(string(serviceHash))).Return(indexService)
indexService.On("BodyJson", mock.AnythingOfType("spanstore.Service")).Return(indexService)
indexService.On("Add")

Expand Down Expand Up @@ -64,9 +73,17 @@ func TestWriteServiceError(t *testing.T) {
indexService := &mocks.IndexService{}

indexName := "jaeger-1995-04-21"
service := Service{
ServiceName: "service",
OperationName: "operation",
}

serviceHash, err := model.HashCode(service)
require.NoError(t, err)

indexService.On("Index", stringMatcher(indexName)).Return(indexService)
indexService.On("Type", stringMatcher(serviceType)).Return(indexService)
indexService.On("Id", stringMatcher("service|operation")).Return(indexService)
indexService.On("Id", stringMatcher(string(serviceHash))).Return(indexService)
indexService.On("BodyJson", mock.AnythingOfType("spanstore.Service")).Return(indexService)
indexService.On("Add")

Expand Down
10 changes: 10 additions & 0 deletions plugin/storage/es/spanstore/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package spanstore

import (
"context"
"encoding/gob"
"io"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -73,6 +75,14 @@ type Span struct {
StartTimeMillis uint64 `json:"startTimeMillis"`
}

// Hash implements Hash from Hashable.
func (s Service) Hash(w io.Writer) (err error) {
// gob is not the most efficient way, but it ensures we don't miss any fields.
// See BenchmarkSpanHash in span_test.go
enc := gob.NewEncoder(w)
return enc.Encode(s)
}

// NewSpanWriter creates a new SpanWriter for use
func NewSpanWriter(
client es.Client,
Expand Down
9 changes: 8 additions & 1 deletion plugin/storage/es/spanstore/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func TestSpanWriter_WriteSpan(t *testing.T) {
},
StartTime: date,
}
service := Service{
ServiceName: "service",
OperationName: "operation",
}

spanIndexName := "jaeger-span-1995-04-21"
serviceIndexName := "jaeger-service-1995-04-21"
Expand Down Expand Up @@ -159,7 +163,10 @@ func TestSpanWriter_WriteSpan(t *testing.T) {
indexService.On("Type", stringMatcher(serviceType)).Return(indexServicePut)
indexService.On("Type", stringMatcher(spanType)).Return(indexSpanPut)

indexServicePut.On("Id", stringMatcher("service|operation")).Return(indexServicePut)
serviceHash, err := model.HashCode(service)
require.NoError(t, err)

indexServicePut.On("Id", stringMatcher(string(serviceHash))).Return(indexServicePut)
indexServicePut.On("BodyJson", mock.AnythingOfType("spanstore.Service")).Return(indexServicePut)
indexServicePut.On("Add")

Expand Down