Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions api/views/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tracing
import (
"context"
"fmt"
"net"
"net/url"
"sort"
"strings"
Expand Down Expand Up @@ -129,6 +130,13 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
var histogram []model.HistogramBucket
var spans []*model.TraceSpan
clients := map[spanKey]string{}
// Requests from monitoring and control-plane applications (metrics
// scrapes, kubelet probes) are not the application's own traffic; the
// overview Traces page hides them by default and this view always does.
var ignoredPeerAddrs []string
if !app.Category.Monitoring() && !app.Category.ControlPlane() {
ignoredPeerAddrs = getAuxPodIps(w)
}
switch {
case traceId != "":
spans, err = ch.GetSpansByTraceId(ctx, traceId)
Expand All @@ -138,10 +146,6 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,

case (source == "" || source == model.TraceSourceOtel) && otelService != "":
source = model.TraceSourceOtel
var ignoredPeerAddrs []string
if !app.Category.Monitoring() {
ignoredPeerAddrs = getMonitoringPodIps(w)
}
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
Expand Down Expand Up @@ -195,7 +199,8 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
defer wg.Done()
var e error
sq := clickhouse.SpanQuery{
Ctx: w.Ctx,
Ctx: w.Ctx,
ExcludePeerAddrs: ignoredPeerAddrs,
}
histogram, e = ch.GetInboundSpansHistogram(ctx, sq, maps.Keys(appClients), listens)
if e != nil {
Expand All @@ -206,13 +211,14 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
defer wg.Done()
var e error
sq := clickhouse.SpanQuery{
Ctx: w.Ctx,
TsFrom: tsFrom,
TsTo: tsTo,
DurFrom: durFrom,
DurTo: durTo,
Errors: errors,
Limit: limit,
Ctx: w.Ctx,
TsFrom: tsFrom,
TsTo: tsTo,
DurFrom: durFrom,
DurTo: durTo,
Errors: errors,
Limit: limit,
ExcludePeerAddrs: ignoredPeerAddrs,
}
spans, e = ch.GetInboundSpans(ctx, sq, maps.Keys(appClients), listens)
if e != nil {
Expand Down Expand Up @@ -383,16 +389,23 @@ func getAppListens(app *model.Application) []model.Listen {
return res
}

func getMonitoringPodIps(w *model.World) []string {
// getAuxPodIps returns every address the monitoring and control-plane
// applications listen on: pod IPs, and the node IPs of host-network agents
// such as the kubelet, whose probes arrive from the node address.
func getAuxPodIps(w *model.World) []string {
seen := map[string]bool{}
var res []string
for _, a := range w.Applications {
if a.Category.Monitoring() {
for _, i := range a.Instances {
for l := range i.TcpListens {
if l.Port == "0" {
res = append(res, l.IP)
}
if !a.Category.Monitoring() && !a.Category.ControlPlane() {
continue
}
for _, i := range a.Instances {
for l := range i.TcpListens {
if ip := net.ParseIP(l.IP); ip == nil || ip.IsLoopback() || seen[l.IP] {
continue
}
seen[l.IP] = true
res = append(res, l.IP)
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions api/views/tracing/tracing_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tracing

import (
"sort"
"testing"

"github.com/coroot/coroot/model"
Expand All @@ -15,3 +16,23 @@ func TestFilterSpans(t *testing.T) {
assert.Equal(t, "a", s.ServiceName)
}
}

func TestGetAuxPodIpsCoversMonitoringAndControlPlane(t *testing.T) {
w := &model.World{Applications: map[model.ApplicationId]*model.Application{}}
add := func(ns, name string, category model.ApplicationCategory, ips ...string) {
app := model.NewApplication(model.NewApplicationId("", ns, model.ApplicationKindDeployment, name))
app.Category = category
i := app.GetOrCreateInstance(name+"-0", nil)
for n, ip := range ips {
i.TcpListens[model.Listen{IP: ip, Port: []string{"0", "10250", "9100"}[n%3]}] = true
}
w.Applications[app.Id] = app
}
add("coroot", "coroot-cluster-agent", model.ApplicationCategoryMonitoring, "192.168.205.139", "127.0.0.1")
add("", "k3s-agent", model.ApplicationCategoryControlPlane, "10.10.33.34", "10.10.33.34", "10.10.33.35")
add("shop-production", "web", model.ApplicationCategoryApplication, "192.168.2.206")

got := getAuxPodIps(w)
sort.Strings(got)
assert.Equal(t, []string{"10.10.33.34", "10.10.33.35", "192.168.205.139"}, got)
}
Loading