Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect OTel instrumentations in applications #1101

Merged
merged 16 commits into from
Aug 26, 2024
3 changes: 3 additions & 0 deletions pkg/beyla/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ var DefaultConfig = Config{
RunMode: process.RunModePrivileged,
Interval: 5 * time.Second,
},
Discovery: services.DiscoveryConfig{
ExcludeOTelInstrumentedServices: true,
},
}

type Config struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/beyla/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/grafana/beyla/pkg/internal/netolly/transform/cidr"
"github.com/grafana/beyla/pkg/internal/traces"
"github.com/grafana/beyla/pkg/kubeflags"
"github.com/grafana/beyla/pkg/services"
"github.com/grafana/beyla/pkg/transform"
)

Expand Down Expand Up @@ -200,6 +201,9 @@ network:
RunMode: process.RunModePrivileged,
Interval: 5 * time.Second,
},
Discovery: services.DiscoveryConfig{
ExcludeOTelInstrumentedServices: true,
},
}, cfg)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/export/alloy/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type tracesReceiver struct {
hostID string
}

func (tr *tracesReceiver) spanDiscarded(span *request.Span) bool {
return span.IgnoreTraces() || span.ServiceID.ExportsOTelTraces()
}

func (tr *tracesReceiver) provideLoop() (pipe.FinalFunc[[]request.Span], error) {
if !tr.cfg.Enabled() {
return pipe.IgnoreFinal[[]request.Span](), nil
Expand All @@ -45,7 +49,7 @@ func (tr *tracesReceiver) provideLoop() (pipe.FinalFunc[[]request.Span], error)
for spans := range in {
for i := range spans {
span := &spans[i]
if span.IgnoreTraces() {
if tr.spanDiscarded(span) {
continue
}

Expand Down
81 changes: 81 additions & 0 deletions pkg/export/alloy/traces_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package alloy

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/attribute"

"github.com/grafana/beyla/pkg/beyla"
"github.com/grafana/beyla/pkg/export/attributes"
"github.com/grafana/beyla/pkg/export/otel"
"github.com/grafana/beyla/pkg/internal/request"
"github.com/grafana/beyla/pkg/internal/svc"
)

func TestTracesSkipsInstrumented(t *testing.T) {
svcNoExport := svc.ID{}

svcNoExportTraces := svc.ID{}
svcNoExportTraces.SetExportsOTelMetrics()

svcExportTraces := svc.ID{}
svcExportTraces.SetExportsOTelTraces()

tests := []struct {
name string
spans []request.Span
filtered bool
}{
{
name: "Foo span is not filtered",
spans: []request.Span{{ServiceID: svcNoExport, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/foo", RequestStart: 100, End: 200}},
filtered: false,
},
{
name: "/v1/metrics span is not filtered",
spans: []request.Span{{ServiceID: svcNoExportTraces, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/metrics", RequestStart: 100, End: 200}},
filtered: false,
},
{
name: "/v1/traces span is filtered",
spans: []request.Span{{ServiceID: svcExportTraces, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/traces", RequestStart: 100, End: 200}},
filtered: true,
},
}

tr := makeTracesTestReceiver()

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
traces := generateTracesForSpans(t, tr, tt.spans)
assert.Equal(t, tt.filtered, len(traces) == 0, tt.name)
})
}
}

func makeTracesTestReceiver() *tracesReceiver {
return &tracesReceiver{
ctx: context.Background(),
cfg: &beyla.TracesReceiverConfig{},
attributes: attributes.Selection{},
hostID: "Alloy",
}
}

func generateTracesForSpans(t *testing.T, tr *tracesReceiver, spans []request.Span) []ptrace.Traces {
res := []ptrace.Traces{}
traceAttrs, err := otel.GetUserSelectedAttributes(tr.attributes)
assert.NoError(t, err)
for i := range spans {
span := &spans[i]
if tr.spanDiscarded(span) {
continue
}
res = append(res, otel.GenerateTraces(span, tr.hostID, traceAttrs, []attribute.KeyValue{}))
}

return res
}
6 changes: 5 additions & 1 deletion pkg/export/otel/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,12 +756,16 @@ func (mr *MetricsReporter) serviceGraphAttributes() []attributes.Field[*request.
})
}

func otelSpanAccepted(span *request.Span, mr *MetricsReporter) bool {
return mr.cfg.OTelMetricsEnabled() && !span.ServiceID.ExportsOTelMetrics()
}

// nolint:cyclop
func (r *Metrics) record(span *request.Span, mr *MetricsReporter) {
t := span.Timings()
duration := t.End.Sub(t.RequestStart).Seconds()

if mr.cfg.OTelMetricsEnabled() {
if otelSpanAccepted(span, mr) {
switch span.Type {
case request.EventTypeHTTP:
if mr.is.HTTPEnabled() {
Expand Down
45 changes: 45 additions & 0 deletions pkg/export/otel/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,51 @@ func TestMetricsConfig_Disabled(t *testing.T) {
assert.False(t, (&MetricsConfig{Grafana: &GrafanaOTLP{Submit: []string{"traces", "metrics"}, InstanceID: "33221"}}).Enabled())
}

func TestSpanMetricsDiscarded(t *testing.T) {
mc := MetricsConfig{
Features: []string{FeatureApplication},
}
mr := MetricsReporter{
cfg: &mc,
}

svcNoExport := svc.ID{}

svcExportMetrics := svc.ID{}
svcExportMetrics.SetExportsOTelMetrics()

svcExportTraces := svc.ID{}
svcExportTraces.SetExportsOTelTraces()

tests := []struct {
name string
span request.Span
discarded bool
}{
{
name: "Foo span is not filtered",
span: request.Span{ServiceID: svcNoExport, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/foo", RequestStart: 100, End: 200},
discarded: false,
},
{
name: "/v1/metrics span is filtered",
span: request.Span{ServiceID: svcExportMetrics, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/metrics", RequestStart: 100, End: 200},
discarded: true,
},
{
name: "/v1/traces span is not filtered",
span: request.Span{ServiceID: svcExportTraces, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/traces", RequestStart: 100, End: 200},
discarded: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.discarded, !otelSpanAccepted(&tt.span, &mr), tt.name)
})
}
}

func (f *fakeInternalMetrics) OTELMetricExport(len int) {
fakeMux.Lock()
defer fakeMux.Unlock()
Expand Down
6 changes: 5 additions & 1 deletion pkg/export/otel/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ func GetUserSelectedAttributes(attrs attributes.Selection) (map[attr.Name]struct
return traceAttrs, err
}

func (tr *tracesOTELReceiver) spanDiscarded(span *request.Span) bool {
return span.IgnoreTraces() || span.ServiceID.ExportsOTelTraces() || !tr.acceptSpan(span)
}

func (tr *tracesOTELReceiver) provideLoop() (pipe.FinalFunc[[]request.Span], error) {
if !tr.cfg.Enabled() {
return pipe.IgnoreFinal[[]request.Span](), nil
Expand Down Expand Up @@ -199,7 +203,7 @@ func (tr *tracesOTELReceiver) provideLoop() (pipe.FinalFunc[[]request.Span], err
for spans := range in {
for i := range spans {
span := &spans[i]
if span.IgnoreTraces() || !tr.acceptSpan(span) {
if tr.spanDiscarded(span) {
continue
}
traces := GenerateTraces(span, tr.ctxInfo.HostID, traceAttrs, envResourceAttrs)
Expand Down
43 changes: 42 additions & 1 deletion pkg/export/otel/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,47 @@ func TestTracesAttrReuse(t *testing.T) {
}
}

func TestTracesSkipsInstrumented(t *testing.T) {
svcNoExport := svc.ID{}

svcNoExportTraces := svc.ID{}
svcNoExportTraces.SetExportsOTelMetrics()

svcExportTraces := svc.ID{}
svcExportTraces.SetExportsOTelTraces()

tests := []struct {
name string
spans []request.Span
filtered bool
}{
{
name: "Foo span is not filtered",
spans: []request.Span{{ServiceID: svcNoExport, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/foo", RequestStart: 100, End: 200}},
filtered: false,
},
{
name: "/v1/metrics span is not filtered",
spans: []request.Span{{ServiceID: svcNoExportTraces, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/metrics", RequestStart: 100, End: 200}},
filtered: false,
},
{
name: "/v1/traces span is filtered",
spans: []request.Span{{ServiceID: svcExportTraces, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/traces", RequestStart: 100, End: 200}},
filtered: true,
},
}

tr := makeTracesTestReceiver([]string{instrumentations.InstrumentationALL})

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
traces := generateTracesForSpans(t, tr, tt.spans)
assert.Equal(t, tt.filtered, len(traces) == 0, tt.name)
})
}
}

type fakeInternalTraces struct {
imetrics.NoopReporter
sum atomic.Int32
Expand Down Expand Up @@ -1183,7 +1224,7 @@ func generateTracesForSpans(t *testing.T, tr *tracesOTELReceiver, spans []reques
assert.NoError(t, err)
for i := range spans {
span := &spans[i]
if span.IgnoreTraces() || !tr.acceptSpan(span) {
if tr.spanDiscarded(span) {
continue
}
res = append(res, GenerateTraces(span, "host-id", traceAttrs, []attribute.KeyValue{}))
Expand Down
7 changes: 6 additions & 1 deletion pkg/export/prom/prom.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,10 @@ func (r *metricsReporter) collectMetrics(input <-chan []request.Span) {
}
}

func (r *metricsReporter) otelSpanObserved(span *request.Span) bool {
return r.cfg.OTelMetricsEnabled() && !span.ServiceID.ExportsOTelMetrics()
}

// nolint:cyclop
func (r *metricsReporter) observe(span *request.Span) {
t := span.Timings()
Expand All @@ -584,7 +588,7 @@ func (r *metricsReporter) observe(span *request.Span) {
targetInfoLabelValues := r.labelValuesTargetInfo(span.ServiceID)
r.targetInfo.WithLabelValues(targetInfoLabelValues...).metric.Set(1)

if r.cfg.OTelMetricsEnabled() {
if r.otelSpanObserved(span) {
switch span.Type {
case request.EventTypeHTTP:
if r.is.HTTPEnabled() {
Expand Down Expand Up @@ -637,6 +641,7 @@ func (r *metricsReporter) observe(span *request.Span) {
}
}
}

if r.cfg.SpanMetricsEnabled() {
lv := r.labelValuesSpans(span)
r.spanMetricsLatency.WithLabelValues(lv...).metric.Observe(duration)
Expand Down
45 changes: 45 additions & 0 deletions pkg/export/prom/prom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,51 @@ func TestAppMetrics_ByInstrumentation(t *testing.T) {
}
}

func TestSpanMetricsDiscarded(t *testing.T) {
mc := PrometheusConfig{
Features: []string{otel.FeatureApplication},
}
mr := metricsReporter{
cfg: &mc,
}

svcNoExport := svc.ID{}

svcExportMetrics := svc.ID{}
svcExportMetrics.SetExportsOTelMetrics()

svcExportTraces := svc.ID{}
svcExportTraces.SetExportsOTelTraces()

tests := []struct {
name string
span request.Span
discarded bool
}{
{
name: "Foo span is not filtered",
span: request.Span{ServiceID: svcNoExport, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/foo", RequestStart: 100, End: 200},
discarded: false,
},
{
name: "/v1/metrics span is filtered",
span: request.Span{ServiceID: svcExportMetrics, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/metrics", RequestStart: 100, End: 200},
discarded: true,
},
{
name: "/v1/traces span is not filtered",
span: request.Span{ServiceID: svcExportTraces, Type: request.EventTypeHTTPClient, Method: "GET", Route: "/v1/traces", RequestStart: 100, End: 200},
discarded: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.discarded, !mr.otelSpanObserved(&tt.span), tt.name)
})
}
}

var mmux = sync.Mutex{}

func getMetrics(t require.TestingT, promURL string) string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/discover/attacher.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func monitorPIDs(tracer *ebpf.ProcessTracer, ie *Instrumentable) {
ie.FileInfo.Service.Name = ie.FileInfo.ExecutableName()
// we mark the service ID as automatically named in case we want to look,
// in later stages of the pipeline, for better automatic service name
ie.FileInfo.Service.AutoName = true
ie.FileInfo.Service.SetAutoName()
}

// allowing the tracer to forward traces from the discovered PID and its children processes
Expand Down
Loading
Loading