---
title: Instrumenting a Ruby Cloud Run Function
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Serverless > Google Cloud Run > Instrumenting Cloud Run Functions >
  Instrumenting a Ruby Cloud Run Function
---

# Instrumenting a Ruby Cloud Run Function

{% alert level="info" %}
A sample application is [available on GitHub](https://github.com/DataDog/serverless-gcp-sample-apps/tree/main/cloud-run/sidecar/ruby).
{% /alert %}

## Setup{% #setup %}

1. **Install the Datadog Ruby tracer**.

Add the `datadog` gem to your Gemfile:

   ```
   source 'https://rubygems.org'
   gem 'datadog'
```



See [Tracing Ruby applications](https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/dd_libraries/ruby/#instrument-your-application) for additional information on how to configure the tracer and enable auto instrumentation.

1. **Install serverless-init as a sidecar**.

   {% tab title="Datadog CLI" %}
   Setup: 
Install the Datadog CLI

   ```shell
   npm install -g @datadog/datadog-ci @datadog/datadog-ci-plugin-cloud-run
   ```

Install the [gcloud CLI](https://cloud.google.com/sdk/docs/install) and authenticate with `gcloud auth login`.
Configuration: 
Configure the [Datadog site](https://docs.datadoghq.com/getting_started/site/) and Datadog API key, and define the service name to use in Datadog.

   ```shell
   export DATADOG_SITE="<DATADOG_SITE>"
   export DD_API_KEY="<DD_API_KEY>"
   export DD_SERVICE="<SERVICE_NAME>"
   ```
Instrument: 
If you are new to Datadog serverless monitoring, launch the Datadog CLI in interactive mode to guide your first installation for a quick start.

   ```shell
   datadog-ci cloud-run instrument -i
   ```

To set up the Datadog sidecar for your applications, run the `instrument` command *after* your normal deployment. You can specify multiple services to instrument by passing multiple `--service` flags.

   ```shell
   datadog-ci cloud-run instrument --project <GCP-PROJECT-ID> --service <CLOUD-RUN-SERVICE-NAME> --region <GCP-REGION>
   ```

You can pin to a specific image with the `--sidecar-image` flag. See the [latest releases on Docker Hub](https://hub.docker.com/r/datadog/serverless-init).

Additional parameters can be found in the [CLI documentation](https://github.com/DataDog/datadog-ci/tree/master/packages/plugin-cloud-run#arguments).
   {% /tab %}

   {% tab title="Terraform" %}
The [Datadog Terraform module for Google Cloud Run](https://github.com/DataDog/terraform-google-cloud-run-datadog) wraps the [`google_cloud_run_v2_service`](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_v2_service) resource and automatically configures your Cloud Run app for Datadog Serverless Monitoring by adding required environment variables and the serverless-init sidecar.

If you don't already have Terraform set up, [install Terraform](https://developer.hashicorp.com/terraform/install), create a new directory, and make a file called `main.tf`.

Then, add the following to your Terraform configuration, updating it as necessary based on your needs:

   ```tf
   variable "datadog_api_key" {
     description = "Your Datadog API key"
     type        = string
     sensitive   = true
   }
   
   module "my-cloud-run-app" {
     source  = "DataDog/cloud-run-datadog/google"
     version = "~> 1.0"
   
     project  = "my-gcp-project"
     name     = "my-cloud-run-app"
     location = "us-central1"
   
     datadog_api_key = var.datadog_api_key
     datadog_service = "test-service" // your application service
     datadog_version = "0.0.0" // your code version
     datadog_env     = "prod" // your application environment
     
     datadog_enable_logging = true
   
     deletion_protection = false
     build_config = {
       function_target          = "helloHttp" // your function entry point
       image_uri                = "us-docker.pkg.dev/cloudrun/container/hello"
       base_image               = "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/your-runtime" // base image for your runtime
       enable_automatic_updates = true
     }
     template = {
       containers = [
         {
           name  = "main"
           image = "us-docker.pkg.dev/cloudrun/container/hello"
           base_image_uri = "us-central1-docker.pkg.dev/serverless-runtimes/google-22-full/runtimes/your-runtime" // base image for your runtime
           resources = {
             limits = {
               cpu    = "1"
               memory = "512Mi"
             }
           }
           ports = {
             container_port = 8080
           }
           env = [
             { name = "DD_TRACE_ENABLED", value = "true" },
           ]
         },
       ]
     }
   }
   ```

See the Environment Variables for more information on the configuration options available through the `env`.

Ensure the container port for the main container is the same as the one exposed in your Dockerfile/service.

If you haven't already, initialize your Terraform project:

   ```shell
   terraform init
   ```

To deploy your app, run:

   ```shell
   terraform apply
   ```

      {% /tab %}

   {% tab title="Other" %}
After deploying your Cloud Run app, you can manually modify your app's settings to enable Datadog monitoring.

   1. Create a **Volume** with `In-Memory` volume type.

   1. Add a **new container** with image URL: `gcr.io/datadoghq/serverless-init:<YOUR_TAG>`. See the [latest releases on Docker Hub](https://hub.docker.com/r/datadog/serverless-init) to pin a specific version.

   1. Add the volume mount to every container in your application. Choose a path such as `/shared-volume`, and remember it for the next step.

   1. Add the following environment variables to your `serverless-init` sidecar container:

      - `DD_SERVICE`: A name for your service. For example, `gcr-sidecar-test`.
      - `DD_ENV`: A name for your environment. For example, `dev`.
      - `DD_SERVERLESS_LOG_PATH`: Your log path. For example, `/shared-volume/logs/*.log`. The path must begin with the mount path you defined in the previous step.
      - `DD_API_KEY`: Your [Datadog API key](https://app.datadoghq.com/organization-settings/api-keys).
      - `FUNCTION_TARGET`: The entry point of your function. For example, `Main`.

For a list of all environment variables, including additional tags, see Environment variables.

      {% /tab %}

1. **Set up logs**.

In the previous step, you created a shared volume. You may have also set the `DD_SERVERLESS_LOG_PATH` environment variable, which defaults to `/shared-volume/logs/app.log`.

In this step, configure your logging library to write logs to the file set in `DD_SERVERLESS_LOG_PATH`. You can also set a custom format for log/trace correlation and other features. Datadog recommends setting the environment variable `DD_SOURCE=ruby` in your sidecar container to enable advanced Datadog log parsing.

Then, update your logging library. For example, you can use Ruby's native `logger` library:

   ```ruby
   LOG_FILE = "/shared-logs/logs/app.log"
   FileUtils.mkdir_p(File.dirname(LOG_FILE))
   
   logger = Logger.new(LOG_FILE)
   logger.formatter = proc do |severity, datetime, progname, msg|
     "[#{datetime}] #{severity}: [#{Datadog::Tracing.log_correlation}] #{msg}\n"
   end
   
   logger.info "Hello World!"
```



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

1. **Add a service label in Google Cloud**. In your Cloud Run service's info panel, add a label with the following key and value:

| Key       | Value                                                                                          |
| --------- | ---------------------------------------------------------------------------------------------- |
| `service` | The name of your service. Matches the value provided as the `DD_SERVICE` environment variable. |

See [Configure labels for services](https://cloud.google.com/run/docs/configuring/services/labels) in the Cloud Run documentation for instructions.

**Send custom metrics**.

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

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

| Variable                 | Description                                                                                                                                                                                                                                                  | Container             |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------- |
| `DD_API_KEY`             | [Datadog API key](https://app.datadoghq.com/organization-settings/api-keys) - **Required**                                                                                                                                                                   | Sidecar container     |
| `DD_SITE`                | [Datadog site](https://docs.datadoghq.com/getting_started/site/) - **Required**                                                                                                                                                                              | Sidecar container     |
| `DD_SERVICE`             | Datadog Service name. **Required**                                                                                                                                                                                                                           | Both containers       |
| `DD_SERVERLESS_LOG_PATH` | The path where the sidecar should tail logs from. Recommended to set to `/shared-volume/logs/app.log`.                                                                                                                                                       | Sidecar container     |
| `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.                                                         | Application container |
| `DD_VERSION`             | See [Unified Service Tagging](https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging/).                                                                                                                                                  | Both containers       |
| `DD_ENV`                 | See [Unified Service Tagging](https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging/).                                                                                                                                                  | Both containers       |
| `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 `ruby`, or use your custom pipeline. Defaults to `cloudrun`. | Sidecar container     |
| `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`).                                                                                                              | Sidecar container     |
| `FUNCTION_TARGET`        | Required for correct tagging. The entry point of your function. For example, `Main`. You can also find `FUNCTION_TARGET` on the source tab inside Google console: `Function entry point`.                                                                    | Sidecar container     |

**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 Cloud Run services appear in the [software catalog](https://cloud.google.com/run/docs/configuring/services/labels), you must set the `DD_SERVICE`, `DD_VERSION`, and `DD_ENV` environment variables.

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

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