Skip to content

Commit

Permalink
feat: run python agent alogside NR (#1886)
Browse files Browse the repository at this point in the history
  • Loading branch information
tamirdavid1 authored Dec 1, 2024
1 parent 32c9aed commit fa73020
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
7 changes: 5 additions & 2 deletions common/envOverwrite/overwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) == "" {
Expand Down
6 changes: 3 additions & 3 deletions instrumentor/instrumentation/instrumentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 containerHaveOtherAgent != nil {
// 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 {
Expand Down
12 changes: 12 additions & 0 deletions opampserver/pkg/connection/conncache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fa73020

Please sign in to comment.