forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
254 lines (230 loc) · 7.03 KB
/
memory.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// 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 memory
import (
"errors"
"sync"
"time"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/model/adjuster"
"github.com/jaegertracing/jaeger/pkg/memory/config"
"github.com/jaegertracing/jaeger/storage/spanstore"
)
var errTraceNotFound = errors.New("Trace was not found")
// Store is an in-memory store of traces
type Store struct {
sync.RWMutex
ids []*model.TraceID
traces map[model.TraceID]*model.Trace
services map[string]struct{}
operations map[string]map[string]struct{}
deduper adjuster.Adjuster
config config.Configuration
index int
}
// NewStore creates an unbounded in-memory store
func NewStore() *Store {
return WithConfiguration(config.Configuration{MaxTraces: 0})
}
// WithConfiguration creates a new in memory storage based on the given configuration
func WithConfiguration(configuration config.Configuration) *Store {
return &Store{
ids: make([]*model.TraceID, configuration.MaxTraces),
traces: map[model.TraceID]*model.Trace{},
services: map[string]struct{}{},
operations: map[string]map[string]struct{}{},
deduper: adjuster.SpanIDDeduper(),
config: configuration,
}
}
// GetDependencies returns dependencies between services
func (m *Store) GetDependencies(endTs time.Time, lookback time.Duration) ([]model.DependencyLink, error) {
// deduper used below can modify the spans, so we take an exclusive lock
m.Lock()
defer m.Unlock()
deps := map[string]*model.DependencyLink{}
startTs := endTs.Add(-1 * lookback)
for _, orig := range m.traces {
// SpanIDDeduper never returns an err
trace, _ := m.deduper.Adjust(orig)
if m.traceIsBetweenStartAndEnd(startTs, endTs, trace) {
for _, s := range trace.Spans {
parentSpan := m.findSpan(trace, s.ParentSpanID())
if parentSpan != nil {
if parentSpan.Process.ServiceName == s.Process.ServiceName {
continue
}
depKey := parentSpan.Process.ServiceName + "&&&" + s.Process.ServiceName
if _, ok := deps[depKey]; !ok {
deps[depKey] = &model.DependencyLink{
Parent: parentSpan.Process.ServiceName,
Child: s.Process.ServiceName,
CallCount: 1,
}
} else {
deps[depKey].CallCount++
}
}
}
}
}
retMe := make([]model.DependencyLink, 0, len(deps))
for _, dep := range deps {
retMe = append(retMe, *dep)
}
return retMe, nil
}
func (m *Store) findSpan(trace *model.Trace, spanID model.SpanID) *model.Span {
for _, s := range trace.Spans {
if s.SpanID == spanID {
return s
}
}
return nil
}
func (m *Store) traceIsBetweenStartAndEnd(startTs, endTs time.Time, trace *model.Trace) bool {
for _, s := range trace.Spans {
if s.StartTime.After(startTs) && endTs.After(s.StartTime) {
return true
}
}
return false
}
// WriteSpan writes the given span
func (m *Store) WriteSpan(span *model.Span) error {
m.Lock()
defer m.Unlock()
if _, ok := m.operations[span.Process.ServiceName]; !ok {
m.operations[span.Process.ServiceName] = map[string]struct{}{}
}
m.operations[span.Process.ServiceName][span.OperationName] = struct{}{}
m.services[span.Process.ServiceName] = struct{}{}
if _, ok := m.traces[span.TraceID]; !ok {
m.traces[span.TraceID] = &model.Trace{}
// if we have a limit, let's cleanup the oldest traces
if m.config.MaxTraces > 0 {
// we only have to deal with this slice if we have a limit
m.index = (m.index + 1) % m.config.MaxTraces
// do we have an item already on this position? if so, we are overriding it,
// and we need to remove from the map
if m.ids[m.index] != nil {
delete(m.traces, *m.ids[m.index])
}
// update the ring with the trace id
m.ids[m.index] = &span.TraceID
}
}
m.traces[span.TraceID].Spans = append(m.traces[span.TraceID].Spans, span)
return nil
}
// GetTrace gets a trace
func (m *Store) GetTrace(traceID model.TraceID) (*model.Trace, error) {
m.RLock()
defer m.RUnlock()
retMe := m.traces[traceID]
if retMe == nil {
return nil, errTraceNotFound
}
return retMe, nil
}
// GetServices returns a list of all known services
func (m *Store) GetServices() ([]string, error) {
m.RLock()
defer m.RUnlock()
var retMe []string
for k := range m.services {
retMe = append(retMe, k)
}
return retMe, nil
}
// GetOperations returns the operations of a given service
func (m *Store) GetOperations(service string) ([]string, error) {
m.RLock()
defer m.RUnlock()
if operations, ok := m.operations[service]; ok {
var retMe []string
for ops := range operations {
retMe = append(retMe, ops)
}
return retMe, nil
}
return []string{}, nil
}
// FindTraces returns all traces in the query parameters are satisfied by a trace's span
func (m *Store) FindTraces(query *spanstore.TraceQueryParameters) ([]*model.Trace, error) {
m.RLock()
defer m.RUnlock()
var retMe []*model.Trace
for _, trace := range m.traces {
if len(retMe) >= query.NumTraces {
return retMe, nil
}
if m.validTrace(trace, query) {
retMe = append(retMe, trace)
}
}
return retMe, nil
}
func (m *Store) validTrace(trace *model.Trace, query *spanstore.TraceQueryParameters) bool {
for _, span := range trace.Spans {
if m.validSpan(span, query) {
return true
}
}
return false
}
func findKeyValueMatch(kvs model.KeyValues, key, value string) (model.KeyValue, bool) {
for _, kv := range kvs {
if kv.Key == key && kv.AsString() == value {
return kv, true
}
}
return model.KeyValue{}, false
}
func (m *Store) validSpan(span *model.Span, query *spanstore.TraceQueryParameters) bool {
if query.ServiceName != span.Process.ServiceName {
return false
}
if query.OperationName != "" && query.OperationName != span.OperationName {
return false
}
if query.DurationMin != 0 && span.Duration < query.DurationMin {
return false
}
if query.DurationMax != 0 && span.Duration > query.DurationMax {
return false
}
if !query.StartTimeMin.IsZero() && span.StartTime.Before(query.StartTimeMin) {
return false
}
if !query.StartTimeMax.IsZero() && span.StartTime.After(query.StartTimeMax) {
return false
}
spanKVs := m.flattenTags(span)
for queryK, queryV := range query.Tags {
// (NB): we cannot use the KeyValues.FindKey function because there can be multiple tags with the same key
if _, ok := findKeyValueMatch(spanKVs, queryK, queryV); !ok {
return false
}
}
return true
}
func (m *Store) flattenTags(span *model.Span) model.KeyValues {
retMe := span.Tags
retMe = append(retMe, span.Process.Tags...)
for _, l := range span.Logs {
retMe = append(retMe, l.Fields...)
}
return retMe
}