Once enabled, the Datadog Agent container collects the traces emitted from the other application containers on the same host as itself.
Configure the Datadog Agent to accept traces
To collect all traces from your running ECS containers, update your Agent’s Task Definition from the original ECS Setup with the configuration below.
Use datadog-agent-ecs-apm.json as a reference point for the required base configuration. In the Task Definition for Datadog Agent container, set the portMappings for a host to container port on 8126 with the protocol tcp.
If you are updating a local file for your Agent’s Task Definition, register your updated Task Definition. This creates a new revision. You can then reference this updated revision in the daemon service for the Datadog Agent.
Configure your application container to submit traces to Datadog Agent
Provide the private IP address for the EC2 instance
Provide the tracer with the private IP address of the underlying EC2 instance that the application container is running on. This address is the hostname of the tracer endpoint. The Datadog Agent container on the same host (with the host port enabled) receives these traces.
Use one of the following methods to dynamically get the private IP address:
The Amazon’s EC2 metadata endpoint (IMDSv1) allows discovery of the private IP address. To get the private IP address for each host, curl the following URL:
The Amazon’s ECS container metadata file allows discovery of the private IP address. To get the private IP address for each host, run the following command:
Provide the result of this request to the tracer by setting the DD_AGENT_HOST environment variable for each application container that sends traces.
Configure the Trace Agent endpoint
In cases where variables on your ECS application are set at launch time (Java, .NET, and PHP), you must set the hostname of the tracer endpoint as an environment variable with DD_AGENT_HOST using one of the above methods. The examples below use the IMDSv1 metadata endpoint, but the configuration can be interchanged if needed. If you have a startup script as your entry point, include this call as part of the script, otherwise add it to the ECS Task Definition’s entryPoint.
For other supported languages (Python, JavaScript, Ruby, and Go) you can alternatively set the hostname in your application’s source code.
For Python the startup command is generally ddtrace-run python my_app.py but may vary depending on the framework used, for example, using uWSGI or instrumenting your code manually with patch_all.
Code
You can alternatively update your code to have the tracer set the hostname explicitly:
You can alternatively update your code to have the tracer set the hostname explicitly:
require'datadog'# Use 'ddtrace' if you're using v1.xrequire'net/http'Datadog.configuredo|c|c.agent.host=Net::HTTP.get(URI('http://169.254.169.254/latest/meta-data/local-ipv4'))end
Launch time variable
Update the Task Definition’s entryPoint with the following, substituting your <Go Startup Command>:
You can alternatively update your code to have the tracer set the hostname explicitly:
packagemainimport("net/http""io/ioutil""gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer")funcmain(){resp,err:=http.Get("http://169.254.169.254/latest/meta-data/local-ipv4")bodyBytes,err:=ioutil.ReadAll(resp.Body)host:=string(bodyBytes)iferr==nil{//set the output of the curl command to the DD_AGENT_HOST env
os.Setenv("DD_AGENT_HOST",host)// tell the trace agent the host setting
tracer.Start(tracer.WithAgentAddr(host))defertracer.Stop()}//...
}
Launch time variable
Update the Task Definition’s entryPoint with the following, substituting your <Java Startup Command>:
For Apache and mod_php in VirtualHost or server configuration file, use PassEnv to set DD_AGENT_HOST and other environment variables, such as the variables for Unified Service Tagging like the below example:
When the ini param is set as clear_env=on, in the pool workers file www.conf you must also configure environment variables to be read from the host. Use this to also set DD_AGENT_HOST and other environment variables, such as the variables for Unified Service Tagging like the below example:
When using IMDSv2, the equivalent entryPoint configuration looks like the following. Substitute <Startup Command> with the appropriate command based on your language, as in the examples above.