---
title: Instrumenting a Go Container App In-Container
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Serverless > Azure Container Apps > In-Container Instrumentation >
  Instrumenting a Go Container App In-Container
---

# Instrumenting a Go Container App In-Container

## Setup{% #setup %}

1. **Install the Datadog Go tracer**.

   1. In your main application, add the tracing library from `dd-trace-go`.

      ```shell
      go get github.com/DataDog/dd-trace-go/v2/ddtrace/tracer
```

   1. Add the following to your application code to initialize the tracer:

      ```go
      tracer.Start()
      defer tracer.Stop()
```

You can also add additional packages:

   ```shell
   # Enable Profiling
   go get github.com/DataDog/dd-trace-go/v2/profiler
   
   # Patch /net/http
   go get github.com/DataDog/dd-trace-go/contrib/net/http/v2
```



For more information, see [Tracing Go Applications](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/go/) and the [Tracer README](https://github.com/DataDog/dd-trace-go?tab=readme-ov-file#installing).

1. **Install serverless-init**.

Datadog publishes new releases of the `serverless-init` container image to Google's gcr.io, AWS's ECR, and on Docker Hub:

| hub.docker.com          | gcr.io                           | public.ecr.aws                         |
| ----------------------- | -------------------------------- | -------------------------------------- |
| datadog/serverless-init | gcr.io/datadoghq/serverless-init | public.ecr.aws/datadog/serverless-init |

Images are tagged based on semantic versioning, with each new version receiving three relevant tags:

   - `1`, `1-alpine`: use these to track the latest minor releases, without breaking changes
   - `1.x.x`, `1.x.x-alpine`: use these to pin to a precise version of the library
   - `latest`, `latest-alpine`: use these to follow the latest version release, which may include breaking changes

Add the following instructions and arguments to your Dockerfile.

   ```dockerfile
   COPY --from=datadog/serverless-init:<YOUR_TAG> /datadog-init /app/datadog-init
   ENTRYPOINT ["/app/datadog-init"]
   CMD [./your-binary]
   ```

   {% collapsible-section %}
   Alternative configuration: 
Datadog expects `serverless-init` to be the top-level application, with the rest of your app's command line passed in for `serverless-init` to execute.

If you already have an entrypoint defined inside your Dockerfile, you can instead modify the CMD argument.

   ```dockerfile
   CMD ["/app/datadog-init", ./your-binary]
   ```

If you require your entrypoint to be instrumented as well, you can instead swap your entrypoint and CMD arguments.

   ```dockerfile
   ENTRYPOINT ["/app/datadog-init"]
   CMD ["/your_entrypoint.sh", ./your-binary]
   ```

As long as your command to run is passed as an argument to `datadog-init`, you will receive full instrumentation.
   {% /collapsible-section %}

1. **Set up logs**.

To enable logging, set the environment variable `DD_LOGS_ENABLED=true`. This allows `serverless-init` to read logs from stdout and stderr.

Datadog also recommends setting the environment variable `DD_SOURCE=go` to enable advanced Datadog log parsing.

If you want multiline logs to be preserved in a single log message, Datadog recommends writing your logs in JSON format. For example, you can use a third-party logging library such as `logrus`:

   ```go
   logrus.SetFormatter(&logrus.JSONFormatter{})
   logrus.AddHook(&dd_logrus.DDContextLogHook{})
   
   logrus.WithContext(ctx).Info("Hello World!")
   ```

For more information, see [Correlating Go Logs and Traces](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/go/).

1. **Configure your application**.

After the container is built and pushed to your registry, set the required environment variables for the Datadog Agent:

   - `DD_API_KEY`: Your [Datadog API key](https://app.datadoghq.com/organization-settings/api-keys), used to send data to your Datadog account. For privacy and safety, configure this API key as a Google Cloud Secret.
   - `DD_SITE`: Your [Datadog site](https://docs.datadoghq.com/getting_started/site/). For example, `datadoghq.com`.

For more environment variables, see the Environment variables section on this page.

**Send custom metrics**.

To send custom metrics, [install the DogStatsD client](https://docs.datadoghq.com/extend/dogstatsd/?tab=go#install-the-dogstatsd-client) and [view code examples](https://docs.datadoghq.com/metrics/custom_metrics/dogstatsd_metrics_submission/?tab=go#code-examples-5). In serverless, only the *distribution* metric type is supported.

### Environment variables{% #environment-variables %}

| Variable                   | Description                                                                                                                                                                                                                                                    |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `DD_API_KEY`               | [Datadog API key](https://app.datadoghq.com/organization-settings/api-keys) - **Required**                                                                                                                                                                     |
| `DD_SITE`                  | [Datadog site](https://docs.datadoghq.com/getting_started/site/) - **Required**                                                                                                                                                                                |
| `DD_SERVICE`               | Datadog Service name. **Required**                                                                                                                                                                                                                             |
| `DD_AZURE_SUBSCRIPTION_ID` | Azure Subscription ID. **Required**                                                                                                                                                                                                                            |
| `DD_AZURE_RESOURCE_GROUP`  | Azure Resource Group name. **Required**                                                                                                                                                                                                                        |
| `DD_LOGS_ENABLED`          | When true, send logs (stdout and stderr) to Datadog. Defaults to false.                                                                                                                                                                                        |
| `DD_LOGS_INJECTION`        | When true, enrich all logs with trace data for supported loggers. See [Correlate Logs and Traces](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/) for more information.                                                           |
| `DD_VERSION`               | See [Unified Service Tagging](https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging/).                                                                                                                                                    |
| `DD_ENV`                   | See [Unified Service Tagging](https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging/).                                                                                                                                                    |
| `DD_SOURCE`                | Set the log source to enable a [Log Pipeline](https://docs.datadoghq.com/logs/log_configuration/pipelines) for advanced parsing. To automatically apply language-specific parsing rules, set to `go`, or use your custom pipeline. Defaults to `containerapp`. |
| `DD_TAGS`                  | Add custom tags to your logs, metrics, and traces. Tags should be comma separated in key/value format (for example: `key1:value1,key2:value2`).                                                                                                                |

**Do not set** the following environment variables in your serverless environment. They should only be set in non-serverless environments.

- `DD_AGENT_HOST`
- `DD_TRACE_AGENT_URL`

## Troubleshooting{% #troubleshooting %}

This integration depends on your runtime having a full SSL implementation. If you are using a slim image, you may need to add the following command to your Dockerfile to include certificates:

```dockerfile
RUN apt-get update && apt-get install -y ca-certificates
```

To have your Azure Container Apps appear in the [software catalog](https://docs.datadoghq.com/internal_developer_portal/software_catalog/), you must set the `DD_SERVICE`, `DD_VERSION`, and `DD_ENV` environment variables.

## Further reading{% #further-reading %}

- [Tracing Go Applications](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/go/)
- [Correlating Go Logs and Traces](https://docs.datadoghq.com/tracing/other_telemetry/connect_logs_and_traces/go/)
