Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrumentations support select #2778

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/instrumentation-support-select.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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. collector, target allocator, auto-instrumentation, opamp, github action)
component: auto-instrumentation

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Instrumentations support select
crossoverJie marked this conversation as resolved.
Show resolved Hide resolved

# One or more tracking issues related to the change
issues: [2744]

# (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:
4 changes: 4 additions & 0 deletions apis/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import (

// InstrumentationSpec defines the desired state of OpenTelemetry SDK and instrumentation.
type InstrumentationSpec struct {
// Selector is the selector label of injected Object
// +optional
crossoverJie marked this conversation as resolved.
Show resolved Hide resolved
Selector map[string]string `json:"selector,omitempty"`
crossoverJie marked this conversation as resolved.
Show resolved Hide resolved

// Exporter defines exporter configuration.
// +optional
Exporter `json:"exporter,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions bundle/manifests/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,10 @@ spec:
- xray
type: string
type: object
selector:
additionalProperties:
type: string
type: object
type: object
status:
type: object
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,10 @@ spec:
- xray
type: string
type: object
selector:
additionalProperties:
type: string
type: object
type: object
status:
type: object
Expand Down
7 changes: 7 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ Resource Types:
<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>selector</b></td>
<td>map[string]string</td>
<td>
<br/>
</td>
<td>false</td>
</tr></tbody>
</table>

Expand Down
29 changes: 21 additions & 8 deletions pkg/instrumentation/podmutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ import (
)

var (
errMultipleInstancesPossible = errors.New("multiple OpenTelemetry Instrumentation instances available, cannot determine which one to select")
errNoInstancesAvailable = errors.New("no OpenTelemetry Instrumentation instances available")
errNoInstancesAvailable = errors.New("no OpenTelemetry Instrumentation instances available")
)

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

if strings.EqualFold(instValue, "true") {
return pm.selectInstrumentationInstanceFromNamespace(ctx, ns)
return pm.selectInstrumentationInstanceFromNamespace(ctx, ns, pod)
}

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

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

switch s := len(otelInsts.Items); {
// selector
var availableInstrument []v1alpha1.Instrumentation
for _, ins := range otelInsts.Items {
isMatch := true
if len(ins.Spec.Selector) != 0 {
for k, v := range ins.Spec.Selector {
if !strings.EqualFold(v, pod.Labels[k]) {
isMatch = false
}
}
}
if isMatch {
availableInstrument = append(availableInstrument, ins)
}
}

switch s := len(availableInstrument); {
swiatekm marked this conversation as resolved.
Show resolved Hide resolved
case s == 0:
return nil, errNoInstancesAvailable
case s > 1:
return nil, errMultipleInstancesPossible
swiatekm marked this conversation as resolved.
Show resolved Hide resolved
default:
return &otelInsts.Items[0], nil
return &availableInstrument[len(availableInstrument)-1], nil
}
}
Loading