-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
writer.go
164 lines (143 loc) · 4.87 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package spanstore
import (
"context"
"time"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/pkg/cache"
"github.com/jaegertracing/jaeger/pkg/es"
"github.com/jaegertracing/jaeger/plugin/storage/es/spanstore/dbmodel"
storageMetrics "github.com/jaegertracing/jaeger/storage/spanstore/metrics"
)
const (
spanType = "span"
serviceType = "service"
)
type spanWriterMetrics struct {
indexCreate *storageMetrics.WriteMetrics
}
type serviceWriter func(string, *dbmodel.Span)
// SpanWriter is a wrapper around elastic.Client
type SpanWriter struct {
ctx context.Context
client es.Client
logger *zap.Logger
writerMetrics spanWriterMetrics // TODO: build functions to wrap around each Do fn
indexCache cache.Cache
serviceWriter serviceWriter
spanConverter dbmodel.FromDomain
spanServiceIndex spanAndServiceIndexFn
}
// SpanWriterParams holds constructor parameters for NewSpanWriter
type SpanWriterParams struct {
Client es.Client
Logger *zap.Logger
MetricsFactory metrics.Factory
IndexPrefix string
AllTagsAsFields bool
TagKeysAsFields []string
TagDotReplacement string
Archive bool
UseReadWriteAliases bool
}
// NewSpanWriter creates a new SpanWriter for use
func NewSpanWriter(p SpanWriterParams) *SpanWriter {
ctx := context.Background()
// TODO: Configurable TTL
serviceOperationStorage := NewServiceOperationStorage(p.Client, p.Logger, time.Hour*12)
return &SpanWriter{
ctx: ctx,
client: p.Client,
logger: p.Logger,
writerMetrics: spanWriterMetrics{
indexCreate: storageMetrics.NewWriteMetrics(p.MetricsFactory, "index_create"),
},
serviceWriter: serviceOperationStorage.Write,
indexCache: cache.NewLRUWithOptions(
5,
&cache.Options{
TTL: 48 * time.Hour,
},
),
spanConverter: dbmodel.NewFromDomain(p.AllTagsAsFields, p.TagKeysAsFields, p.TagDotReplacement),
spanServiceIndex: getSpanAndServiceIndexFn(p.Archive, p.UseReadWriteAliases, p.IndexPrefix),
}
}
// CreateTemplates creates index templates.
func (s *SpanWriter) CreateTemplates(spanTemplate, serviceTemplate string) error {
_, err := s.client.CreateTemplate("jaeger-span").Body(spanTemplate).Do(context.Background())
if err != nil {
return err
}
_, err = s.client.CreateTemplate("jaeger-service").Body(serviceTemplate).Do(context.Background())
if err != nil {
return err
}
return nil
}
// spanAndServiceIndexFn returns names of span and service indices
type spanAndServiceIndexFn func(spanTime time.Time) (string, string)
func getSpanAndServiceIndexFn(archive, useReadWriteAliases bool, prefix string) spanAndServiceIndexFn {
if prefix != "" {
prefix += indexPrefixSeparator
}
spanIndexPrefix := prefix + spanIndex
serviceIndexPrefix := prefix + serviceIndex
if archive {
return func(date time.Time) (string, string) {
if useReadWriteAliases {
return archiveIndex(spanIndexPrefix, archiveWriteIndexSuffix), ""
}
return archiveIndex(spanIndexPrefix, archiveIndexSuffix), ""
}
}
if useReadWriteAliases {
return func(spanTime time.Time) (string, string) {
return spanIndexPrefix + "write", serviceIndexPrefix + "write"
}
}
return func(date time.Time) (string, string) {
return indexWithDate(spanIndexPrefix, date), indexWithDate(serviceIndexPrefix, date)
}
}
// WriteSpan writes a span and its corresponding service:operation in ElasticSearch
func (s *SpanWriter) WriteSpan(span *model.Span) error {
spanIndexName, serviceIndexName := s.spanServiceIndex(span.StartTime)
jsonSpan := s.spanConverter.FromDomainEmbedProcess(span)
if serviceIndexName != "" {
s.writeService(serviceIndexName, jsonSpan)
}
s.writeSpan(spanIndexName, jsonSpan)
return nil
}
// Close closes SpanWriter
func (s *SpanWriter) Close() error {
return s.client.Close()
}
func keyInCache(key string, c cache.Cache) bool {
return c.Get(key) != nil
}
func writeCache(key string, c cache.Cache) {
c.Put(key, key)
}
func (s *SpanWriter) writeService(indexName string, jsonSpan *dbmodel.Span) {
s.serviceWriter(indexName, jsonSpan)
}
func (s *SpanWriter) writeSpan(indexName string, jsonSpan *dbmodel.Span) {
s.client.Index().Index(indexName).Type(spanType).BodyJson(&jsonSpan).Add()
}