-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
operation_names.go
214 lines (192 loc) · 6.57 KB
/
operation_names.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
// 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 (
"fmt"
"time"
"go.uber.org/zap"
"github.com/jaegertracing/jaeger/pkg/cache"
"github.com/jaegertracing/jaeger/pkg/cassandra"
casMetrics "github.com/jaegertracing/jaeger/pkg/cassandra/metrics"
"github.com/jaegertracing/jaeger/pkg/metrics"
"github.com/jaegertracing/jaeger/plugin/storage/cassandra/spanstore/dbmodel"
"github.com/jaegertracing/jaeger/storage/spanstore"
)
const (
// latestVersion of operation_names table
// increase the version if your table schema changes require code change
latestVersion = schemaVersion("v2")
// previous version of operation_names table
// if latest version does not work, will fail back to use previous version
previousVersion = schemaVersion("v1")
// tableCheckStmt the query statement used to check if a table exists or not
tableCheckStmt = "SELECT * from %s limit 1"
)
type schemaVersion string
type tableMeta struct {
tableName string
insertStmt string
queryByKindStmt string
queryStmt string
createWriteQuery func(query cassandra.Query, service, kind, opName string) cassandra.Query
getOperations func(
s *OperationNamesStorage,
query spanstore.OperationQueryParameters,
) ([]spanstore.Operation, error)
}
func (t *tableMeta) materialize() {
t.insertStmt = fmt.Sprintf(t.insertStmt, t.tableName)
t.queryByKindStmt = fmt.Sprintf(t.queryByKindStmt, t.tableName)
t.queryStmt = fmt.Sprintf(t.queryStmt, t.tableName)
}
var schemas = map[schemaVersion]tableMeta{
previousVersion: {
tableName: "operation_names",
insertStmt: "INSERT INTO %s(service_name, operation_name) VALUES (?, ?)",
queryByKindStmt: "SELECT operation_name FROM %s WHERE service_name = ?",
queryStmt: "SELECT operation_name FROM %s WHERE service_name = ?",
getOperations: getOperationsV1,
createWriteQuery: func(query cassandra.Query, service, kind, opName string) cassandra.Query {
return query.Bind(service, opName)
},
},
latestVersion: {
tableName: "operation_names_v2",
insertStmt: "INSERT INTO %s(service_name, span_kind, operation_name) VALUES (?, ?, ?)",
queryByKindStmt: "SELECT span_kind, operation_name FROM %s WHERE service_name = ? AND span_kind = ?",
queryStmt: "SELECT span_kind, operation_name FROM %s WHERE service_name = ?",
getOperations: getOperationsV2,
createWriteQuery: func(query cassandra.Query, service, kind, opName string) cassandra.Query {
return query.Bind(service, kind, opName)
},
},
}
// OperationNamesStorage stores known operation names by service.
type OperationNamesStorage struct {
// CQL statements are public so that Cassandra2 storage can override them
schemaVersion schemaVersion
table tableMeta
session cassandra.Session
writeCacheTTL time.Duration
metrics *casMetrics.Table
operationNames cache.Cache
logger *zap.Logger
}
// NewOperationNamesStorage returns a new OperationNamesStorage
func NewOperationNamesStorage(
session cassandra.Session,
writeCacheTTL time.Duration,
metricsFactory metrics.Factory,
logger *zap.Logger,
) *OperationNamesStorage {
schemaVersion := latestVersion
if !tableExist(session, schemas[schemaVersion].tableName) {
schemaVersion = previousVersion
}
table := schemas[schemaVersion]
table.materialize()
return &OperationNamesStorage{
session: session,
schemaVersion: schemaVersion,
table: table,
metrics: casMetrics.NewTable(metricsFactory, schemas[schemaVersion].tableName),
writeCacheTTL: writeCacheTTL,
logger: logger,
operationNames: cache.NewLRUWithOptions(
100000,
&cache.Options{
TTL: writeCacheTTL,
InitialCapacity: 10000,
}),
}
}
// Write saves Operation and Service name tuples
func (s *OperationNamesStorage) Write(operation dbmodel.Operation) error {
key := fmt.Sprintf("%s|%s|%s",
operation.ServiceName,
operation.SpanKind,
operation.OperationName,
)
if inCache := checkWriteCache(key, s.operationNames, s.writeCacheTTL); !inCache {
q := s.table.createWriteQuery(
s.session.Query(s.table.insertStmt),
operation.ServiceName,
operation.SpanKind,
operation.OperationName,
)
err := s.metrics.Exec(q, s.logger)
if err != nil {
return err
}
}
return nil
}
// GetOperations returns all operations for a specific service traced by Jaeger
func (s *OperationNamesStorage) GetOperations(
query spanstore.OperationQueryParameters,
) ([]spanstore.Operation, error) {
return s.table.getOperations(s, query)
}
func tableExist(session cassandra.Session, tableName string) bool {
query := session.Query(fmt.Sprintf(tableCheckStmt, tableName))
err := query.Exec()
return err == nil
}
func getOperationsV1(
s *OperationNamesStorage,
query spanstore.OperationQueryParameters,
) ([]spanstore.Operation, error) {
iter := s.session.Query(s.table.queryStmt, query.ServiceName).Iter()
var operation string
var operations []spanstore.Operation
for iter.Scan(&operation) {
operations = append(operations, spanstore.Operation{
Name: operation,
})
}
if err := iter.Close(); err != nil {
err = fmt.Errorf("error reading operation_names from storage: %w", err)
return nil, err
}
return operations, nil
}
func getOperationsV2(
s *OperationNamesStorage,
query spanstore.OperationQueryParameters,
) ([]spanstore.Operation, error) {
var casQuery cassandra.Query
if query.SpanKind == "" {
// Get operations for all spanKind
casQuery = s.session.Query(s.table.queryStmt, query.ServiceName)
} else {
// Get operations for given spanKind
casQuery = s.session.Query(s.table.queryByKindStmt, query.ServiceName, query.SpanKind)
}
iter := casQuery.Iter()
var operationName string
var spanKind string
var operations []spanstore.Operation
for iter.Scan(&spanKind, &operationName) {
operations = append(operations, spanstore.Operation{
Name: operationName,
SpanKind: spanKind,
})
}
if err := iter.Close(); err != nil {
err = fmt.Errorf("error reading %s from storage: %w", s.table.tableName, err)
return nil, err
}
return operations, nil
}