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
59 changes: 59 additions & 0 deletions enrichments/trace/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 {
Enabled bool `mapstructure:"enabled"`
}

// Enabled returns a config with all default enrichments enabled.
func Enabled() Config {
return Config{
Transaction: ElasticTransactionConfig{
Type: AttributeConfig{Enabled: true},
Result: AttributeConfig{Enabled: true},
EventOutcome: AttributeConfig{Enabled: true},
},
Span: ElasticSpanConfig{
EventOutcome: AttributeConfig{Enabled: true},
ServiceTarget: AttributeConfig{Enabled: true},
},
}
}
42 changes: 42 additions & 0 deletions enrichments/trace/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestEnabled(t *testing.T) {
config := Enabled()
assertAllEnabled(t, reflect.ValueOf(config.Transaction))
assertAllEnabled(t, reflect.ValueOf(config.Span))
}

func assertAllEnabled(t *testing.T, cfg reflect.Value) {
t.Helper()

for i := 0; i < cfg.NumField(); i++ {
rAttrCfg := cfg.Field(i).Interface()
attrCfg, ok := rAttrCfg.(AttributeConfig)
require.True(t, ok, "must be a type of AttributeConfig")
require.True(t, attrCfg.Enabled, "must be 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.Enabled {
s.setTxnType(span)
}
if cfg.Result.Enabled {
s.setTxnResult(span)
}
if cfg.EventOutcome.Enabled {
s.setEventOutcome(span)
} else {
}
}

func (s *spanEnrichmentContext) enrichSpan(
span ptrace.Span,
cfg config.ElasticSpanConfig,
) {
if cfg.EventOutcome.Enabled {
s.setEventOutcome(span)
}
if cfg.ServiceTarget.Enabled {
s.setServiceTarget(span)
}
}
Expand Down
Loading