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
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ test/integration/components/gokafka/vendor/*
test/integration/components/gokafka-seg/vendor/*
test/integration/components/gokafka-seg/gokafka
test/integration/components/goredis/vendor/*
test/integration/components/goredis/goredis
test/integration/components/goredis/goredis
test/integration/components/go_otel/rolldice
test/integration/components/go_otel_grpc/rolldice
26 changes: 25 additions & 1 deletion docs/sources/configure/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,37 @@ namespace.
For more details about this section, go to the [discovery services section](#discovery-services-section)
of this document.

| YAML | Environment variable | Type | Default |
| ------------------ | ------- | --------------- | ------- |
| `exclude_services` | N/A | list of objects | (unset) |

This section allows for specifying selection criteria for excluding services from
being instrumented. It follows the same definition format as described in the
[discovery services section](#discovery-services-section) of this document.

This option is useful for avoiding instrumentation of services which are typically
found in observability environments. For example, use this option to exclude instrumenting
Prometheus, the OpenTelemetry collector or Grafana Alloy.

| YAML | Environment variable | Type | Default |
| -------------------------- | -------------------------------- | ------- | ------- |
| `skip_go_specific_tracers` | `BEYLA_SKIP_GO_SPECIFIC_TRACERS` | boolean | false |

Disables the detection of Go specifics when ebpf tracer inspects executables to be instrumented.
Disables the detection of Go specifics when the **ebpf** tracer inspects executables to be instrumented.
The tracer will fallback to using generic instrumentation, which will generally be less efficient.

| YAML | Environment variable | Type | Default |
| ------------------------------------ | ------------------------------------------ | ------- | ------- |
| `exclude_otel_instrumented_services` | `BEYLA_EXCLUDE_OTEL_INSTRUMENTED_SERVICES` | boolean | true |

Disables Beyla instrumentation of services which are already instrumented with OpenTelemetry. Since Beyla
is often deployed to monitor all services in a Kubernetes cluster, monitoring already instrumented services
can lead to duplicate telemetry data, unless the instrumentation selection (or exclusion) criteria is
carefully crafted. To avoid unnecessary configuration overhead, Beyla monitors for the OpenTelemetry SDK calls
to publish metrics and traces, and automatically turns off instrumentation of services which publish their own
telemetry data. Turn this option off if your application generated telemetry data doesn't conflict with the
Beyla generated metrics and traces.

### Discovery services section

Example of YAML file allowing the selection of multiple groups of services:
Expand Down
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
Loading
Loading