-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[connector/signaltometrics]Add collector telemetry as resource attrib…
…utes
- Loading branch information
Showing
14 changed files
with
326 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
connector/signaltometricsconnector/internal/model/collectorinstanceinfo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package model // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/signaltometricsconnector/internal/model" | ||
|
||
import ( | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
semconv "go.opentelemetry.io/collector/semconv/v1.26.0" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/signaltometricsconnector/internal/metadata" | ||
) | ||
|
||
var prefix = metadata.Type.String() | ||
|
||
// CollectorInstanceInfo holds the attributes that could uniquely identify | ||
// the current collector instance. These attributes are initialized from the | ||
// telemetry settings. The CollectorInstanceInfo can copy these attributes, | ||
// with a given prefix, to a provided map. | ||
type CollectorInstanceInfo struct { | ||
size int | ||
serviceInstanceID string | ||
serviceName string | ||
serviceNamespace string | ||
} | ||
|
||
func NewCollectorInstanceInfo( | ||
set component.TelemetrySettings, | ||
) *CollectorInstanceInfo { | ||
var info CollectorInstanceInfo | ||
set.Resource.Attributes().Range(func(k string, v pcommon.Value) bool { | ||
switch k { | ||
case semconv.AttributeServiceInstanceID: | ||
if str := v.Str(); str != "" { | ||
info.serviceInstanceID = str | ||
info.size++ | ||
} | ||
case semconv.AttributeServiceName: | ||
if str := v.Str(); str != "" { | ||
info.serviceName = str | ||
info.size++ | ||
} | ||
case semconv.AttributeServiceNamespace: | ||
if str := v.Str(); str != "" { | ||
info.serviceNamespace = str | ||
info.size++ | ||
} | ||
} | ||
return true | ||
}) | ||
return &info | ||
} | ||
|
||
// Size returns the max number of attributes that defines a collector's | ||
// instance information. Can be used to presize the attributes. | ||
func (info CollectorInstanceInfo) Size() int { | ||
return info.size | ||
} | ||
|
||
func (info CollectorInstanceInfo) Copy(to pcommon.Map) { | ||
to.EnsureCapacity(info.Size()) | ||
if info.serviceInstanceID != "" { | ||
to.PutStr(keyWithPrefix(semconv.AttributeServiceInstanceID), info.serviceInstanceID) | ||
} | ||
if info.serviceName != "" { | ||
to.PutStr(keyWithPrefix(semconv.AttributeServiceName), info.serviceName) | ||
} | ||
if info.serviceNamespace != "" { | ||
to.PutStr(keyWithPrefix(semconv.AttributeServiceNamespace), info.serviceNamespace) | ||
} | ||
} | ||
|
||
func keyWithPrefix(key string) string { | ||
return prefix + "." + key | ||
} |
94 changes: 94 additions & 0 deletions
94
connector/signaltometricsconnector/internal/model/collectorinstanceinfo_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package model | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/component/componenttest" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
semconv "go.opentelemetry.io/collector/semconv/v1.26.0" | ||
) | ||
|
||
func TestCollectorInstanceInfo(t *testing.T) { | ||
for _, tc := range []struct { | ||
name string | ||
input component.TelemetrySettings | ||
expected pcommon.Map | ||
}{ | ||
{ | ||
name: "empty", | ||
input: componenttest.NewNopTelemetrySettings(), | ||
expected: pcommon.NewMap(), | ||
}, | ||
{ | ||
name: "with_service_instance_id", | ||
input: func() component.TelemetrySettings { | ||
ts := componenttest.NewNopTelemetrySettings() | ||
ts.Resource.Attributes().PutStr(semconv.AttributeServiceInstanceID, "627cc493-f310-47de-96bd-71410b7dec09") | ||
return ts | ||
}(), | ||
expected: func() pcommon.Map { | ||
m := pcommon.NewMap() | ||
m.PutStr( | ||
"signaltometrics."+semconv.AttributeServiceInstanceID, | ||
"627cc493-f310-47de-96bd-71410b7dec09", | ||
) | ||
return m | ||
}(), | ||
}, | ||
{ | ||
name: "with_all_values", | ||
input: func() component.TelemetrySettings { | ||
ts := componenttest.NewNopTelemetrySettings() | ||
ts.Resource.Attributes().PutStr(semconv.AttributeServiceInstanceID, "627cc493-f310-47de-96bd-71410b7dec09") | ||
ts.Resource.Attributes().PutStr(semconv.AttributeServiceName, "signaltometrics") | ||
ts.Resource.Attributes().PutStr(semconv.AttributeServiceNamespace, "test") | ||
return ts | ||
}(), | ||
expected: func() pcommon.Map { | ||
m := pcommon.NewMap() | ||
m.PutStr( | ||
"signaltometrics."+semconv.AttributeServiceInstanceID, | ||
"627cc493-f310-47de-96bd-71410b7dec09", | ||
) | ||
m.PutStr( | ||
"signaltometrics."+semconv.AttributeServiceName, | ||
"signaltometrics", | ||
) | ||
m.PutStr( | ||
"signaltometrics."+semconv.AttributeServiceNamespace, | ||
"test", | ||
) | ||
return m | ||
}(), | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ci := NewCollectorInstanceInfo(tc.input) | ||
require.NotNil(t, ci) | ||
|
||
actual := pcommon.NewMap() | ||
ci.Copy(actual) | ||
assert.Equal(t, ci.Size(), actual.Len()) | ||
assertMapEquality(t, tc.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
func assertMapEquality(t *testing.T, expected, actual pcommon.Map) bool { | ||
t.Helper() | ||
|
||
expectedRaw := expected.AsRaw() | ||
actualRaw := actual.AsRaw() | ||
return assert.True( | ||
t, reflect.DeepEqual(expectedRaw, actualRaw), | ||
"attributes don't match expected: %v, actual: %v", | ||
expectedRaw, actualRaw, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.