|
| 1 | +--- |
| 2 | +title: "How to export telemetry data to Grafana" |
| 3 | +description: "Complete guide to exporting telemetry data with OpenTelemetry and Grafana. Learn how to configure collectors, visualize traces, and monitor application performance." |
| 4 | +url: /examples/grafana_tutorial/ |
| 5 | +--- |
| 6 | + |
| 7 | +[OpenTelemetry](https://opentelemetry.io/) (often abbreviated as OTel) is an |
| 8 | +open-source observability framework that provides a standardized way to collect |
| 9 | +and export telemetry data such as traces, metrics and logs. Deno has built-in |
| 10 | +support for OpenTelemetry, making it easy to instrument your applications |
| 11 | +without adding external dependencies. This integration works out of the box with |
| 12 | +observability platforms like [Grafana](https://grafana.com/). |
| 13 | + |
| 14 | +Grafana is an open-source observability platform that lets DevOps teams |
| 15 | +visualize, query, and alert on metrics, logs, and traces from diverse data |
| 16 | +sources in real time. It’s widely used for building dashboards to monitor |
| 17 | +infrastructure, applications, and systems health. |
| 18 | + |
| 19 | +Grafana also offers a hosted version called |
| 20 | +[Grafana Cloud](https://grafana.com/products/cloud/). This tutorial will help |
| 21 | +you configure your project to export OTel data to Grafana Cloud. |
| 22 | + |
| 23 | +In this tutorial, we'll build a simple application and export its telemetry data |
| 24 | +to Grafana Cloud. We'll cover: |
| 25 | + |
| 26 | +- [Set up your chat app](#set-up-your-chat-app) |
| 27 | +- [Set up a Docker collector](#set-up-a-docker-collector) |
| 28 | +- [Generating telemetry data](#generating-telemetry-data) |
| 29 | +- [Viewing telemetry data](#viewing-telemetry-data) |
| 30 | + |
| 31 | +You can find the complete source code for this tutorial |
| 32 | +[on GitHub](https://github.com/denoland/examples/tree/main/with-grafana). |
| 33 | + |
| 34 | +## Set up your chat app |
| 35 | + |
| 36 | +For this tutorial, we'll use a simple chat application to demonstrate how to |
| 37 | +export telemetry data. You can find the |
| 38 | +[code for the app on GitHub](https://github.com/denoland/examples/tree/main/with-grafana). |
| 39 | + |
| 40 | +Either take a copy of that repository or create a |
| 41 | +[main.ts](https://github.com/denoland/examples/blob/main/with-grafana/main.ts) |
| 42 | +file and a |
| 43 | +[.env](https://github.com/denoland/examples/blob/main/with-grafana/.env.example) |
| 44 | +file. |
| 45 | + |
| 46 | +In order to run the app you will need an OpenAI API key. You can get one by |
| 47 | +signing up for an account at [OpenAI](https://platform.openai.com/signup) and |
| 48 | +creating a new secret key. You can find your API key in the |
| 49 | +[API keys section](https://platform.openai.com/account/api-keys) of your OpenAI |
| 50 | +account. Once you have an API key, set up an `OPENAI_API-KEY` environment |
| 51 | +variable in your `.env` file: |
| 52 | + |
| 53 | +```env title=".env" |
| 54 | +OPENAI_API_KEY=your_openai_api_key |
| 55 | +``` |
| 56 | + |
| 57 | +## Set up a Docker collector |
| 58 | + |
| 59 | +Next, we'll set up a Docker container to run the OpenTelemetry collector. The |
| 60 | +collector is responsible for receiving telemetry data from your application and |
| 61 | +exporting it to Grafana Cloud. |
| 62 | + |
| 63 | +In the same directory as your `main.ts` file, create a `Dockerfile` and an |
| 64 | +`otel-collector.yml` file. The `Dockerfile` will be used to build a Docker |
| 65 | +image: |
| 66 | + |
| 67 | +```dockerfile title="Dockerfile" |
| 68 | +FROM otel/opentelemetry-collector-contrib:latest |
| 69 | + |
| 70 | +COPY otel-collector.yml /otel-config.yml |
| 71 | + |
| 72 | +CMD ["--config", "/otel-config.yml"] |
| 73 | +``` |
| 74 | + |
| 75 | +[`FROM otel/opentelemetry-collector-contrib:latest`](https://hub.docker.com/r/otel/opentelemetry-collector-contrib/) - |
| 76 | +This line specifies the base image for the container. It uses the official |
| 77 | +OpenTelemetry Collector Contributor image, which contains all receivers, |
| 78 | +exporters, processors, connectors, and other optional components, and pulls the |
| 79 | +latest version. |
| 80 | + |
| 81 | +`COPY otel-collector.yml /otel-config.yml` - This instruction copies our |
| 82 | +configuration file named `otel-collector.yml` from the local build context into |
| 83 | +the container. The file is renamed to `/otel-config.yml` inside the container. |
| 84 | + |
| 85 | +`CMD ["--config", "/otel-config.yml"]` - This sets the default command that will |
| 86 | +run when the container starts. It tells the OpenTelemetry Collector to use the |
| 87 | +configuration file we copied in the previous step. |
| 88 | + |
| 89 | +Next, let's setup a Grafana Cloud account and grab some info. |
| 90 | + |
| 91 | +If you have not already, |
| 92 | +[create a free Grafana Cloud account](https://grafana.com/auth/sign-up/create-user). |
| 93 | +Once created, you will receive a Grafana Cloud stack. Click "Details". |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +Next, find "OpenTelemetry" and click "Configure". |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +This page will provide you with all the details you'll need to configure your |
| 102 | +OpenTelemetry collector. Make note of your **OTLP Endpoint**, **Instance ID**, |
| 103 | +and **Password / API Token** (you will have to generate one). |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +Next, add the following to your `otel-collector.yml` file to define how how |
| 108 | +telemetry data should be collected and exported to Grafana Cloud: |
| 109 | + |
| 110 | +```yml title="otel-collector.yml" |
| 111 | +receivers: |
| 112 | + otlp: |
| 113 | + protocols: |
| 114 | + grpc: |
| 115 | + endpoint: 0.0.0.0:4317 |
| 116 | + http: |
| 117 | + endpoint: 0.0.0.0:4318 |
| 118 | + |
| 119 | +exporters: |
| 120 | + otlphttp/grafana_cloud: |
| 121 | + endpoint: $_YOUR_GRAFANA_OTLP_ENDPOINT |
| 122 | + auth: |
| 123 | + authenticator: basicauth/grafana_cloud |
| 124 | + |
| 125 | +extensions: |
| 126 | + basicauth/grafana_cloud: |
| 127 | + client_auth: |
| 128 | + username: $_YOUR_INSTANCE_ID |
| 129 | + password: $_YOUR_API_TOKEN |
| 130 | + |
| 131 | +processors: |
| 132 | + batch: |
| 133 | + |
| 134 | +service: |
| 135 | + extensions: [basicauth/grafana_cloud] |
| 136 | + pipelines: |
| 137 | + traces: |
| 138 | + receivers: [otlp] |
| 139 | + processors: [batch] |
| 140 | + exporters: [otlphttp/grafana_cloud] |
| 141 | + metrics: |
| 142 | + receivers: [otlp] |
| 143 | + processors: [batch] |
| 144 | + exporters: [otlphttp/grafana_cloud] |
| 145 | + logs: |
| 146 | + receivers: [otlp] |
| 147 | + processors: [batch] |
| 148 | + exporters: [otlphttp/grafana_cloud] |
| 149 | +``` |
| 150 | +
|
| 151 | +The `receivers` section configures how the collector receives data. It sets up |
| 152 | +an OTLP (OpenTelemetry Protocol) receiver that listens on two protocols, `gRPC` |
| 153 | +and `HTTP`, the `0.0.0.0` address means it will accept data from any source. |
| 154 | + |
| 155 | +The `exporters` section defines where the collected data should be sent. Be sure |
| 156 | +to include **the OTLP endpoint** provided by your Grafana Cloud instance. |
| 157 | + |
| 158 | +The `extensions` section defines the authentication for OTel to export data to |
| 159 | +Grafana Cloud. Be sure to include your Grafana Cloud **Instance ID**, as well as |
| 160 | +your generated **Password / API Token**. |
| 161 | + |
| 162 | +The `processors` section defines how the data should be processed before export. |
| 163 | +It uses batch processing with a timeout of 5 seconds and a maximum batch size of |
| 164 | +5000 items. |
| 165 | + |
| 166 | +The `service` section ties everything together by defining three pipelines. Each |
| 167 | +pipeline is responsible for a different type of telemetry data. The logs |
| 168 | +pipeline collects application logs. The traces pipeline is for distributed |
| 169 | +tracing data. The metric pipeline is for performance metrics. |
| 170 | + |
| 171 | +Build and run the docker instance to start collecting your telemetry data with |
| 172 | +the following command: |
| 173 | + |
| 174 | +```sh |
| 175 | +docker build -t otel-collector . && docker run -p 4317:4317 -p 4318:4318 otel-collector |
| 176 | +``` |
| 177 | + |
| 178 | +## Generating telemetry data |
| 179 | + |
| 180 | +Now that we have the app and the docker container set up, we can start |
| 181 | +generating telemetry data. Run your application with these environment variables |
| 182 | +to send data to the collector: |
| 183 | + |
| 184 | +```sh |
| 185 | +OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318 \ |
| 186 | +OTEL_SERVICE_NAME=chat-app \ |
| 187 | +OTEL_DENO=true \ |
| 188 | +deno run --unstable-otel --allow-net --allow-env --env-file --allow-read main.ts |
| 189 | +``` |
| 190 | + |
| 191 | +This command: |
| 192 | + |
| 193 | +- Points the OpenTelemetry exporter to your local collector (`localhost:4318`) |
| 194 | +- Names your service "chat-app" in Grafana Cloud |
| 195 | +- Enables Deno's OpenTelemetry integration |
| 196 | +- Runs your application with the necessary permissions |
| 197 | + |
| 198 | +To generate some telemetry data, make a few requests to your running application |
| 199 | +in your browser at [`http://localhost:8000`](http://localhost:8000). |
| 200 | + |
| 201 | +Each request will: |
| 202 | + |
| 203 | +1. Generate traces as it flows through your application |
| 204 | +2. Send logs from your application's console output |
| 205 | +3. Create metrics about the request performance |
| 206 | +4. Forward all this data through the collector to Grafana Cloud |
| 207 | + |
| 208 | +## Viewing telemetry data |
| 209 | + |
| 210 | +After making some requests to your application, you'll see three types of data |
| 211 | +in your Grafana Cloud dashboard: |
| 212 | + |
| 213 | +1. **Traces** - End-to-end request flows through your system |
| 214 | +2. **Logs** - Console output and structured log data |
| 215 | +3. **Metrics** - Performance and resource utilization data |
| 216 | + |
| 217 | + |
| 218 | + |
| 219 | +You can drill down into individual spans to debug performance issues: |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | +🦕 Now that you have telemetry export working, you could: |
| 224 | + |
| 225 | +1. Add custom spans and attributes to better understand your application |
| 226 | +2. Set up alerts based on latency or error conditions |
| 227 | +3. Deploy your application and collector to production using platforms like: |
| 228 | + - [Fly.io](https://docs.deno.com/examples/deploying_deno_with_docker/) |
| 229 | + - [Digital Ocean](https://docs.deno.com/examples/digital_ocean_tutorial/) |
| 230 | + - [AWS Lightsail](https://docs.deno.com/examples/aws_lightsail_tutorial/) |
| 231 | + |
| 232 | +For more details on OpenTelemetry configuration, check out the |
| 233 | +[Grafana Cloud documentation](https://grafana.com/docs/grafana-cloud/monitor-applications/application-observability/collector/). |
0 commit comments