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

feat: Create APM package #15

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/instrumentation/dotnet.go → src/apm/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package instrumentation
package apm

import (
"errors"
Expand All @@ -37,7 +37,7 @@ const (
dotnetInitContainerName = initContainerName + "-dotnet"
)

func injectDotNetSDK(dotNetSpec v1alpha1.DotNet, pod corev1.Pod, index int) (corev1.Pod, error) {
func InjectDotNetSDK(dotNetSpec v1alpha1.DotNet, pod corev1.Pod, index int) (corev1.Pod, error) {

// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]
Expand Down
4 changes: 2 additions & 2 deletions src/instrumentation/golang.go → src/apm/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package instrumentation
package apm

import (
"fmt"
Expand All @@ -30,7 +30,7 @@ const (
kernelDebugVolumePath = "/sys/kernel/debug"
)

func injectGoSDK(goSpec v1alpha1.Go, pod corev1.Pod) (corev1.Pod, error) {
func InjectGoSDK(goSpec v1alpha1.Go, pod corev1.Pod) (corev1.Pod, error) {
// skip instrumentation if share process namespaces is explicitly disabled
if pod.Spec.ShareProcessNamespace != nil && !*pod.Spec.ShareProcessNamespace {
return pod, fmt.Errorf("shared process namespace has been explicitly disabled")
Expand Down
79 changes: 79 additions & 0 deletions src/apm/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
Copyright 2024.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package apm

import (
"fmt"

corev1 "k8s.io/api/core/v1"
)

const (
volumeName = "newrelic-instrumentation"
initContainerName = "newrelic-instrumentation"
sideCarName = "opentelemetry-auto-instrumentation"

// indicates whether newrelic agents should be injected or not.
// Possible values are "true", "false" or "<Instrumentation>" name.
annotationInjectJava = "instrumentation.newrelic.com/inject-java"
annotationInjectJavaContainersName = "instrumentation.newrelic.com/java-container-names"
annotationInjectNodeJS = "instrumentation.newrelic.com/inject-nodejs"
annotationInjectNodeJSContainersName = "instrumentation.newrelic.com/nodejs-container-names"
annotationInjectPython = "instrumentation.newrelic.com/inject-python"
annotationInjectPythonContainersName = "instrumentation.newrelic.com/python-container-names"
annotationInjectDotNet = "instrumentation.newrelic.com/inject-dotnet"
annotationInjectDotnetContainersName = "instrumentation.newrelic.com/dotnet-container-names"
annotationInjectPhp = "instrumentation.newrelic.com/inject-php"
annotationInjectPhpContainersName = "instrumentation.newrelic.com/php-container-names"
annotationPhpExecCmd = "instrumentation.newrelic.com/php-exec-command"
annotationInjectContainerName = "instrumentation.newrelic.com/container-name"
annotationInjectGo = "instrumentation.opentelemetry.io/inject-go"
annotationGoExecPath = "instrumentation.opentelemetry.io/otel-go-auto-target-exe"
annotationInjectGoContainerName = "instrumentation.opentelemetry.io/go-container-name"
)

// Calculate if we already inject InitContainers.
func isInitContainerMissing(pod corev1.Pod) bool {
for _, initContainer := range pod.Spec.InitContainers {
if initContainer.Name == initContainerName {
return false
}
}
return true
}

func getIndexOfEnv(envs []corev1.EnvVar, name string) int {
for i := range envs {
if envs[i].Name == name {
return i
}
}
return -1
}

func validateContainerEnv(envs []corev1.EnvVar, envsToBeValidated ...string) error {
for _, envToBeValidated := range envsToBeValidated {
for _, containerEnv := range envs {
if containerEnv.Name == envToBeValidated {
if containerEnv.ValueFrom != nil {
return fmt.Errorf("the container defines env var value via ValueFrom, envVar: %s", containerEnv.Name)
}
break
}
}
}
return nil
}
4 changes: 2 additions & 2 deletions src/instrumentation/javaagent.go → src/apm/javaagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package instrumentation
package apm

import (
corev1 "k8s.io/api/core/v1"
Expand All @@ -28,7 +28,7 @@ const (
javaVolumeName = volumeName + "-java"
)

func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.Pod, error) {
func InjectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.Pod, error) {
// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]

Expand Down
4 changes: 2 additions & 2 deletions src/instrumentation/nodejs.go → src/apm/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package instrumentation
package apm

import (
corev1 "k8s.io/api/core/v1"
Expand All @@ -28,7 +28,7 @@ const (
nodejsVolumeName = volumeName + "-nodejs"
)

func injectNodeJSSDK(nodeJSSpec v1alpha1.NodeJS, pod corev1.Pod, index int) (corev1.Pod, error) {
func InjectNodeJSSDK(nodeJSSpec v1alpha1.NodeJS, pod corev1.Pod, index int) (corev1.Pod, error) {
// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]

Expand Down
4 changes: 2 additions & 2 deletions src/instrumentation/php.go → src/apm/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package instrumentation
package apm

import (
"fmt"
Expand All @@ -33,7 +33,7 @@ const (
phpInstallArgument = "/newrelic-instrumentation/newrelic-install install && sed -i -e \"s/PHP Application/$NEW_RELIC_APP_NAME/g; s/REPLACE_WITH_REAL_KEY/$NEW_RELIC_LICENSE_KEY/g\" /usr/local/etc/php/conf.d/newrelic.ini"
)

func injectPhpagent(phpSpec v1alpha1.Php, pod corev1.Pod, index int) (corev1.Pod, error) {
func InjectPhpagent(phpSpec v1alpha1.Php, pod corev1.Pod, index int) (corev1.Pod, error) {
// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]

Expand Down
4 changes: 2 additions & 2 deletions src/instrumentation/python.go → src/apm/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package instrumentation
package apm

import (
"fmt"
Expand All @@ -31,7 +31,7 @@ const (
pythonInitContainerName = initContainerName + "-python"
)

func injectPythonSDK(pythonSpec v1alpha1.Python, pod corev1.Pod, index int) (corev1.Pod, error) {
func InjectPythonSDK(pythonSpec v1alpha1.Python, pod corev1.Pod, index int) (corev1.Pod, error) {
// caller checks if there is at least one container.
container := &pod.Spec.Containers[index]

Expand Down
30 changes: 0 additions & 30 deletions src/instrumentation/helper.go

This file was deleted.

41 changes: 7 additions & 34 deletions src/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/newrelic/k8s-agents-operator/src/api/v1alpha1"
apm "github.com/newrelic/k8s-agents-operator/src/apm"
"github.com/newrelic/k8s-agents-operator/src/constants"
)

const (
volumeName = "newrelic-instrumentation"
initContainerName = "newrelic-instrumentation"
sideCarName = "opentelemetry-auto-instrumentation"
)

type sdkInjector struct {
client client.Client
logger logr.Logger
Expand All @@ -69,7 +64,7 @@ func (i *sdkInjector) inject(ctx context.Context, insts languageInstrumentations
newrelic := *insts.Java
var err error
i.logger.V(1).Info("injecting Java instrumentation into pod", "newrelic-namespace", newrelic.Namespace, "newrelic-name", newrelic.Name)
pod, err = injectJavaagent(newrelic.Spec.Java, pod, index)
pod, err = apm.InjectJavaagent(newrelic.Spec.Java, pod, index)
if err != nil {
i.logger.Info("Skipping Java agent injection", "reason", err.Error(), "container", pod.Spec.Containers[index].Name)
} else {
Expand All @@ -80,7 +75,7 @@ func (i *sdkInjector) inject(ctx context.Context, insts languageInstrumentations
newrelic := *insts.NodeJS
var err error
i.logger.V(1).Info("injecting NodeJS instrumentation into pod", "newrelic-namespace", newrelic.Namespace, "newrelic-name", newrelic.Name)
pod, err = injectNodeJSSDK(newrelic.Spec.NodeJS, pod, index)
pod, err = apm.InjectNodeJSSDK(newrelic.Spec.NodeJS, pod, index)
if err != nil {
i.logger.Info("Skipping NodeJS agent injection", "reason", err.Error(), "container", pod.Spec.Containers[index].Name)
} else {
Expand All @@ -91,7 +86,7 @@ func (i *sdkInjector) inject(ctx context.Context, insts languageInstrumentations
newrelic := *insts.Python
var err error
i.logger.V(1).Info("injecting Python instrumentation into pod", "newrelic-namespace", newrelic.Namespace, "newrelic-name", newrelic.Name)
pod, err = injectPythonSDK(newrelic.Spec.Python, pod, index)
pod, err = apm.InjectPythonSDK(newrelic.Spec.Python, pod, index)
if err != nil {
i.logger.Info("Skipping Python agent injection", "reason", err.Error(), "container", pod.Spec.Containers[index].Name)
} else {
Expand All @@ -102,7 +97,7 @@ func (i *sdkInjector) inject(ctx context.Context, insts languageInstrumentations
newrelic := *insts.DotNet
var err error
i.logger.V(1).Info("injecting DotNet instrumentation into pod", "newrelic-namespace", newrelic.Namespace, "newrelic-name", newrelic.Name)
pod, err = injectDotNetSDK(newrelic.Spec.DotNet, pod, index)
pod, err = apm.InjectDotNetSDK(newrelic.Spec.DotNet, pod, index)
if err != nil {
i.logger.Info("Skipping DotNet agent injection", "reason", err.Error(), "container", pod.Spec.Containers[index].Name)
} else {
Expand All @@ -113,15 +108,14 @@ func (i *sdkInjector) inject(ctx context.Context, insts languageInstrumentations
newrelic := *insts.Php
var err error
i.logger.V(1).Info("injecting Php instrumentation into pod", "newrelic-namespace", newrelic.Namespace, "newrelic-name", newrelic.Name)
pod, err = injectPhpagent(newrelic.Spec.Php, pod, index)
pod, err = apm.InjectPhpagent(newrelic.Spec.Php, pod, index)
if err != nil {
i.logger.Info("Skipping Php agent injection", "reason", err.Error(), "container", pod.Spec.Containers[index].Name)
} else {
pod = i.injectNewrelicConfig(ctx, newrelic, ns, pod, index)
}
}
if insts.Go != nil {
origPod := pod
newrelic := *insts.Go
var err error
i.logger.V(1).Info("injecting Go instrumentation into pod", "newrelic-namespace", newrelic.Namespace, "newrelic-name", newrelic.Name)
Expand All @@ -130,20 +124,13 @@ func (i *sdkInjector) inject(ctx context.Context, insts languageInstrumentations
index := getContainerIndex(goContainers, pod)

// Go instrumentation supports only single container instrumentation.
pod, err = injectGoSDK(newrelic.Spec.Go, pod)
pod, err = apm.InjectGoSDK(newrelic.Spec.Go, pod)
if err != nil {
i.logger.Info("Skipping Go SDK injection", "reason", err.Error(), "container", pod.Spec.Containers[index].Name)
} else {
// Common env vars and config need to be applied to the agent container.
pod = i.injectCommonEnvVar(newrelic, pod, len(pod.Spec.Containers)-1)
pod = i.injectCommonSDKConfig(ctx, newrelic, ns, pod, len(pod.Spec.Containers)-1, 0)

// Ensure that after all the env var coalescing we have a value for OTEL_GO_AUTO_TARGET_EXE
idx := getIndexOfEnv(pod.Spec.Containers[len(pod.Spec.Containers)-1].Env, envOtelTargetExe)
if idx == -1 {
i.logger.Info("Skipping Go SDK injection", "reason", "OTEL_GO_AUTO_TARGET_EXE not set", "container", pod.Spec.Containers[index].Name)
pod = origPod
}
}
}
return pod
Expand Down Expand Up @@ -507,17 +494,3 @@ func moveEnvToListEnd(envs []corev1.EnvVar, idx int) []corev1.EnvVar {

return envs
}

func validateContainerEnv(envs []corev1.EnvVar, envsToBeValidated ...string) error {
for _, envToBeValidated := range envsToBeValidated {
for _, containerEnv := range envs {
if containerEnv.Name == envToBeValidated {
if containerEnv.ValueFrom != nil {
return fmt.Errorf("the container defines env var value via ValueFrom, envVar: %s", containerEnv.Name)
}
break
}
}
}
return nil
}
Loading