Skip to content

Commit 9ffc2fc

Browse files
tmszdmskyurishkuro
authored andcommitted
Do not exceed ES _id length limit (#905)
* leave document _id generation to Eleasticsearch Signed-off-by: Tomasz Adamski <tomasz.adamski@gmail.com> * Calculate sha256 checksum for document _id for Elasticsearch. Instead of using arbitrary string (which length can be quite big) as document _id we compute sha256 from it instead. Signed-off-by: Łukasz Harasimowicz <dev@harnash.eu> * Using Hashable interface to calculate Elasticsearch ids Signed-off-by: Łukasz Harasimowicz <dev@harnash.eu> * Introduce Service::hashCode() to calculate Elasticsearch _id Signed-off-by: Łukasz Harasimowicz <dev@harnash.eu> * Do not calculate Service hash in tests Signed-off-by: Łukasz Harasimowicz <dev@harnash.eu>
1 parent 48bbbae commit 9ffc2fc

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

plugin/storage/es/spanstore/service_operation.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package spanstore
1616

1717
import (
1818
"context"
19-
"fmt"
2019
"time"
2120

2221
"github.com/pkg/errors"
@@ -74,10 +73,10 @@ func (s *ServiceOperationStorage) Write(indexName string, jsonSpan *jModel.Span)
7473
ServiceName: jsonSpan.Process.ServiceName,
7574
OperationName: jsonSpan.OperationName,
7675
}
77-
serviceID := fmt.Sprintf("%s|%s", service.ServiceName, service.OperationName)
78-
cacheKey := fmt.Sprintf("%s:%s", indexName, serviceID)
76+
77+
cacheKey := service.hashCode()
7978
if !keyInCache(cacheKey, s.serviceCache) {
80-
s.client.Index().Index(indexName).Type(serviceType).Id(serviceID).BodyJson(service).Add()
79+
s.client.Index().Index(indexName).Type(serviceType).Id(cacheKey).BodyJson(service).Add()
8180
writeCache(cacheKey, s.serviceCache)
8281
}
8382
}

plugin/storage/es/spanstore/service_operation_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@ func TestWriteService(t *testing.T) {
3131
indexService := &mocks.IndexService{}
3232

3333
indexName := "jaeger-1995-04-21"
34+
serviceHash := "de3b5a8f1a79989d"
35+
3436
indexService.On("Index", stringMatcher(indexName)).Return(indexService)
3537
indexService.On("Type", stringMatcher(serviceType)).Return(indexService)
36-
indexService.On("Id", stringMatcher("service|operation")).Return(indexService)
38+
indexService.On("Id", stringMatcher(serviceHash)).Return(indexService)
3739
indexService.On("BodyJson", mock.AnythingOfType("spanstore.Service")).Return(indexService)
3840
indexService.On("Add")
3941

@@ -64,9 +66,11 @@ func TestWriteServiceError(t *testing.T) {
6466
indexService := &mocks.IndexService{}
6567

6668
indexName := "jaeger-1995-04-21"
69+
serviceHash := "de3b5a8f1a79989d"
70+
6771
indexService.On("Index", stringMatcher(indexName)).Return(indexService)
6872
indexService.On("Type", stringMatcher(serviceType)).Return(indexService)
69-
indexService.On("Id", stringMatcher("service|operation")).Return(indexService)
73+
indexService.On("Id", stringMatcher(serviceHash)).Return(indexService)
7074
indexService.On("BodyJson", mock.AnythingOfType("spanstore.Service")).Return(indexService)
7175
indexService.On("Add")
7276

plugin/storage/es/spanstore/writer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package spanstore
1616

1717
import (
1818
"context"
19+
"fmt"
20+
"hash/fnv"
1921
"strconv"
2022
"strings"
2123
"time"
@@ -73,6 +75,13 @@ type Span struct {
7375
StartTimeMillis uint64 `json:"startTimeMillis"`
7476
}
7577

78+
func (s Service) hashCode() string {
79+
h := fnv.New64a()
80+
h.Write([]byte(s.ServiceName))
81+
h.Write([]byte(s.OperationName))
82+
return fmt.Sprintf("%x", h.Sum64())
83+
}
84+
7685
// NewSpanWriter creates a new SpanWriter for use
7786
func NewSpanWriter(
7887
client es.Client,

plugin/storage/es/spanstore/writer_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func TestSpanWriter_WriteSpan(t *testing.T) {
134134

135135
spanIndexName := "jaeger-span-1995-04-21"
136136
serviceIndexName := "jaeger-service-1995-04-21"
137+
serviceHash := "de3b5a8f1a79989d"
137138

138139
serviceExistsService := &mocks.IndicesExistsService{}
139140
spanExistsService := &mocks.IndicesExistsService{}
@@ -159,7 +160,7 @@ func TestSpanWriter_WriteSpan(t *testing.T) {
159160
indexService.On("Type", stringMatcher(serviceType)).Return(indexServicePut)
160161
indexService.On("Type", stringMatcher(spanType)).Return(indexSpanPut)
161162

162-
indexServicePut.On("Id", stringMatcher("service|operation")).Return(indexServicePut)
163+
indexServicePut.On("Id", stringMatcher(serviceHash)).Return(indexServicePut)
163164
indexServicePut.On("BodyJson", mock.AnythingOfType("spanstore.Service")).Return(indexServicePut)
164165
indexServicePut.On("Add")
165166

0 commit comments

Comments
 (0)