Skip to content

Commit 377b51f

Browse files
committed
Instrumentations support select
1 parent 78e7c2f commit 377b51f

File tree

7 files changed

+63
-8
lines changed

7 files changed

+63
-8
lines changed
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: Instrumentations support select
9+
10+
# One or more tracking issues related to the change
11+
issues: [2744]
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:

apis/v1alpha1/instrumentation_types.go

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import (
2222

2323
// InstrumentationSpec defines the desired state of OpenTelemetry SDK and instrumentation.
2424
type InstrumentationSpec struct {
25+
// Selector is the selector label of injected Object
26+
// +optional
27+
Selector map[string]string `json:"selector,omitempty"`
28+
2529
// Exporter defines exporter configuration.
2630
// +optional
2731
Exporter `json:"exporter,omitempty"`

apis/v1alpha1/zz_generated.deepcopy.go

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/opentelemetry.io_instrumentations.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,10 @@ spec:
10271027
- xray
10281028
type: string
10291029
type: object
1030+
selector:
1031+
additionalProperties:
1032+
type: string
1033+
type: object
10301034
type: object
10311035
status:
10321036
type: object

config/crd/bases/opentelemetry.io_instrumentations.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,10 @@ spec:
10251025
- xray
10261026
type: string
10271027
type: object
1028+
selector:
1029+
additionalProperties:
1030+
type: string
1031+
type: object
10281032
type: object
10291033
status:
10301034
type: object

docs/api.md

+7
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ Resource Types:
172172
<br/>
173173
</td>
174174
<td>false</td>
175+
</tr><tr>
176+
<td><b>selector</b></td>
177+
<td>map[string]string</td>
178+
<td>
179+
<br/>
180+
</td>
181+
<td>false</td>
175182
</tr></tbody>
176183
</table>
177184

pkg/instrumentation/podmutator.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ import (
3333
)
3434

3535
var (
36-
errMultipleInstancesPossible = errors.New("multiple OpenTelemetry Instrumentation instances available, cannot determine which one to select")
37-
errNoInstancesAvailable = errors.New("no OpenTelemetry Instrumentation instances available")
36+
errNoInstancesAvailable = errors.New("no OpenTelemetry Instrumentation instances available")
3837
)
3938

4039
type instPodMutator struct {
@@ -373,7 +372,7 @@ func (pm *instPodMutator) getInstrumentationInstance(ctx context.Context, ns cor
373372
}
374373

375374
if strings.EqualFold(instValue, "true") {
376-
return pm.selectInstrumentationInstanceFromNamespace(ctx, ns)
375+
return pm.selectInstrumentationInstanceFromNamespace(ctx, ns, pod)
377376
}
378377

379378
var instNamespacedName types.NamespacedName
@@ -392,18 +391,32 @@ func (pm *instPodMutator) getInstrumentationInstance(ctx context.Context, ns cor
392391
return otelInst, nil
393392
}
394393

395-
func (pm *instPodMutator) selectInstrumentationInstanceFromNamespace(ctx context.Context, ns corev1.Namespace) (*v1alpha1.Instrumentation, error) {
394+
func (pm *instPodMutator) selectInstrumentationInstanceFromNamespace(ctx context.Context, ns corev1.Namespace, pod corev1.Pod) (*v1alpha1.Instrumentation, error) {
396395
var otelInsts v1alpha1.InstrumentationList
397396
if err := pm.Client.List(ctx, &otelInsts, client.InNamespace(ns.Name)); err != nil {
398397
return nil, err
399398
}
400399

401-
switch s := len(otelInsts.Items); {
400+
// selector
401+
var availableInstrument []v1alpha1.Instrumentation
402+
for _, ins := range otelInsts.Items {
403+
isMatch := true
404+
if len(ins.Spec.Selector) != 0 {
405+
for k, v := range ins.Spec.Selector {
406+
if !strings.EqualFold(v, pod.Labels[k]) {
407+
isMatch = false
408+
}
409+
}
410+
}
411+
if isMatch {
412+
availableInstrument = append(availableInstrument, ins)
413+
}
414+
}
415+
416+
switch s := len(availableInstrument); {
402417
case s == 0:
403418
return nil, errNoInstancesAvailable
404-
case s > 1:
405-
return nil, errMultipleInstancesPossible
406419
default:
407-
return &otelInsts.Items[0], nil
420+
return &availableInstrument[len(availableInstrument)-1], nil
408421
}
409422
}

0 commit comments

Comments
 (0)