Skip to content

Commit 1a5d464

Browse files
authored
Not ignore the container-names annotation when the multi-instrumentation is enabled (#3213)
* Not ignore the container-names annotation when the multi-instrumentation is enabled Signed-off-by: Israel Blancas <[email protected]> * Fixes Signed-off-by: Israel Blancas <[email protected]> * Fix unit tests Signed-off-by: Israel Blancas <[email protected]> * Apply changes requested in code review Signed-off-by: Israel Blancas <[email protected]> --------- Signed-off-by: Israel Blancas <[email protected]>
1 parent 1dd161e commit 1a5d464

File tree

12 files changed

+513
-985
lines changed

12 files changed

+513
-985
lines changed

.chloggen/feature_3090.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: 'enhancement'
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: auto-instrumentation
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: "Not ignore the `instrumentation.opentelemetry.io/container-names` annotation when the multi-instrumentation is enabled"
9+
10+
# One or more tracking issues related to the change
11+
issues: [3090]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

pkg/instrumentation/helper.go

+35-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ package instrumentation
1616

1717
import (
1818
"fmt"
19+
"regexp"
1920
"sort"
2021
"strings"
2122

2223
corev1 "k8s.io/api/core/v1"
2324
"k8s.io/apimachinery/pkg/api/resource"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2426
"k8s.io/utils/strings/slices"
2527

2628
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
@@ -77,15 +79,9 @@ func isAutoInstrumentationInjected(pod corev1.Pod) bool {
7779

7880
// Look for duplicates in the provided containers.
7981
func findDuplicatedContainers(ctrs []string) error {
80-
// Merge is needed because of multiple containers can be provided for single instrumentation.
81-
mergedContainers := strings.Join(ctrs, ",")
82-
83-
// Split all containers.
84-
splitContainers := strings.Split(mergedContainers, ",")
85-
8682
countMap := make(map[string]int)
8783
var duplicates []string
88-
for _, str := range splitContainers {
84+
for _, str := range ctrs {
8985
countMap[str]++
9086
}
9187

@@ -111,7 +107,7 @@ func findDuplicatedContainers(ctrs []string) error {
111107

112108
// Return positive for instrumentation with defined containers.
113109
func isInstrWithContainers(inst instrumentationWithContainers) int {
114-
if inst.Containers != "" {
110+
if len(inst.Containers) > 0 {
115111
return 1
116112
}
117113

@@ -120,7 +116,7 @@ func isInstrWithContainers(inst instrumentationWithContainers) int {
120116

121117
// Return positive for instrumentation without defined containers.
122118
func isInstrWithoutContainers(inst instrumentationWithContainers) int {
123-
if inst.Containers == "" {
119+
if len(inst.Containers) == 0 {
124120
return 1
125121
}
126122

@@ -133,3 +129,33 @@ func volumeSize(quantity *resource.Quantity) *resource.Quantity {
133129
}
134130
return quantity
135131
}
132+
133+
func isValidContainersAnnotation(containersAnnotation string) error {
134+
if containersAnnotation == "" {
135+
return nil
136+
}
137+
138+
matched, err := regexp.MatchString("^[a-zA-Z0-9-,]+$", containersAnnotation)
139+
if err != nil {
140+
return fmt.Errorf("error while checking for instrumentation container annotations %w", err)
141+
}
142+
if !matched {
143+
return fmt.Errorf("not valid characters included in the instrumentation container annotation %s", containersAnnotation)
144+
}
145+
return nil
146+
}
147+
148+
// setContainersFromAnnotation sets the containers associated to one intrumentation based on the content of the provided annotation.
149+
func setContainersFromAnnotation(inst *instrumentationWithContainers, annotation string, ns metav1.ObjectMeta, pod metav1.ObjectMeta) error {
150+
annotationValue := annotationValue(ns, pod, annotation)
151+
if annotationValue == "" {
152+
return nil
153+
}
154+
155+
if err := isValidContainersAnnotation(annotationValue); err != nil {
156+
return err
157+
}
158+
languageContainers := strings.Split(annotationValue, ",")
159+
inst.Containers = append(inst.Containers, languageContainers...)
160+
return nil
161+
}

pkg/instrumentation/helper_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,12 @@ func TestDuplicatedContainers(t *testing.T) {
170170
}{
171171
{
172172
name: "No duplicates",
173-
containers: []string{"app1,app2", "app3", "app4,app5"},
173+
containers: []string{"app1", "app2", "app3", "app4", "app5"},
174174
expectedDuplicates: nil,
175175
},
176176
{
177177
name: "Duplicates in containers",
178-
containers: []string{"app1,app2", "app1", "app1,app3,app4", "app4"},
178+
containers: []string{"app1", "app2", "app1", "app1", "app3", "app4", "app4"},
179179
expectedDuplicates: fmt.Errorf("duplicated container names detected: [app1 app4]"),
180180
},
181181
}
@@ -196,12 +196,12 @@ func TestInstrWithContainers(t *testing.T) {
196196
}{
197197
{
198198
name: "No containers",
199-
containers: instrumentationWithContainers{Containers: ""},
199+
containers: instrumentationWithContainers{Containers: []string{}},
200200
expectedResult: 0,
201201
},
202202
{
203203
name: "With containers",
204-
containers: instrumentationWithContainers{Containers: "ct1"},
204+
containers: instrumentationWithContainers{Containers: []string{"ct1"}},
205205
expectedResult: 1,
206206
},
207207
}
@@ -222,12 +222,12 @@ func TestInstrWithoutContainers(t *testing.T) {
222222
}{
223223
{
224224
name: "No containers",
225-
containers: instrumentationWithContainers{Containers: ""},
225+
containers: instrumentationWithContainers{Containers: []string{}},
226226
expectedResult: 1,
227227
},
228228
{
229229
name: "With containers",
230-
containers: instrumentationWithContainers{Containers: "ct1"},
230+
containers: instrumentationWithContainers{Containers: []string{"ct1"}},
231231
expectedResult: 0,
232232
},
233233
}

0 commit comments

Comments
 (0)