Skip to content

Commit

Permalink
traces: add custom filters to the Traces view
Browse files Browse the repository at this point in the history
  • Loading branch information
apetruhin committed May 23, 2024
1 parent 1dae488 commit c56893f
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 66 deletions.
4 changes: 4 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,10 @@ func (api *Api) loadWorldByRequest(r *http.Request) (*model.World, *db.Project,
}

world, cacheStatus, err := api.loadWorld(r.Context(), project, from, to)
if world == nil {
step := increaseStepForBigDurations(to.Sub(from), 15*timeseries.Second)
world = model.NewWorld(from, to.Add(-step), step)
}
return world, project, cacheStatus, err
}

Expand Down
54 changes: 41 additions & 13 deletions api/views/overview/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net"
"strings"
"time"

"github.com/coroot/coroot/utils"
Expand All @@ -28,8 +29,9 @@ type Traces struct {
Message string `json:"message"`
Error string `json:"error"`
Heatmap *model.Heatmap `json:"heatmap"`
Spans []Span `json:"spans"`
Traces []Span `json:"traces"`
Limit int `json:"limit"`
Trace []Span `json:"trace"`
Summary *model.TraceSpanSummary `json:"summary"`
AttrStats []model.TraceSpanAttrStats `json:"attr_stats"`
Errors []model.TraceErrorsStat `json:"errors"`
Expand Down Expand Up @@ -63,17 +65,22 @@ type Query struct {
DurFrom string `json:"dur_from"`
DurTo string `json:"dur_to"`

TraceId string `json:"trace_id"`
ServiceName string `json:"service_name"`
SpanName string `json:"span_name"`
IncludeAux bool `json:"include_aux"`
Diff bool `json:"diff"`
TraceId string `json:"trace_id"`
Filters []Filter `json:"filters"`
IncludeAux bool `json:"include_aux"`
Diff bool `json:"diff"`

durFrom time.Duration
durTo time.Duration
errors bool
}

type Filter struct {
Field string `json:"field"`
Op string `json:"op"`
Value string `json:"value"`
}

func renderTraces(ctx context.Context, ch *clickhouse.Client, w *model.World, query string) *Traces {
res := &Traces{}

Expand All @@ -84,13 +91,15 @@ func renderTraces(ctx context.Context, ch *clickhouse.Client, w *model.World, qu

q := parseQuery(query, w.Ctx)

sq := clickhouse.SpanQuery{
Ctx: w.Ctx,
ServiceName: q.ServiceName,
SpanName: q.SpanName,
sq := clickhouse.SpanQuery{Ctx: w.Ctx}

for _, f := range q.Filters {
sq.Filters = append(sq.Filters, clickhouse.NewSpanFilter(f.Field, f.Op, f.Value))
}

if !q.IncludeAux {
sq.ExcludePeerAddrs = getMonitoringAndControlPlanePodIps(w)
sq.Filters = append(sq.Filters, clickhouse.NewSpanFilter("SpanName", "!~", "GET /(health[z]*|metrics|debug/.+|actuator/.+)"))
}

histogram, err := ch.GetRootSpansHistogram(ctx, sq)
Expand All @@ -106,8 +115,23 @@ func renderTraces(ctx context.Context, ch *clickhouse.Client, w *model.World, qu
}
res.Heatmap.AddSeries("errors", "errors", histogram[0].TimeSeries, "", "err")
} else {
res.Message = "not_found"
return res
services, err := ch.GetServicesFromTraces(ctx)
if err != nil {
klog.Errorln(err)
res.Error = fmt.Sprintf("Clickhouse error: %s", err)
return res
}
var otelTracesFound bool
for _, s := range services {
if !strings.HasPrefix(s, "/") {
otelTracesFound = true
break
}
}
if !otelTracesFound {
res.Message = "not_found"
return res
}
}

sq.TsFrom = q.TsFrom
Expand Down Expand Up @@ -177,7 +201,11 @@ func renderTraces(ctx context.Context, ch *clickhouse.Client, w *model.World, qu
Attributes: e.Attributes,
})
}
res.Spans = append(res.Spans, ss)
if q.TraceId != "" {
res.Trace = append(res.Trace, ss)
} else {
res.Traces = append(res.Traces, ss)
}
}

return res
Expand Down
4 changes: 2 additions & 2 deletions api/views/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
var e error
sq := clickhouse.SpanQuery{
Ctx: w.Ctx,
ServiceName: service,
ExcludePeerAddrs: ignoredPeerAddrs,
}
sq.Filters = append(sq.Filters, clickhouse.NewSpanFilter("ServiceName", "=", service))
histogram, e = ch.GetSpansByServiceNameHistogram(ctx, sq)
if e != nil {
err = e
Expand All @@ -162,9 +162,9 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
DurTo: durTo,
Errors: errors,
Limit: limit,
ServiceName: service,
ExcludePeerAddrs: ignoredPeerAddrs,
}
sq.Filters = append(sq.Filters, clickhouse.NewSpanFilter("ServiceName", "=", service))
spans, e = ch.GetSpansByServiceName(ctx, sq)
if e != nil {
err = e
Expand Down
43 changes: 30 additions & 13 deletions clickhouse/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,29 @@ func (c *Client) getSelectionAndBaselineTraces(ctx context.Context, q SpanQuery)
return selectionTraces, baselineTraces, nil
}

type SpanFilter struct {
Field string
Op string
Value string
}

func NewSpanFilter(field, op, value string) SpanFilter {
return SpanFilter{Field: field, Op: op, Value: value}
}

func (f SpanFilter) String() string {
expr := "%s = '%s'"
switch f.Op {
case "!=":
expr = "%s != '%s'"
case "~":
expr = "match(%s, '%s')"
case "!~":
expr = "NOT match(%s, '%s')"
}
return fmt.Sprintf(expr, f.Field, f.Value)
}

type SpanQuery struct {
Ctx timeseries.Context

Expand All @@ -635,8 +658,7 @@ type SpanQuery struct {

Limit int

ServiceName string
SpanName string
Filters []SpanFilter
ExcludePeerAddrs []string

Diff bool
Expand Down Expand Up @@ -679,15 +701,10 @@ func (q SpanQuery) RootSpansFilter() ([]string, []any) {
"SpanKind = 'SPAN_KIND_SERVER'",
"ParentSpanId = ''",
}
var args []any
if q.ServiceName != "" {
filter = append(filter, "ServiceName = @serviceName")
args = append(args, clickhouse.Named("serviceName", q.ServiceName))
}
if q.SpanName != "" {
filter = append(filter, "SpanName = @spanName")
args = append(args, clickhouse.Named("spanName", q.SpanName))
for _, f := range q.Filters {
filter = append(filter, f.String())
}
var args []any
if len(q.ExcludePeerAddrs) > 0 {
filter = append(filter, "NetSockPeerAddr NOT IN (@addrs)")
args = append(args, clickhouse.Named("addrs", q.ExcludePeerAddrs))
Expand All @@ -697,12 +714,12 @@ func (q SpanQuery) RootSpansFilter() ([]string, []any) {

func (q SpanQuery) SpansByServiceNameFilter() ([]string, []any) {
filter := []string{
"ServiceName = @serviceName",
"SpanKind = 'SPAN_KIND_SERVER'",
}
args := []any{
clickhouse.Named("serviceName", q.ServiceName),
for _, f := range q.Filters {
filter = append(filter, f.String())
}
var args []any
if len(q.ExcludePeerAddrs) > 0 {
filter = append(filter, "NetSockPeerAddr NOT IN (@addrs)")
args = append(args, clickhouse.Named("addrs", q.ExcludePeerAddrs))
Expand Down
2 changes: 1 addition & 1 deletion front/src/components/Heatmap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ export default {
}
const p = new Path2D();
rect(p, x + i * w + w / 2, y, w - margin, h - margin);
const b = ys[i] / norm;
const b = norm ? ys[i] / norm : 1;
u.ctx.fillStyle = 'hsl(' + baselineColor + ' 100% ' + (75 - Math.trunc(b * 50)) + '%)';
u.ctx.fill(p);
});
Expand Down
Loading

0 comments on commit c56893f

Please sign in to comment.