Skip to content

Commit

Permalink
ELASTIC_APM_ACTIVATION_METHOD=K8S_ATTACH, prefer `ELASTIC_APM_SERVE…
Browse files Browse the repository at this point in the history
…R_URL`; integration.yml workflow tweak (#87)

* test: drop redundant kind cluster creation in integration.yml workflow

* use ELASTIC_APM_SERVER_URL instead of the plural; the plural works with java, but not the Node.js or other APM agents

* set ELASTIC_APM_ACTIVATION_METHOD=K8S_ATTACH in code rather than values.yaml

This means that custom 'webhookConfig.agent.*' names do not need to repeat this
envvar. It *does* mean that newer Node.js and .NET APM agents that use the
newer value (used to be 'K8S') will be required for the 'activation_method' to
get set correctly.

Closes: #91

* fix tests

* improve the workflow step name
  • Loading branch information
trentm authored Jan 12, 2024
1 parent 22750b9 commit 7e30998
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 24 deletions.
17 changes: 7 additions & 10 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Agents Integration Testing

on:
on:
workflow_dispatch:
push:
branches:
Expand All @@ -18,10 +18,10 @@ on:
paths-ignore:
- '**.md'
- '**.asciidoc'

env:
AGENT_TESTS: java nodejs

jobs:
integration-test:
runs-on: ubuntu-latest
Expand All @@ -31,15 +31,12 @@ jobs:
with:
fetch-depth: 0

- name: Create kind cluster
uses: helm/[email protected]

- name: Create Local Registry
run:
- name: Create Kind cluster and local Docker registry
run:
sh test/mock/kind-with-registry.sh

- name: Create Mock APM server image
run:
run:
cd test/mock;
docker build -t mock-apm-server .;
docker tag mock-apm-server localhost:5001/registry/mock-apm-server;
Expand Down Expand Up @@ -72,7 +69,7 @@ jobs:
version: v3.11.2

- name: Install webhook
run:
run:
cd charts;
helm install webhook-chart apm-attacher/ --namespace=elastic-apm --create-namespace --values ../test/mock/test_values.yaml;
sleep 5;
Expand Down
3 changes: 0 additions & 3 deletions charts/apm-attacher/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ webhookConfig:
artifact: "/usr/agent/elastic-apm-agent.jar"
environment:
JAVA_TOOL_OPTIONS: "-javaagent:/elastic/apm/agent/elastic-apm-agent.jar"
ELASTIC_APM_ACTIVATION_METHOD: "K8S_ATTACH"
nodejs:
image: docker.elastic.co/observability/apm-agent-nodejs:latest
artifact: "/opt/nodejs/node_modules/elastic-apm-node"
environment:
NODE_OPTIONS: "-r /elastic/apm/agent/elastic-apm-node/start"
ELASTIC_APM_ACTIVATION_METHOD: "K8S"
dotnet:
image: docker.elastic.co/observability/apm-agent-dotnet:latest
artifact: "/usr/agent/apm-dotnet-agent"
Expand All @@ -46,4 +44,3 @@ webhookConfig:
CORECLR_PROFILER_PATH: "/usr/agent/apm-dotnet-agent/libelastic_apm_profiler.so"
ELASTIC_APM_PROFILER_HOME: "/usr/agent/apm-dotnet-agent"
ELASTIC_APM_PROFILER_INTEGRATIONS: "/usr/agent/apm-dotnet-agent/integrations.yml"
ELASTIC_APM_ACTIVATION_METHOD: "K8S"
8 changes: 4 additions & 4 deletions docs/apm-k8s-attacher.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ agents:
image: docker.elastic.co/observability/apm-agent-java:latest
artifact: "/usr/agent/elastic-apm-agent.jar"
environment:
ELASTIC_APM_SERVER_URLS: "http://192.168.1.10:8200"
ELASTIC_APM_SERVER_URL: "http://192.168.1.10:8200"
ELASTIC_APM_ENVIRONMENT: "dev"
ELASTIC_APM_LOG_LEVEL: "debug"
ELASTIC_APM_PROFILING_INFERRED_SPANS_ENABLED: "true"
Expand All @@ -227,7 +227,7 @@ agents:
image: docker.elastic.co/observability/apm-agent-java:1.44.0 <1>
artifact: "/usr/agent/elastic-apm-agent.jar"
environment:
ELASTIC_APM_SERVER_URLS: "http://192.168.1.11:8200"
ELASTIC_APM_SERVER_URL: "http://192.168.1.11:8200"
ELASTIC_APM_ENVIRONMENT: "prod"
ELASTIC_APM_LOG_LEVEL: "info"
ELASTIC_APM_PROFILING_INFERRED_SPANS_ENABLED: "true"
Expand All @@ -237,7 +237,7 @@ agents:
artifact: "/opt/nodejs/node_modules/elastic-apm-node"
environment:
NODE_OPTIONS: "-r /elastic/apm/agent/elastic-apm-node/start"
ELASTIC_APM_SERVER_URLS: "http://192.168.1.11:8200"
ELASTIC_APM_SERVER_URL: "http://192.168.1.11:8200"
ELASTIC_APM_SERVICE_NAME: "petclinic"
ELASTIC_APM_LOG_LEVEL: "info"
----
Expand All @@ -255,5 +255,5 @@ your image sets will be ignored in favour of the value set here or in the https:

You may not see data flow into the {stack} right away; that's normal.
The addition of a pod annotation does not trigger an automatic restart.
Therefore, existing pods will will not be effected by the APM Attacher. Only new pods--as they are created via the natural lifecycle of a Kubernetes deployment--will be instrumented.
Therefore, existing pods will will not be affected by the APM Attacher. Only new pods--as they are created via the natural lifecycle of a Kubernetes deployment--will be instrumented.
Restarting pods you'd like instrumented manually will speed up this process, but that workflow is too specific to individual deployments to make any recommendations.
2 changes: 1 addition & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestYAMLParse(t *testing.T) {
java := cfg.Agents["java"]
assert.Equal(t, java.Image, "docker.com/elastic/agent-java:1.2.3")
assert.Len(t, java.Environment, 5)
assert.Contains(t, java.Environment, "ELASTIC_APM_SERVER_URLS")
assert.Contains(t, java.Environment, "ELASTIC_APM_SERVER_URL")
assert.Contains(t, java.Environment, "ELASTIC_APM_SERVICE_NAME")
assert.Contains(t, java.Environment, "ELASTIC_APM_ENVIRONMENT")
assert.Contains(t, java.Environment, "ELASTIC_APM_LOG_LEVEL")
Expand Down
5 changes: 5 additions & 0 deletions patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func generateEnvironmentVariables(config agentConfig) ([]corev1.EnvVar, error) {
Name: "ELASTIC_APM_SERVER_URL", Value: "http://$(HOST_IP):8200",
})
}
if _, ok := config.Environment["ELASTIC_APM_ACTIVATION_METHOD"]; !ok {
vars = append(vars, corev1.EnvVar{
Name: "ELASTIC_APM_ACTIVATION_METHOD", Value: "K8S_ATTACH",
})
}
for k, v := range kubernetesEnvironmentVariables {
vars = append(vars, corev1.EnvVar{
Name: k,
Expand Down
9 changes: 6 additions & 3 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func TestEnvVarPatch(t *testing.T) {
patches := createEnvVariablesPatches(vars, true, 0)
assert.Len(t, patches, 1)
environmentVariables := patches[0].Value.([]corev1.EnvVar)
assert.Len(t, environmentVariables, 8)
assert.Len(t, environmentVariables, 9)
assert.Equal(t, "ELASTIC_APM_SECRET_TOKEN", environmentVariables[0].Name)
assert.Equal(t, "ELASTIC_APM_API_KEY", environmentVariables[1].Name)
assert.Equal(t, "ELASTIC_APM_ACTIVATION_METHOD", environmentVariables[2].Name)

m := map[string]struct{}{
"KUBERNETES_NODE_NAME": {},
Expand All @@ -51,7 +52,7 @@ func TestEnvVarPatch(t *testing.T) {
delete(m, envVar.Name)
}
assert.Len(t, m, 0)
customVars := environmentVariables[6:]
customVars := environmentVariables[7:]
sort.Slice(customVars, func(i, j int) bool {
return customVars[i].Name < customVars[j].Name
})
Expand All @@ -66,12 +67,14 @@ func TestAddAPMServerURLIfNotPresent(t *testing.T) {
patches := createEnvVariablesPatches(vars, true, 0)
assert.Len(t, patches, 1)
environmentVariables := patches[0].Value.([]corev1.EnvVar)
assert.Len(t, environmentVariables, 8)
assert.Len(t, environmentVariables, 9)
assert.Equal(t, "ELASTIC_APM_SECRET_TOKEN", environmentVariables[0].Name)
assert.Equal(t, "ELASTIC_APM_API_KEY", environmentVariables[1].Name)
assert.Equal(t, "HOST_IP", environmentVariables[2].Name)
assert.Equal(t, "ELASTIC_APM_SERVER_URL", environmentVariables[3].Name)
assert.Equal(t, "http://$(HOST_IP):8200", environmentVariables[3].Value)
assert.Equal(t, "ELASTIC_APM_ACTIVATION_METHOD", environmentVariables[4].Name)
assert.Equal(t, "K8S_ATTACH", environmentVariables[4].Value)
}

func TestInitContainerPatch(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions test/nodejs/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# This file isn't currently used, but can be nice for manual testing or demos.
apiVersion: apps/v1
kind: Deployment
metadata:
Expand Down
2 changes: 1 addition & 1 deletion testdata/bad.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
agents:
java:
environment:
ELASTIC_APM_SERVER_URLS: "http://34.78.173.219:8200"
ELASTIC_APM_SERVER_URL: "http://34.78.173.219:8200"
ELASTIC_APM_SERVICE_NAME: "petclinic"
ELASTIC_APM_ENVIRONMENT: "test"
ELASTIC_APM_LOG_LEVEL: "debug"
Expand Down
2 changes: 1 addition & 1 deletion testdata/bad2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ agents:
java:
image: docker.com/elastic/agent-java:1.2.3
environment:
ELASTIC_APM_SERVER_URLS: "http://34.78.173.219:8200"
ELASTIC_APM_SERVER_URL: "http://34.78.173.219:8200"
ELASTIC_APM_SERVICE_NAME: "petclinic"
ELASTIC_APM_ENVIRONMENT: "test"
ELASTIC_APM_LOG_LEVEL: "debug"
Expand Down
2 changes: 1 addition & 1 deletion testdata/good.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ agents:
image: docker.com/elastic/agent-java:1.2.3
artifact: foo
environment:
ELASTIC_APM_SERVER_URLS: "http://34.78.173.219:8200"
ELASTIC_APM_SERVER_URL: "http://34.78.173.219:8200"
ELASTIC_APM_SERVICE_NAME: "petclinic"
ELASTIC_APM_ENVIRONMENT: "test"
ELASTIC_APM_LOG_LEVEL: "debug"
Expand Down

0 comments on commit 7e30998

Please sign in to comment.