Skip to content

Commit

Permalink
feat: Support runtime metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
adebayor123 committed Sep 18, 2024
1 parent a42b661 commit 62c1865
Show file tree
Hide file tree
Showing 19 changed files with 5,763 additions and 1,904 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (n *attributesNormalizer) copyResourceAttributesToAttributes(attributes, re
}
}
}
// runtime metrics do not have service attribute, so need to manually add
if serviceAttribute, ok := resourceAttributes.Get("aws.local.service"); ok {
attributes.PutStr(attr.AWSLocalService, serviceAttribute.AsString())
}
}

func (n *attributesNormalizer) normalizeTelemetryAttributes(attributes, resourceAttributes pcommon.Map, isTrace bool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestCopyResourceAttributesToAttributes(t *testing.T) {
resourceAttributes.PutStr(resourceAttrKey, attrKey+"-value")
}
resourceAttributes.PutStr("host.id", "i-01ef7d37f42caa168")
resourceAttributes.PutStr("aws.local.service", "PetClinic")

// Create a pcommon.Map for attributes
attributes := pcommon.NewMap()
Expand All @@ -107,6 +108,10 @@ func TestCopyResourceAttributesToAttributes(t *testing.T) {
if value, ok := attributes.Get("K8s.Node"); !ok || value.AsString() != "i-01ef7d37f42caa168" {
t.Errorf("Attribute was not copied correctly: got %v, want %v", value.AsString(), "i-01ef7d37f42caa168")
}

if value, ok := attributes.Get("aws.local.service"); !ok || value.AsString() != "PetClinic" {
t.Errorf("Attribute was not copied correctly: got %v, want %v", value.AsString(), "PetClinic")
}
}

func TestTruncateAttributes(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func (p *MetricPruner) ShouldBeDropped(attributes pcommon.Map) (bool, error) {
return true, errors.New("Metric attribute " + attributeKey + " must contain only ASCII characters.")
}
}
if _, ok := attributes.Get(common.MetricAttributeTelemetrySource); !ok {
return true, errors.New("metric must contain Telemetry.Source")
}
}
return false, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestMetricPrunerWithIndexableAttribute(t *testing.T) {
p := &MetricPruner{}
for _, tt := range tests {
attributes := pcommon.NewMap()
attributes.PutStr(common.MetricAttributeTelemetrySource, "RuntimeMetric")
attributes.PutStr(common.CWMetricAttributeLocalService, tt.val)
t.Run(tt.name, func(t *testing.T) {
got, _ := p.ShouldBeDropped(attributes)
Expand Down Expand Up @@ -71,6 +72,33 @@ func TestMetricPrunerWithNonIndexableAttribute(t *testing.T) {
},
}

p := &MetricPruner{}
for _, tt := range tests {
attributes := pcommon.NewMap()
attributes.PutStr(common.MetricAttributeTelemetrySource, "RuntimeMetric")
attributes.PutStr(common.AttributeEC2InstanceId, tt.val)
t.Run(tt.name, func(t *testing.T) {
got, _ := p.ShouldBeDropped(attributes)
if got != tt.want {
t.Errorf("ShouldBeDropped() got = %v, want %v", got, tt.want)
}
})
}
}

func TestMetricPrunerWithNoTelemetrySourceAttribute(t *testing.T) {
tests := []struct {
name string
val string
want bool
}{
{
"testShouldDropValidChar",
"abc",
true,
},
}

p := &MetricPruner{}
for _, tt := range tests {
attributes := pcommon.NewMap()
Expand Down
6 changes: 5 additions & 1 deletion plugins/processors/awsapplicationsignals/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package awsapplicationsignals

import (
"context"
"unicode"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/pcommon"
Expand Down Expand Up @@ -137,7 +138,10 @@ func (ap *awsapplicationsignalsprocessor) processMetrics(ctx context.Context, md
metrics := ils.Metrics()
for k := 0; k < metrics.Len(); k++ {
m := metrics.At(k)
m.SetName(metricCaser.String(m.Name())) // Ensure metric name is in sentence case
// Check if the first letter of the metric name is not capitalized
if len(m.Name()) > 0 && !unicode.IsUpper(rune(m.Name()[0])) {
m.SetName(metricCaser.String(m.Name())) // Ensure metric name is in sentence case
}
ap.processMetricAttributes(ctx, m, resourceAttributes)
}
}
Expand Down
4 changes: 4 additions & 0 deletions plugins/processors/awsapplicationsignals/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestProcessMetrics(t *testing.T) {
"dim_action": "reserved",
"dim_val": "test",
"dim_op": "keep",
"Telemetry.Source": "RuntimeMetric",
})
ap.processMetrics(ctx, keepMetrics)
assert.Equal(t, "reserved", getDimensionValue(t, keepMetrics, "dim_action"))
Expand All @@ -81,6 +82,7 @@ func TestProcessMetrics(t *testing.T) {
replaceMetrics := generateMetrics(map[string]string{
"dim_action": "reserved",
"dim_val": "test1",
"Telemetry.Source": "RuntimeMetric",
})
ap.processMetrics(ctx, replaceMetrics)
assert.Equal(t, "reserved", getDimensionValue(t, replaceMetrics, "dim_action"))
Expand All @@ -89,12 +91,14 @@ func TestProcessMetrics(t *testing.T) {
dropMetricsByDrop := generateMetrics(map[string]string{
"dim_action": "reserved",
"dim_drop": "hc",
"Telemetry.Source": "RuntimeMetric",
})
ap.processMetrics(ctx, dropMetricsByDrop)
assert.True(t, isMetricNil(dropMetricsByDrop))

dropMetricsByKeep := generateMetrics(map[string]string{
"dim_op": "drop",
"Telemetry.Source": "RuntimeMetric",
})
ap.processMetrics(ctx, dropMetricsByKeep)
assert.True(t, isMetricNil(dropMetricsByKeep))
Expand Down
Loading

0 comments on commit 62c1865

Please sign in to comment.