Skip to content

Commit f14a61a

Browse files
authored
Extend enrichment to support logs and metrics (#200)
* Extend enrichment to support logs and metrics * Set processor.event for logs and metrics * Add header
1 parent ed64a6e commit f14a61a

File tree

12 files changed

+137
-23
lines changed

12 files changed

+137
-23
lines changed

enrichments/trace/config/config.go renamed to enrichments/config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type Config struct {
2424
Transaction ElasticTransactionConfig `mapstructure:"elastic_transaction"`
2525
Span ElasticSpanConfig `mapstructure:"elastic_span"`
2626
SpanEvent SpanEventConfig `mapstructure:"span_event"`
27+
Log ElasticLogConfig `mapstructure:"elastic_log"`
28+
Metric ElasticMetricConfig `mapstructure:"elastic_metric"`
2729
}
2830

2931
// ResourceConfig configures the enrichment of resource attributes.
@@ -97,6 +99,16 @@ type SpanEventConfig struct {
9799
ErrorGroupingName AttributeConfig `mapstructure:"error_grouping_name"`
98100
}
99101

102+
// ElasticLogConfig configures the enrichment attributes for logs
103+
type ElasticLogConfig struct {
104+
ProcessorEvent AttributeConfig `mapstructure:"processor_event"`
105+
}
106+
107+
// ElasticMetricConfig configures the enrichment attributes for metrics
108+
type ElasticMetricConfig struct {
109+
ProcessorEvent AttributeConfig `mapstructure:"processor_event"`
110+
}
111+
100112
// AttributeConfig is the configuration options for each attribute.
101113
type AttributeConfig struct {
102114
Enabled bool `mapstructure:"enabled"`
File renamed without changes.

enrichments/trace/trace.go renamed to enrichments/enricher.go

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
package trace
18+
package enrichments
1919

2020
import (
21-
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
22-
"github.com/elastic/opentelemetry-lib/enrichments/trace/internal/elastic"
21+
"github.com/elastic/opentelemetry-lib/enrichments/config"
22+
"github.com/elastic/opentelemetry-lib/enrichments/internal/elastic"
2323
"github.com/ua-parser/uap-go/uaparser"
24+
"go.opentelemetry.io/collector/pdata/plog"
25+
"go.opentelemetry.io/collector/pdata/pmetric"
2426
"go.opentelemetry.io/collector/pdata/ptrace"
2527
)
2628

@@ -34,19 +36,11 @@ type Enricher struct {
3436
Config config.Config
3537
}
3638

37-
// NewEnricher creates a new instance of Enricher.
38-
func NewEnricher(cfg config.Config) *Enricher {
39-
return &Enricher{
40-
Config: cfg,
41-
userAgentParser: uaparser.NewFromSaved(),
42-
}
43-
}
44-
45-
// Enrich enriches the OTel traces with attributes required to power
39+
// EnrichTraces enriches the OTel traces with attributes required to power
4640
// functionalities in the Elastic UI. The traces are processed as per the
4741
// Elastic's definition of transactions and spans. The traces passed to
4842
// this function are mutated.
49-
func (e *Enricher) Enrich(pt ptrace.Traces) {
43+
func (e *Enricher) EnrichTraces(pt ptrace.Traces) {
5044
resSpans := pt.ResourceSpans()
5145
for i := 0; i < resSpans.Len(); i++ {
5246
resSpan := resSpans.At(i)
@@ -62,3 +56,46 @@ func (e *Enricher) Enrich(pt ptrace.Traces) {
6256
}
6357
}
6458
}
59+
60+
// EnrichLogs enriches the OTel logs with attributes required to power
61+
// functionalities in the Elastic UI. The logs passed to this function are mutated.
62+
func (e *Enricher) EnrichLogs(pl plog.Logs) {
63+
resLogs := pl.ResourceLogs()
64+
for i := 0; i < resLogs.Len(); i++ {
65+
resLog := resLogs.At(i)
66+
elastic.EnrichResource(resLog.Resource(), e.Config)
67+
scopeLogs := resLog.ScopeLogs()
68+
for j := 0; j < scopeLogs.Len(); j++ {
69+
scopeSpan := scopeLogs.At(j)
70+
elastic.EnrichScope(scopeSpan.Scope(), e.Config)
71+
logRecords := scopeSpan.LogRecords()
72+
for k := 0; k < logRecords.Len(); k++ {
73+
elastic.EnrichLog(logRecords.At(k), e.Config)
74+
}
75+
}
76+
}
77+
}
78+
79+
// EnrichMetrics enriches the OTel metrics with attributes required to power
80+
// functionalities in the Elastic UI. The metrics passed to this function are mutated.
81+
func (e *Enricher) EnrichMetrics(pl pmetric.Metrics) {
82+
resMetrics := pl.ResourceMetrics()
83+
for i := 0; i < resMetrics.Len(); i++ {
84+
resMetric := resMetrics.At(i)
85+
elastic.EnrichMetric(resMetric, e.Config)
86+
elastic.EnrichResource(resMetric.Resource(), e.Config)
87+
scopeMetics := resMetric.ScopeMetrics()
88+
for j := 0; j < scopeMetics.Len(); j++ {
89+
scopeMetric := scopeMetics.At(j)
90+
elastic.EnrichScope(scopeMetric.Scope(), e.Config)
91+
}
92+
}
93+
}
94+
95+
// NewEnricher creates a new instance of Enricher.
96+
func NewEnricher(cfg config.Config) *Enricher {
97+
return &Enricher{
98+
Config: cfg,
99+
userAgentParser: uaparser.NewFromSaved(),
100+
}
101+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package elastic
19+
20+
import (
21+
"github.com/elastic/opentelemetry-lib/elasticattr"
22+
"github.com/elastic/opentelemetry-lib/enrichments/config"
23+
"go.opentelemetry.io/collector/pdata/plog"
24+
)
25+
26+
func EnrichLog(log plog.LogRecord, cfg config.Config) {
27+
if cfg.Log.ProcessorEvent.Enabled {
28+
if _, exists := log.Attributes().Get(elasticattr.ProcessorEvent); !exists {
29+
log.Attributes().PutStr(elasticattr.ProcessorEvent, "log")
30+
}
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package elastic
19+
20+
import (
21+
"github.com/elastic/opentelemetry-lib/elasticattr"
22+
"github.com/elastic/opentelemetry-lib/enrichments/config"
23+
"go.opentelemetry.io/collector/pdata/pmetric"
24+
)
25+
26+
func EnrichMetric(metric pmetric.ResourceMetrics, cfg config.Config) {
27+
if cfg.Metric.ProcessorEvent.Enabled {
28+
if _, exists := metric.Resource().Attributes().Get(elasticattr.ProcessorEvent); !exists {
29+
metric.Resource().Attributes().PutStr(elasticattr.ProcessorEvent, "metric")
30+
}
31+
}
32+
}

enrichments/trace/internal/elastic/resource.go renamed to enrichments/internal/elastic/resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"fmt"
2222

2323
"github.com/elastic/opentelemetry-lib/elasticattr"
24-
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
24+
"github.com/elastic/opentelemetry-lib/enrichments/config"
2525
"go.opentelemetry.io/collector/pdata/pcommon"
2626
semconv25 "go.opentelemetry.io/otel/semconv/v1.25.0"
2727
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"

enrichments/trace/internal/elastic/resource_test.go renamed to enrichments/internal/elastic/resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/elastic/opentelemetry-lib/elasticattr"
24-
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
24+
"github.com/elastic/opentelemetry-lib/enrichments/config"
2525
"github.com/google/go-cmp/cmp"
2626
"github.com/stretchr/testify/assert"
2727
"go.opentelemetry.io/collector/pdata/pcommon"

enrichments/trace/internal/elastic/scope.go renamed to enrichments/internal/elastic/scope.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package elastic
1919

2020
import (
2121
"github.com/elastic/opentelemetry-lib/elasticattr"
22-
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
22+
"github.com/elastic/opentelemetry-lib/enrichments/config"
2323
"go.opentelemetry.io/collector/pdata/pcommon"
2424
)
2525

enrichments/trace/internal/elastic/scope_test.go renamed to enrichments/internal/elastic/scope_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/elastic/opentelemetry-lib/elasticattr"
24-
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
24+
"github.com/elastic/opentelemetry-lib/enrichments/config"
2525
"github.com/google/go-cmp/cmp"
2626
"github.com/stretchr/testify/assert"
2727
"go.opentelemetry.io/collector/pdata/pcommon"

enrichments/trace/internal/elastic/span.go renamed to enrichments/internal/elastic/span.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"strings"
3232

3333
"github.com/elastic/opentelemetry-lib/elasticattr"
34-
"github.com/elastic/opentelemetry-lib/enrichments/trace/config"
34+
"github.com/elastic/opentelemetry-lib/enrichments/config"
3535
"github.com/ua-parser/uap-go/uaparser"
3636
"go.opentelemetry.io/collector/pdata/pcommon"
3737
"go.opentelemetry.io/collector/pdata/ptrace"

0 commit comments

Comments
 (0)