-
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.
Merge branch 'main' into renovate/all-opentelemetry-collector-contrib…
…-packages
- Loading branch information
Showing
15 changed files
with
353 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: signaltometrics | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Adds resource attributes based on telemetry settings to the connector to ensure single writer | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35930] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
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
Oops, something went wrong.