MCPcopy
hub / github.com/uptrace/uptrace / selectMetricsFromCH

Function selectMetricsFromCH

pkg/metrics/metric_handler.go:141–213  ·  view source on GitHub ↗
(
	ctx context.Context, pg *bun.DB, chdb *ch.DB, f *MetricFilter,
)

Source from the content-addressed store, hash-verified

139}
140
141func selectMetricsFromCH(
142 ctx context.Context, pg *bun.DB, chdb *ch.DB, f *MetricFilter,
143) ([]*Metric, bool, error) {
144 const limit = 1000
145
146 tableName := DatapointTableForWhere(&f.TimeFilter)
147 q := chdb.NewSelect().
148 ColumnExpr("m.metric AS name").
149 ColumnExpr("anyLast(m.instrument) AS instrument").
150 ColumnExpr("uniqCombined64(m.attrs_hash) AS num_timeseries").
151 TableExpr("? AS m", ch.Name(tableName)).
152 Where("m.project_id = ?", f.ProjectID).
153 Where("m.time >= ?", f.TimeGTE).
154 Where("m.time < ?", f.TimeLT).
155 GroupExpr("m.metric").
156 OrderExpr("name ASC").
157 Limit(limit)
158
159 if len(f.AttrKey) > 0 {
160 q = q.Where("hasAll(m.string_keys, ?)", chschema.Array(f.AttrKey))
161 }
162 if len(f.Instrument) > 0 {
163 q = q.Where("m.instrument IN ?", ch.In(f.Instrument))
164 }
165 if len(f.OtelLibraryName) > 0 {
166 q = q.Where("m.otel_library_name IN ?", ch.In(f.OtelLibraryName))
167 }
168 if f.SearchInput != "" {
169 values := strings.Split(f.SearchInput, "|")
170 q = q.Where("multiSearchAnyCaseInsensitiveUTF8(metric, ?) != 0", ch.Array(values))
171 }
172
173 if f.Query != "" {
174 query := mql.ParseQuery(f.Query)
175 for _, part := range query.Parts {
176 if part.Error.Wrapped != nil {
177 continue
178 }
179
180 switch v := part.AST.(type) {
181 case *ast.Where:
182 if err := compileFilters(q, InstrumentDeleted, v.Filters); err != nil {
183 return nil, false, err
184 }
185 default:
186 return nil, false, fmt.Errorf("unsupported AST type: %T", v)
187 }
188 }
189 }
190
191 metrics := make([]*Metric, 0)
192 if err := q.Scan(ctx, &metrics); err != nil {
193 return nil, false, err
194 }
195
196 if len(metrics) == 0 {
197 return metrics, false, nil
198 }

Callers 2

selectMetricsFunction · 0.85
StatsMethod · 0.85

Calls 15

ParseQueryFunction · 0.92
DatapointTableForWhereFunction · 0.85
compileFiltersFunction · 0.85
LimitMethod · 0.80
GroupExprMethod · 0.80
NewSelectMethod · 0.80
WithMethod · 0.80
NameMethod · 0.65
OrderExprMethod · 0.45
WhereMethod · 0.45
TableExprMethod · 0.45
ColumnExprMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…