Skip to content

Set APM from the host with apm.yaml

A host policy lets you choose which applications the agent traces, and how, in a file on the host instead of in the KloudMate UI. Use it when instances are created for you: an Auto Scaling group, a cloned AMI, a Docker or ECS fleet, or any host built by a pipeline. Every instance reads the same rules, applies them to the applications it finds, and stays registered, so pipeline rules, configuration changes, and agent upgrades keep working.

You need agent version 2.12 or later.

You write rules that pick applications by what they are, not by an ID that only exists on one host. For example, “the application named checkout gets the SDK” or “every Go service gets eBPF.” The agent matches each rule against the applications it discovers, so the same file works on every instance.

The policy can come from two places. If both are set, the environment variable wins:

SourceWhere
apm.yaml file/etc/kmagent/apm.yaml on Linux, C:\ProgramData\kmagent\apm.yaml on Windows. On the Docker and ECS agents, the host’s /etc/kmagent/apm.yaml. Set KM_APM_CONFIG to read it from somewhere else.
KM_APM_POLICY environment variableThe same YAML, inline. Use it for agents that run in a container, where a file on the host is harder to manage.

The agent reads the policy once, when it starts. After you change it, restart the agent.

# /etc/kmagent/apm.yaml
default: off            # mode for applications no rule matches: off, ebpf, or sdk
services:
  - match: { name: checkout }
    mode: sdk
  - match: { runtime: java, unit: "orders-*.service" }
    mode: sdk
    sampling: 0.2
  - match: { runtime: [go, ruby] }
    mode: ebpf

Each application gets the mode of the first rule that matches it, or default when none does. The modes are the same ones the UI offers:

  • off: not traced. This is the default.
  • ebpf: RED metrics and spans with no code change and no restart. On Windows, this is ETW monitoring, which covers IIS application pools only.
  • sdk: the OpenTelemetry SDK in the process, for full traces. Turning it on or off restarts the application once.

Every field you set in match has to match. A field can be one value or a list, and a list matches any of its values. Values are globs: * matches any run of characters except /, and ? matches one character.

FieldMatchesExample
nameThe application’s name, as the agent lists it on the APM tabname: checkout
runtimejava, nodejs, python, dotnet, php, go, or rubyruntime: [go, ruby]
unitThe systemd unit, PM2 app, Windows service, or IIS application poolunit: "orders-*.service"
containerThe Docker container namecontainer: "checkout*"
portA port the application listens on (a number, not a glob)port: 8080
exeThe path of the executableexe: "/opt/orders/*"
idThe service ID the agent assigned on this hostid: "systemd:orders.service"

Prefer name, runtime, unit, container, and port. They’re the same on every instance. An id is tied to one host, so it doesn’t carry to new instances.

On Amazon ECS, the agent names containers ecs-<task-family>-<revision>-<container>-<random>, so container: "ecs-orders-*" matches every task of the orders family on the instance.

KeyWhereValuesDefault
defaultTop leveloff, ebpf, sdkoff
restartTop level or a ruleon-change restarts an application to apply the SDK. never writes the change and lets it apply at the application’s next restart.on-change
samplingTop level or a ruleA ratio from 0 to 1 for SDK traces. For example, 0.2 keeps 20%.Keep every trace
service_nameA ruleThe service name the SDK reports, instead of the name the agent works outThe discovered name
versionTop level11

The agent checks the file when it starts. An unknown key, an invalid mode, or a sampling value outside 0 to 1 makes the whole policy invalid, and then nothing is traced from it. The agent logs why, and the UI shows Host policy not applied with the error.

If a pipeline rule sets head sampling, that rule decides sampling, and the policy’s sampling values are ignored.

Run kmagent apm check on a host that runs your applications. It lists each one with the values a rule can match:

sudo kmagent apm check
No APM policy found. These are the values a rule can match on.

checkout
  match   name=checkout  runtime=nodejs  unit=checkout.service  port=3000  exe=/usr/bin/node  id=systemd:checkout.service

To start from a file that already lists every application, use --init. It writes a policy with each application set to off and the values you could match on in a comment above it, and it never overwrites an existing file:

sudo kmagent apm check --init --policy /tmp/apm.yaml

Set the modes you want, then check what the policy does before you use it:

sudo kmagent apm check --policy /tmp/apm.yaml
Policy: /tmp/apm.yaml

checkout
  match   name=checkout  runtime=nodejs  unit=checkout.service  port=3000  exe=/usr/bin/node  id=systemd:checkout.service
  mode    sdk (services[0])
  how     systemd drop-in on checkout.service, then a restart of checkout.service

The how line tells you what the agent will do: write a systemd drop-in and restart the service, attach to a running JVM, restart a PM2 app, recreate a container, or use eBPF when there’s no SDK path. When the policy is ready, copy it into place and restart the agent:

sudo cp /tmp/apm.yaml /etc/kmagent/apm.yaml
sudo systemctl restart kmagent

See the CLI reference for every flag.

The agent’s APM tab shows Set by the host policy with the file’s path, and each application shows the mode the policy gave it. In Settings → Agents & Pipelines, the agent’s row shows APM from host policy.

You can still change a single host from the UI. When you click Apply on the APM tab, the selection you save replaces the host policy for that host, and its row shows Host policy overridden. Click Use host policy to remove the saved selection and go back to the file.

So the order is:

  1. A selection saved in the UI for this host.
  2. The host policy (KM_APM_POLICY, then apm.yaml).
  3. KM_INSTRUMENT_SELECTION and KM_INSTRUMENT_ALL.

In autonomous mode, the policy limits what the agent instruments, and the agent doesn’t check in.

PlatformWhere the policy goesWhat it can do
Linux host, Auto Scaling group, cloned AMI/etc/kmagent/apm.yamlSDK through systemd, PM2, or runtime attach, and eBPF. See AWS Auto Scaling.
WindowsC:\ProgramData\kmagent\apm.yamlSDK for Windows services and IIS application pools. ebpf means ETW, for IIS application pools only.
DockerKM_APM_POLICY on the agent container, or the host’s /etc/kmagent/apm.yamleBPF for every container and host process, and SDK for containers by recreating them. See Docker.
Amazon ECS on EC2KM_APM_POLICY in the daemon’s task definitioneBPF for the containers the policy selects. SDK comes from kmagent ecs inject, so an sdk rule falls back to eBPF. See Amazon ECS.
Amazon ECS on FargateNot usedThe task definition decides what’s instrumented.
KubernetesNot usedUse the APM tab or Helm values.

KM_APM_POLICY takes the same YAML on one line. Flow style keeps it on one line:

KM_APM_POLICY='{default: off, services: [{match: {name: checkout}, mode: sdk}, {match: {runtime: go}, mode: ebpf}]}'

When an application already runs another vendor’s agent, such as Datadog, New Relic, or Elastic APM, an sdk rule replaces it only if the rule names the application exactly, by name or id with no wildcard. A rule like default: sdk or name: "web-*" leaves the other agent running, and eBPF covers the application instead. Dynatrace, AppDynamics, and Instana agents are never replaced. kmagent apm check shows the other agent and whether the rule allows the switch.

The agent checks the policy against what’s running about once a minute, so a redeployed application picks up its mode again:

  • systemd services: the SDK lives in a drop-in next to the unit (/etc/systemd/system/<unit>.d/zz-km-otel.conf), so a deploy that replaces the files and restarts the unit keeps it. If a deploy deletes that directory, the agent writes the drop-in again and restarts the service once.
  • JVMs with no unit (started from /etc/rc.local, cron, or a script): the agent attaches to the new JVM, with no restart. Requests served before it attaches aren’t traced.
  • PM2 apps and containers: if the new app or container starts without the SDK, the agent restarts or recreates it once with the SDK. On Docker, Redeploy-safe SDK adds the SDK when the container is created, with no second restart.

Rules match by name, unit, or runtime, so they keep matching when a deploy changes the application’s ID. That happens with a new unit name, a new release directory, or a new container name. A selection saved in the UI is tied to the old ID, so after one of those deploys the application shows up as a new, untraced service.

When the SDK is turned on, the agent restarts the application and waits for it to come back healthy. If it doesn’t within the wait, the agent undoes the change and restarts it again. The wait is 30 seconds for services and 3 minutes for containers. For an application that takes longer to start, raise it with KM_INSTRUMENT_HEALTH_TIMEOUT:

echo 'KM_INSTRUMENT_HEALTH_TIMEOUT=10m' | sudo tee -a /etc/kmagent/kmagent.env
sudo systemctl restart kmagent

It takes a duration such as 10m, or a number of seconds, up to 30m. It applies to every runtime.

On the host:

sudo kmagent apm check
journalctl -u kmagent -b | grep -E 'APM policy|instrumentation reconcile|runtime-attach'

APM policy loaded gives the source and the number of rules. APM policy not applied gives the error that stopped it. Each application’s result appears as applied, no_change, pending_restart, rolledback, or unsupported.

In KloudMate, the agent’s row shows APM from host policy, and the applications start sending traces within a few minutes.