From 2e8187cf5e4bc62608455a7f6f3e74299d933600 Mon Sep 17 00:00:00 2001 From: tamirdavid1 Date: Fri, 29 Nov 2024 11:54:33 +0200 Subject: [PATCH] feat: run python agent alogside NR --- common/envOverwrite/overwriter.go | 7 +++++-- instrumentor/instrumentation/instrumentation.go | 8 ++++---- opampserver/pkg/connection/conncache.go | 12 ++++++++++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/common/envOverwrite/overwriter.go b/common/envOverwrite/overwriter.go index 14e620631..66f7323a1 100644 --- a/common/envOverwrite/overwriter.go +++ b/common/envOverwrite/overwriter.go @@ -112,10 +112,13 @@ func GetPatchedEnvValue(envName string, observedValue string, currentSdk *common // temporary fix clean up observed value from the known webhook injected value parts := strings.Split(observedValue, envMetadata.delim) - ignoreEnvValue := "-javaagent:/opt/sre-agent/sre-agent.jar" + const ( + ignoredJavaAgentValue = "-javaagent:/opt/sre-agent/sre-agent.jar" + ignoredNRPythonPathAddition = "newrelic/bootstrap" + ) newValues := []string{} for _, part := range parts { - if part == ignoreEnvValue { + if part == ignoredJavaAgentValue || strings.Contains(part, ignoredNRPythonPathAddition) { continue } if strings.TrimSpace(part) == "" { diff --git a/instrumentor/instrumentation/instrumentation.go b/instrumentor/instrumentation/instrumentation.go index d7a9d598c..167dd7f04 100644 --- a/instrumentor/instrumentation/instrumentation.go +++ b/instrumentor/instrumentation/instrumentation.go @@ -42,15 +42,15 @@ func ApplyInstrumentationDevicesToPodTemplate(original *corev1.PodTemplateSpec, containerLanguage := getLanguageOfContainer(runtimeDetails, container.Name) containerHaveOtherAgent := getContainerOtherAgents(runtimeDetails, container.Name) - // In case there is another agent in the container, we should not apply the instrumentation device. - if containerLanguage == common.PythonProgrammingLanguage && containerHaveOtherAgent != nil { - logger.Info("Python container has other agent, skip applying instrumentation device", "agent", containerHaveOtherAgent.Name, "container", container.Name) + // In case there is another agent in the container, we should not apply the instrumentation device '*'. + // '*' - In Python, we can run it with New Relic (the only one we detect), but not in other languages. + if containerHaveOtherAgent != nil && containerLanguage != common.PythonProgrammingLanguage { + logger.Info("Container is running other agent, skip applying instrumentation device", "agent", containerHaveOtherAgent.Name, "container", container.Name) // Not actually modifying the container, but we need to append it to the list. modifiedContainers = append(modifiedContainers, container) deviceSkippedDueToOtherAgent = true continue - } // handle containers with unknown language or ignored language if containerLanguage == common.UnknownProgrammingLanguage || containerLanguage == common.IgnoredProgrammingLanguage || containerLanguage == common.NginxProgrammingLanguage { diff --git a/opampserver/pkg/connection/conncache.go b/opampserver/pkg/connection/conncache.go index be3a44189..e946aa86f 100644 --- a/opampserver/pkg/connection/conncache.go +++ b/opampserver/pkg/connection/conncache.go @@ -55,9 +55,21 @@ func (c *ConnectionsCache) AddConnection(instanceUid string, conn *ConnectionInf connCopy := *conn c.mux.Lock() defer c.mux.Unlock() + c.RemoveMatchingConnections(conn.Pod.Name, conn.Pid) c.liveConnections[instanceUid] = &connCopy } +// RemoveMatchingConnections removes all connections that match the given podName and pid. +// This ensures that outdated connections are cleaned up, such as when a new process +// is spawned within the same pod (e.g., using os.execl in Python). +func (c *ConnectionsCache) RemoveMatchingConnections(podName string, pid int64) { + for k, v := range c.liveConnections { + if v.Pod.Name == podName && v.Pid == pid { + delete(c.liveConnections, k) + } + } +} + func (c *ConnectionsCache) RemoveConnection(instanceUid string) { c.mux.Lock() defer c.mux.Unlock()