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

Add configuration to disable attribute enrichment #58

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
44 changes: 44 additions & 0 deletions enrichments/trace/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package config

// Config configures the enrichment attributes produced.
type Config struct {
Transaction ElasticTransactionConfig `mapstructure:"elastic_transaction"`
Span ElasticSpanConfig `mapstructure:"elastic_span"`
}

// ElasticTransactionConfig configures the enrichment attributes for the
// spans which are identified as elastic transaction.
type ElasticTransactionConfig struct {
Type AttributeConfig `mapstructure:"type"`
Result AttributeConfig `mapstructure:"result"`
EventOutcome AttributeConfig `mapstructure:"event_outcome"`
}

// ElasticSpanConfig configures the enrichment attributes for the spans
// which are NOT identified as elastic transaction.
type ElasticSpanConfig struct {
EventOutcome AttributeConfig `mapstructure:"event_outcome"`
ServiceTarget AttributeConfig `mapstructure:"service_target"`
}

// AttributeConfig is the configuration options for each attribute.
type AttributeConfig struct {
Disabled bool `mapstructure:"disabled"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it comes to the collector config, I think it may be a bit easier to understand if we use enabled: true|false, with the default being enabled for all attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I debated a bit on this (enabled vs disabled). If we choose enabled then we would have to provide something like AllEnabledConfig that can be used in the processor and be careful when we add new attributes. That is also okay. Do you think it makes sense to use enabled all the way (here in the lib as well as in the processor)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to use enabled as the config and also provided a method to get a configuration with all enrichments enabled.

}
34 changes: 29 additions & 5 deletions enrichments/trace/internal/elastic/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/url"
"strconv"

"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
semconv "go.opentelemetry.io/collector/semconv/v1.25.0"
Expand All @@ -40,9 +41,9 @@ import (
// - Elastic spans, defined as all spans (including transactions).
// However, for the enrichment logic spans are treated as a separate
// entity i.e. all transactions are not enriched as spans and vice versa.
func EnrichSpan(span ptrace.Span) {
func EnrichSpan(span ptrace.Span, cfg config.Config) {
var c spanEnrichmentContext
c.Enrich(span)
c.Enrich(span, cfg)
}

type spanEnrichmentContext struct {
Expand Down Expand Up @@ -73,7 +74,7 @@ type spanEnrichmentContext struct {
messagingDestinationTemp bool
}

func (s *spanEnrichmentContext) Enrich(span ptrace.Span) {
func (s *spanEnrichmentContext) Enrich(span ptrace.Span, cfg config.Config) {
// Extract top level span information.
s.spanStatusCode = span.Status().Code()

Expand Down Expand Up @@ -149,13 +150,36 @@ func (s *spanEnrichmentContext) Enrich(span ptrace.Span) {
// Ensure all dependent attributes are handled.
s.normalizeAttributes()

// Enrich the span depending on the nature of the span.
if isElasticTransaction(span) {
s.enrichTransaction(span, cfg.Transaction)
} else {
s.enrichSpan(span, cfg.Span)
}
}

func (s *spanEnrichmentContext) enrichTransaction(
span ptrace.Span,
cfg config.ElasticTransactionConfig,
) {
if !cfg.Type.Disabled {
s.setTxnType(span)
}
if !cfg.Result.Disabled {
s.setTxnResult(span)
}
if !cfg.EventOutcome.Disabled {
s.setEventOutcome(span)
} else {
}
}

func (s *spanEnrichmentContext) enrichSpan(
span ptrace.Span,
cfg config.ElasticSpanConfig,
) {
if !cfg.EventOutcome.Disabled {
s.setEventOutcome(span)
}
if !cfg.ServiceTarget.Disabled {
s.setServiceTarget(span)
}
}
Expand Down
30 changes: 28 additions & 2 deletions enrichments/trace/internal/elastic/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"testing"

"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/pdata/ptrace"
Expand All @@ -34,6 +35,7 @@ func TestElasticTransactionEnrich(t *testing.T) {
for _, tc := range []struct {
name string
input ptrace.Span
config config.ElasticTransactionConfig
enrichedAttrs map[string]any
}{
{
Expand All @@ -45,6 +47,16 @@ func TestElasticTransactionEnrich(t *testing.T) {
AttributeTransactionType: "unknown",
},
},
{
name: "all_disabled",
input: ptrace.NewSpan(),
config: config.ElasticTransactionConfig{
Type: config.AttributeConfig{Disabled: true},
Result: config.AttributeConfig{Disabled: true},
EventOutcome: config.AttributeConfig{Disabled: true},
},
enrichedAttrs: map[string]any{},
},
{
name: "http_status_ok",
input: func() ptrace.Span {
Expand Down Expand Up @@ -177,7 +189,9 @@ func TestElasticTransactionEnrich(t *testing.T) {
expectedAttrs[k] = v
}

EnrichSpan(tc.input)
EnrichSpan(tc.input, config.Config{
Transaction: tc.config,
})

assert.Empty(t, cmp.Diff(expectedAttrs, tc.input.Attributes().AsRaw()))
})
Expand All @@ -194,6 +208,7 @@ func TestElasticSpanEnrich(t *testing.T) {
for _, tc := range []struct {
name string
input ptrace.Span
config config.ElasticSpanConfig
enrichedAttrs map[string]any
}{
{
Expand All @@ -203,6 +218,15 @@ func TestElasticSpanEnrich(t *testing.T) {
AttributeEventOutcome: "success",
},
},
{
name: "all_disabled",
input: getElasticSpan(),
config: config.ElasticSpanConfig{
EventOutcome: config.AttributeConfig{Disabled: true},
ServiceTarget: config.AttributeConfig{Disabled: true},
},
enrichedAttrs: map[string]any{},
},
{
name: "peer_service",
input: func() ptrace.Span {
Expand Down Expand Up @@ -422,7 +446,9 @@ func TestElasticSpanEnrich(t *testing.T) {
expectedAttrs[k] = v
}

EnrichSpan(tc.input)
EnrichSpan(tc.input, config.Config{
Span: tc.config,
})

assert.Empty(t, cmp.Diff(expectedAttrs, tc.input.Attributes().AsRaw()))
})
Expand Down
18 changes: 16 additions & 2 deletions enrichments/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,29 @@
package trace

import (
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
"github.com/elastic/opentelemetry-lib/enrichments/trace/internal/elastic"
"go.opentelemetry.io/collector/pdata/ptrace"
)

// Enricher enriches the OTel traces with attributes required to power
// functionalities in the Elastic UI.
type Enricher struct {
Config config.Config
}

// NewEnricher creates a new instance of Enricher.
func NewEnricher(cfg config.Config) Enricher {
return Enricher{
Config: cfg,
}
}

// Enrich enriches the OTel traces with attributes required to power
// functionalities in the Elastic UI. The traces are processed as per the
// Elastic's definition of transactions and spans. The traces passed to
// this function are mutated.
func Enrich(pt ptrace.Traces) {
func (e *Enricher) Enrich(pt ptrace.Traces) {
resSpans := pt.ResourceSpans()
for i := 0; i < resSpans.Len(); i++ {
resSpan := resSpans.At(i)
Expand All @@ -35,7 +49,7 @@ func Enrich(pt ptrace.Traces) {
scopeSpan := scopeSpans.At(j)
spans := scopeSpan.Spans()
for k := 0; k < spans.Len(); k++ {
elastic.EnrichSpan(spans.At(k))
elastic.EnrichSpan(spans.At(k), e.Config)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion enrichments/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"testing"

"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand All @@ -31,11 +32,12 @@ func BenchmarkEnrich(b *testing.B) {
traceFile := filepath.Join("testdata", "trace.yaml")
traces, err := golden.ReadTraces(traceFile)
require.NoError(b, err)
enricher := NewEnricher(config.Config{})

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Enrich(traces)
enricher.Enrich(traces)
}
}

Expand Down