This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
103 lines (91 loc) · 3.45 KB
/
storage.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
// 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 dependencystore
import (
"time"
"github.com/pkg/errors"
"github.com/uber/jaeger-lib/metrics"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/pkg/cassandra"
casMetrics "github.com/jaegertracing/jaeger/pkg/cassandra/metrics"
)
const (
depsInsertStmt = "INSERT INTO dependencies(ts, ts_index, dependencies) VALUES (?, ?, ?)"
depsSelectStmt = "SELECT ts, dependencies FROM dependencies WHERE ts_index >= ? AND ts_index < ?"
)
// DependencyStore handles all queries and insertions to Cassandra dependencies
type DependencyStore struct {
session cassandra.Session
dependencyDataFrequency time.Duration
dependenciesTableMetrics *casMetrics.Table
logger *zap.Logger
}
// NewDependencyStore returns a DependencyStore
func NewDependencyStore(
session cassandra.Session,
dependencyDataFrequency time.Duration,
metricsFactory metrics.Factory,
logger *zap.Logger,
) *DependencyStore {
return &DependencyStore{
session: session,
dependencyDataFrequency: dependencyDataFrequency,
dependenciesTableMetrics: casMetrics.NewTable(metricsFactory, "Dependencies"),
logger: logger,
}
}
// WriteDependencies implements dependencystore.Writer#WriteDependencies.
func (s *DependencyStore) WriteDependencies(ts time.Time, dependencies []model.DependencyLink) error {
deps := make([]Dependency, len(dependencies))
for i, d := range dependencies {
deps[i] = Dependency{
Parent: d.Parent,
Child: d.Child,
CallCount: int64(d.CallCount),
}
}
query := s.session.Query(depsInsertStmt, ts, ts, deps)
return s.dependenciesTableMetrics.Exec(query, s.logger)
}
// GetDependencies returns all interservice dependencies
func (s *DependencyStore) GetDependencies(endTs time.Time, lookback time.Duration) ([]model.DependencyLink, error) {
query := s.session.Query(depsSelectStmt, endTs.Add(-1*lookback), endTs)
iter := query.Consistency(cassandra.One).Iter()
var mDependency []model.DependencyLink
var dependencies []Dependency
var ts time.Time
for iter.Scan(&ts, &dependencies) {
for _, dependency := range dependencies {
mDependency = append(mDependency, model.DependencyLink{
Parent: dependency.Parent,
Child: dependency.Child,
CallCount: uint64(dependency.CallCount),
})
}
}
if err := iter.Close(); err != nil {
s.logger.Error("Failure to read Dependencies", zap.Time("endTs", endTs), zap.Duration("lookback", lookback), zap.Error(err))
return nil, errors.Wrap(err, "Error reading dependencies from storage")
}
return mDependency, nil
}
func (s *DependencyStore) timeIntervalToPoints(endTs time.Time, lookback time.Duration) []time.Time {
startTs := endTs.Add(-lookback)
var days []time.Time
for day := endTs; startTs.Before(day); day = day.Add(-s.dependencyDataFrequency) {
days = append(days, day.Truncate(s.dependencyDataFrequency))
}
return days
}