Skip to content
This repository was archived by the owner on Oct 29, 2021. It is now read-only.

Commit 11e791d

Browse files
committed
improves code style
1 parent 0283f31 commit 11e791d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

influxdb_store.go

+17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func (in *InfluxDBStore) Collect(id SpanID, anns ...Annotation) error {
4040
if err := in.removeSpanIfExists(id); err != nil {
4141
return err
4242
}
43+
4344
// trace_id, span_id & parent_id are set as tags
4445
// because InfluxDB tags are indexed & those values
4546
// are uselater on queries.
@@ -48,12 +49,14 @@ func (in *InfluxDBStore) Collect(id SpanID, anns ...Annotation) error {
4849
"span_id": id.Span.String(),
4950
"parent_id": id.Parent.String(),
5051
}
52+
5153
// Saving annotations as InfluxDB measurement spans fields
5254
// which are not indexed.
5355
fields := make(map[string]interface{}, len(anns))
5456
for _, ann := range anns {
5557
fields[ann.Key] = string(ann.Value)
5658
}
59+
5760
// InfluxDB point represents a single span.
5861
pts := []influxDBClient.Point{
5962
influxDBClient.Point{
@@ -77,18 +80,21 @@ func (in *InfluxDBStore) Collect(id SpanID, anns ...Annotation) error {
7780

7881
func (in *InfluxDBStore) Trace(id ID) (*Trace, error) {
7982
trace := &Trace{}
83+
8084
// GROUP BY * -> meaning group by all tags(trace_id, span_id & parent_id)
8185
// grouping by all tags includes those and it's values on the query response.
8286
q := fmt.Sprintf("SELECT * FROM spans WHERE trace_id='%s' GROUP BY *", id)
8387
result, err := in.executeOneQuery(q)
8488
if err != nil {
8589
return nil, err
8690
}
91+
8792
// result.Series -> A slice containing all the spans.
8893
if len(result.Series) == 0 {
8994
return nil, errors.New("trace not found")
9095
}
9196
var isRootSpan bool
97+
9298
// Iterate over series(spans) to create trace children's & set trace fields.
9399
for _, s := range result.Series {
94100
span, err := newSpanFromRow(&s)
@@ -118,19 +124,23 @@ func (in *InfluxDBStore) Trace(id ID) (*Trace, error) {
118124

119125
func (in *InfluxDBStore) Traces() ([]*Trace, error) {
120126
traces := make([]*Trace, 0)
127+
121128
// GROUP BY * -> meaning group by all tags(trace_id, span_id & parent_id)
122129
// grouping by all tags includes those and it's values on the query response.
123130
rootSpansQuery := fmt.Sprintf("SELECT * FROM spans WHERE parent_id='%s' GROUP BY * LIMIT %d", zeroID, in.tracesPerPage)
124131
rootSpansResult, err := in.executeOneQuery(rootSpansQuery)
125132
if err != nil {
126133
return nil, err
127134
}
135+
128136
// result.Series -> A slice containing all the spans.
129137
if len(rootSpansResult.Series) == 0 {
130138
return traces, nil
131139
}
140+
132141
// Cache to keep track of traces to be returned.
133142
tracesCache := make(map[ID]*Trace, 0)
143+
134144
// Iterate over series(spans) to create traces.
135145
for _, s := range rootSpansResult.Series {
136146
span, err := newSpanFromRow(&s)
@@ -149,23 +159,27 @@ func (in *InfluxDBStore) Traces() ([]*Trace, error) {
149159
return nil, errors.New("duplicated root span")
150160
}
151161
}
162+
152163
// Using 'OR' since 'IN' not supported yet.
153164
where := `WHERE `
154165
var i int = 1
155166
for _, trace := range tracesCache {
156167
where += fmt.Sprintf("(trace_id='%s' AND parent_id!='%s')", trace.Span.ID.Trace, zeroID)
168+
157169
// Adds 'OR' except for last iteration.
158170
if i != len(tracesCache) && len(tracesCache) > 1 {
159171
where += " OR "
160172
}
161173
i += 1
162174
}
175+
163176
// Queries for all children spans of the traces to be returned.
164177
childrenSpansQuery := fmt.Sprintf("SELECT * FROM spans %s GROUP BY *", where)
165178
childrenSpansResult, err := in.executeOneQuery(childrenSpansQuery)
166179
if err != nil {
167180
return nil, err
168181
}
182+
169183
// Iterate over series(children spans) to create sub-traces
170184
// and associates sub-traces with it's parent trace.
171185
for _, s := range childrenSpansResult.Series {
@@ -220,6 +234,7 @@ func (in *InfluxDBStore) executeOneQuery(command string) (*influxDBClient.Result
220234
if response.Error() != nil {
221235
return nil, response.Error()
222236
}
237+
223238
// Expecting one result, since a single query is executed.
224239
if len(response.Results) != 1 {
225240
return nil, errors.New("unexpected number of results for an influxdb single query")
@@ -263,13 +278,15 @@ func annotationsFromRow(r *influxDBModels.Row) (*Annotations, error) {
263278
if len(r.Values) == 1 {
264279
fields = r.Values[0]
265280
}
281+
266282
// len(r.Values) might be greater than one - meaning there are
267283
// some spans to drop, see: InfluxDBStore.Collect(...).
268284
// If so last one is picked.
269285
if len(r.Values) > 1 {
270286
fields = r.Values[len(r.Values)-1]
271287
}
272288
annotations := make(Annotations, len(fields))
289+
273290
// Iterates over fields which represent span's annotation values.
274291
for i, field := range fields {
275292
// It is safe to do column[0] (eg. 'Server.Request.Method')

0 commit comments

Comments
 (0)