Skip to content

Commit

Permalink
Merge pull request #236 from coroot/otel_fix_service_detection
Browse files Browse the repository at this point in the history
otel: fix service detection for traces and logs
  • Loading branch information
apetruhin committed May 27, 2024
2 parents 46e1bb4 + f240978 commit 41afd67
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
41 changes: 25 additions & 16 deletions api/views/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/coroot/coroot/timeseries"
"github.com/coroot/coroot/utils"
"github.com/coroot/logparser"
"golang.org/x/exp/maps"
"k8s.io/klog"
)

Expand Down Expand Up @@ -133,22 +132,32 @@ func renderEntries(ctx context.Context, v *View, ch *clickhouse.Client, app *mod
return
}

service := ""
if appSettings != nil && appSettings.Logs != nil {
service = appSettings.Logs.Service
} else {
service = model.GuessService(maps.Keys(services), app.Id)
}
var logsFromAgentFound bool
var otelServices []string
for s := range services {
if strings.HasPrefix(s, "/") {
v.Sources = append(v.Sources, model.LogSourceAgent)
logsFromAgentFound = true
} else {
v.Services = append(v.Services, s)
if s == service {
v.Service = s
v.Sources = append(v.Sources, model.LogSourceOtel)
}
otelServices = append(otelServices, s)
}
}
otelService := ""
if appSettings != nil && appSettings.Logs != nil {
otelService = appSettings.Logs.Service
} else {
otelService = model.GuessService(otelServices, app.Id)
}

if logsFromAgentFound {
v.Sources = append(v.Sources, model.LogSourceAgent)
}

for _, s := range otelServices {
if s == otelService {
v.Service = s
v.Sources = append(v.Sources, model.LogSourceOtel)
}
v.Services = append(v.Services, s)
}
sort.Strings(v.Services)

Expand All @@ -172,15 +181,15 @@ func renderEntries(ctx context.Context, v *View, ch *clickhouse.Client, app *mod
var entries []*model.LogEntry
switch v.Source {
case model.LogSourceOtel:
v.Message = fmt.Sprintf("Using OpenTelemetry logs of <i>%s</i>", service)
v.Message = fmt.Sprintf("Using OpenTelemetry logs of <i>%s</i>", otelService)
v.Severities = services[v.Service]
if len(v.Severity) == 0 {
v.Severity = v.Severities
}
if v.View == viewMessages {
histogram, err = ch.GetServiceLogsHistogram(ctx, w.Ctx.From, w.Ctx.To, w.Ctx.Step, service, v.Severity, q.Search)
histogram, err = ch.GetServiceLogsHistogram(ctx, w.Ctx.From, w.Ctx.To, w.Ctx.Step, otelService, v.Severity, q.Search)
if err == nil {
entries, err = ch.GetServiceLogs(ctx, w.Ctx.From, w.Ctx.To, service, v.Severity, q.Search, q.Limit)
entries, err = ch.GetServiceLogs(ctx, w.Ctx.From, w.Ctx.To, otelService, v.Severity, q.Search, q.Limit)
}
}
case model.LogSourceAgent:
Expand Down
37 changes: 20 additions & 17 deletions api/views/tracing/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,35 +88,38 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
v.Message = fmt.Sprintf("Clickhouse error: %s", err)
return v
}
service := ""
if appSettings != nil && appSettings.Tracing != nil {
service = appSettings.Tracing.Service
} else {
service = model.GuessService(services, app.Id)
}
var serviceFound, ebpfSpansFound bool

var ebpfSpansFound bool
var otelServices []string
for _, s := range services {
if strings.HasPrefix(s, "/") {
ebpfSpansFound = true
} else {
if s == service {
serviceFound = true
}
v.Services = append(v.Services, Service{Name: s, Linked: s == service})
otelServices = append(otelServices, s)
}
}

var otelService string
if appSettings != nil && appSettings.Tracing != nil {
otelService = appSettings.Tracing.Service
} else {
otelService = model.GuessService(otelServices, app.Id)
}
for _, s := range otelServices {
v.Services = append(v.Services, Service{Name: s, Linked: s == otelService})
}
sort.Slice(v.Services, func(i, j int) bool {
return v.Services[i].Name < v.Services[j].Name
})

if serviceFound {
if len(otelServices) > 0 {
v.Sources = append(v.Sources, Source{Type: model.TraceSourceOtel, Name: "OpenTelemetry"})
}
if ebpfSpansFound {
v.Sources = append(v.Sources, Source{Type: model.TraceSourceAgent, Name: "OpenTelemetry (eBPF)"})
}

if !serviceFound && !ebpfSpansFound {
if len(v.Sources) == 0 {
v.Status = model.UNKNOWN
v.Message = "No traces found"
return v
Expand All @@ -129,7 +132,7 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
case traceId != "":
spans, err = ch.GetSpansByTraceId(ctx, traceId)

case (source == "" || source == model.TraceSourceOtel) && serviceFound:
case (source == "" || source == model.TraceSourceOtel) && otelService != "":
source = model.TraceSourceOtel
var ignoredPeerAddrs []string
if !app.Category.Monitoring() {
Expand All @@ -144,7 +147,7 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
Ctx: w.Ctx,
ExcludePeerAddrs: ignoredPeerAddrs,
}
sq.Filters = append(sq.Filters, clickhouse.NewSpanFilter("ServiceName", "=", service))
sq.Filters = append(sq.Filters, clickhouse.NewSpanFilter("ServiceName", "=", otelService))
histogram, e = ch.GetSpansByServiceNameHistogram(ctx, sq)
if e != nil {
err = e
Expand All @@ -164,7 +167,7 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,
Limit: limit,
ExcludePeerAddrs: ignoredPeerAddrs,
}
sq.Filters = append(sq.Filters, clickhouse.NewSpanFilter("ServiceName", "=", service))
sq.Filters = append(sq.Filters, clickhouse.NewSpanFilter("ServiceName", "=", otelService))
spans, e = ch.GetSpansByServiceName(ctx, sq)
if e != nil {
err = e
Expand Down Expand Up @@ -222,7 +225,7 @@ func Render(ctx context.Context, ch *clickhouse.Client, app *model.Application,

switch source {
case model.TraceSourceOtel:
v.Message = fmt.Sprintf("Using traces of <i>%s</i>", service)
v.Message = fmt.Sprintf("Using traces of <i>%s</i>", otelService)
case model.TraceSourceAgent:
v.Message = "Using data gathered by the eBPF tracer"
}
Expand Down

0 comments on commit 41afd67

Please sign in to comment.