Skip to content

Commit b966cc5

Browse files
authored
[refactor] Move ClickHouse Queries To SQL Files With Embed Directive (#7523)
## Which problem is this PR solving? - Towards #7134 ## Description of the changes - This is a small cleanup PR to move the queries in the ClickHouse reader to the `sql` package and pull them into the binary using `go:embed` ## How was this change tested? - CI ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality - [x] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `npm run lint` and `npm run test` --------- Signed-off-by: Mahad Zaryab <mahadzaryab1@gmail.com> Signed-off-by: Mahad Zaryab <43658574+mahadzaryab1@users.noreply.github.com>
1 parent a9bfd75 commit b966cc5

File tree

9 files changed

+187
-184
lines changed

9 files changed

+187
-184
lines changed

internal/storage/v2/clickhouse/schema/schema.sql

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
CREATE TABLE
2+
IF NOT EXISTS spans (
3+
id String,
4+
trace_id String,
5+
trace_state String,
6+
parent_span_id String,
7+
name String,
8+
kind String,
9+
start_time DateTime64 (9),
10+
status_code String,
11+
status_message String,
12+
duration Int64,
13+
bool_attributes Nested (key String, value Bool),
14+
double_attributes Nested (key String, value Float64),
15+
int_attributes Nested (key String, value Int64),
16+
str_attributes Nested (key String, value String),
17+
complex_attributes Nested (key String, value String),
18+
events Nested (
19+
name String,
20+
timestamp DateTime64 (9),
21+
bool_attributes Nested (key String, value Bool),
22+
double_attributes Nested (key String, value Float64),
23+
int_attributes Nested (key String, value Int64),
24+
str_attributes Nested (key String, value String),
25+
complex_attributes Nested (key String, value String)
26+
),
27+
links Nested (
28+
trace_id String,
29+
span_id String,
30+
trace_state String
31+
),
32+
service_name String,
33+
scope_name String,
34+
scope_version String
35+
) ENGINE = MergeTree PRIMARY KEY (trace_id);
36+
37+
CREATE TABLE
38+
IF NOT EXISTS services (name String) ENGINE = AggregatingMergeTree PRIMARY KEY (name);
39+
40+
CREATE MATERIALIZED VIEW IF NOT EXISTS services_mv TO services AS
41+
SELECT
42+
service_name AS name
43+
FROM
44+
spans
45+
GROUP BY
46+
service_name;
47+
48+
CREATE TABLE
49+
IF NOT EXISTS operations (name String, span_kind String) ENGINE = AggregatingMergeTree PRIMARY KEY (name, span_kind);
50+
51+
CREATE MATERIALIZED VIEW IF NOT EXISTS operations_mv TO operations AS
52+
SELECT
53+
name,
54+
kind AS span_kind
55+
FROM
56+
spans
57+
GROUP BY
58+
name,
59+
span_kind;

internal/storage/v2/clickhouse/sql/embed.go

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) 2025 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package sql
5+
6+
import _ "embed"
7+
8+
const InsertSpan = `
9+
INSERT INTO
10+
spans (
11+
id,
12+
trace_id,
13+
trace_state,
14+
parent_span_id,
15+
name,
16+
kind,
17+
start_time,
18+
status_code,
19+
status_message,
20+
duration,
21+
service_name,
22+
scope_name,
23+
scope_version
24+
)
25+
VALUES
26+
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
27+
`
28+
29+
const SelectSpansByTraceID = `
30+
SELECT
31+
id,
32+
trace_id,
33+
trace_state,
34+
parent_span_id,
35+
name,
36+
kind,
37+
start_time,
38+
status_code,
39+
status_message,
40+
duration,
41+
bool_attributes.key,
42+
bool_attributes.value,
43+
double_attributes.key,
44+
double_attributes.value,
45+
int_attributes.key,
46+
int_attributes.value,
47+
str_attributes.key,
48+
str_attributes.value,
49+
complex_attributes.key,
50+
complex_attributes.value,
51+
events.name,
52+
events.timestamp,
53+
events.bool_attributes.key,
54+
events.bool_attributes.value,
55+
events.double_attributes.key,
56+
events.double_attributes.value,
57+
events.int_attributes.key,
58+
events.int_attributes.value,
59+
events.str_attributes.key,
60+
events.str_attributes.value,
61+
events.complex_attributes.key,
62+
events.complex_attributes.value,
63+
links.trace_id,
64+
links.span_id,
65+
links.trace_state,
66+
service_name,
67+
scope_name,
68+
scope_version
69+
FROM
70+
spans
71+
WHERE
72+
trace_id = ?
73+
`
74+
75+
const SelectServices = `
76+
SELECT DISTINCT
77+
name
78+
FROM
79+
services
80+
`
81+
82+
const SelectOperationsAllKinds = `
83+
SELECT
84+
name,
85+
span_kind
86+
FROM
87+
operations
88+
WHERE
89+
service_name = ?
90+
`
91+
92+
const SelectOperationsByKind = `
93+
SELECT
94+
name,
95+
span_kind
96+
FROM
97+
operations
98+
WHERE
99+
service_name = ?
100+
AND span_kind = ?
101+
`
102+
103+
//go:embed create_schema.sql
104+
var CreateSchema string

internal/storage/v2/clickhouse/sql/spans_insert.sql

Lines changed: 0 additions & 29 deletions
This file was deleted.

internal/storage/v2/clickhouse/tracestore/reader.go

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,12 @@ import (
1212
"go.opentelemetry.io/collector/pdata/ptrace"
1313

1414
"github.com/jaegertracing/jaeger/internal/storage/v2/api/tracestore"
15+
"github.com/jaegertracing/jaeger/internal/storage/v2/clickhouse/sql"
1516
"github.com/jaegertracing/jaeger/internal/storage/v2/clickhouse/tracestore/dbmodel"
1617
)
1718

1819
var _ tracestore.Reader = (*Reader)(nil)
1920

20-
const (
21-
sqlSelectSpansByTraceID = `
22-
SELECT
23-
id,
24-
trace_id,
25-
trace_state,
26-
parent_span_id,
27-
name,
28-
kind,
29-
start_time,
30-
status_code,
31-
status_message,
32-
duration,
33-
bool_attributes.key,
34-
bool_attributes.value,
35-
double_attributes.key,
36-
double_attributes.value,
37-
int_attributes.key,
38-
int_attributes.value,
39-
str_attributes.key,
40-
str_attributes.value,
41-
complex_attributes.key,
42-
complex_attributes.value,
43-
events.name,
44-
events.timestamp,
45-
events.bool_attributes.key,
46-
events.bool_attributes.value,
47-
events.double_attributes.key,
48-
events.double_attributes.value,
49-
events.int_attributes.key,
50-
events.int_attributes.value,
51-
events.str_attributes.key,
52-
events.str_attributes.value,
53-
events.complex_attributes.key,
54-
events.complex_attributes.value,
55-
links.trace_id,
56-
links.span_id,
57-
links.trace_state,
58-
service_name,
59-
scope_name,
60-
scope_version
61-
FROM spans
62-
WHERE
63-
trace_id = ?`
64-
sqlSelectAllServices = `SELECT DISTINCT name FROM services`
65-
sqlSelectOperationsAllKinds = `SELECT name, span_kind
66-
FROM operations
67-
WHERE service_name = ?`
68-
sqlSelectOperationsByKind = `SELECT name, span_kind
69-
FROM operations
70-
WHERE service_name = ? AND span_kind = ?`
71-
)
72-
7321
type Reader struct {
7422
conn driver.Conn
7523
}
@@ -89,7 +37,7 @@ func (r *Reader) GetTraces(
8937
) iter.Seq2[[]ptrace.Traces, error] {
9038
return func(yield func([]ptrace.Traces, error) bool) {
9139
for _, traceID := range traceIDs {
92-
rows, err := r.conn.Query(ctx, sqlSelectSpansByTraceID, traceID.TraceID)
40+
rows, err := r.conn.Query(ctx, sql.SelectSpansByTraceID, traceID.TraceID)
9341
if err != nil {
9442
yield(nil, fmt.Errorf("failed to query trace: %w", err))
9543
return
@@ -126,7 +74,7 @@ func (r *Reader) GetTraces(
12674
}
12775

12876
func (r *Reader) GetServices(ctx context.Context) ([]string, error) {
129-
rows, err := r.conn.Query(ctx, sqlSelectAllServices)
77+
rows, err := r.conn.Query(ctx, sql.SelectServices)
13078
if err != nil {
13179
return nil, fmt.Errorf("failed to query services: %w", err)
13280
}
@@ -150,9 +98,9 @@ func (r *Reader) GetOperations(
15098
var rows driver.Rows
15199
var err error
152100
if query.SpanKind == "" {
153-
rows, err = r.conn.Query(ctx, sqlSelectOperationsAllKinds, query.ServiceName)
101+
rows, err = r.conn.Query(ctx, sql.SelectOperationsAllKinds, query.ServiceName)
154102
} else {
155-
rows, err = r.conn.Query(ctx, sqlSelectOperationsByKind, query.ServiceName, query.SpanKind)
103+
rows, err = r.conn.Query(ctx, sql.SelectOperationsByKind, query.ServiceName, query.SpanKind)
156104
}
157105
if err != nil {
158106
return nil, fmt.Errorf("failed to query operations: %w", err)

0 commit comments

Comments
 (0)