Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(docs): Azure App Insights Recipes (#2821)
* chore(docs): Adding App Insights Configuration Page * chore(docs): Adding App Insights Recipes * chore(docs): Adding App Insights Recipes * chore(docs): fixing typo * adding recipe links * adding recipe links * fixing typo
- Loading branch information
Showing
8 changed files
with
789 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
...amples-tutorials/recipes/running-tracetest-with-azure-app-insights-collector.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
# Running Tracetest with Azure App Insights (Node.js + OpenTelemetry Collector) | ||
|
||
:::note | ||
[Check out the source code on GitHub here.](https://github.com/kubeshop/tracetest/tree/main/examples/tracetest-azure-app-insights-collector) | ||
::: | ||
|
||
[Tracetest](https://tracetest.io/) is a testing tool based on [OpenTelemetry](https://opentelemetry.io/) that allows you to test your distributed application. It allows you to use data from distributed traces generated by OpenTelemetry to validate and assert if your application has the desired behavior defined by your test definitions. | ||
|
||
[Azure Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/app-insights-overview) is an extension of Azure Monitor and provides application performance monitoring (APM) features. APM tools are useful to monitor applications from development, through test, and into production in the following ways: | ||
|
||
- Proactively understand how an application is performing. | ||
- Reactively review application execution data to determine the cause of an incident. | ||
|
||
[OpenTelemetry Collector Contrib](https://github.com/open-telemetry/opentelemetry-collector-contrib) - The official OpenTelemetry Distribution for packages outside of the core collector. | ||
|
||
## Sample Node.js App with Azure App Insights, The OpenTelemetry Collector and Tracetest | ||
|
||
This is a simple quick start guide on how to configure a Node.js app to use instrumentation with traces and Tracetest for enhancing your E2E and integration tests with trace-based testing. The infrastructure will use Azure App Insights as the trace data store, the OpenTelemetry Collector to process and route the telemetry data and a Node.js app to generate it. | ||
|
||
## Prerequisites | ||
|
||
You will need [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed on your machine to run this quick start app! | ||
|
||
And the [App Insights Instrumentation Key](https://learn.microsoft.com/en-us/azure/bot-service/bot-service-resources-app-insights-keys?view=azure-bot-service-4.0) from your instance. | ||
|
||
## Project Structure | ||
|
||
The project is built with Docker Compose. | ||
|
||
### 1. Node.js App | ||
|
||
The `Dockerfile` and the `docker-compose.yaml` in the root directory is for the Node.js app. | ||
|
||
### 2. Tracetest | ||
|
||
The `docker-compose.yaml` file, `tracetest.provision.yaml`, and `tracetest-config.yaml` in the `tracetest` directory are for the setting up the Node.js App and Tracetest. | ||
|
||
### Docker Compose Network | ||
|
||
All `services` in the `docker-compose.yaml` are on the same network and will be reachable by hostname from within other services. | ||
|
||
## Node.js App | ||
|
||
The Node.js app is a simple Express app, contained in the `src/index.js` file. | ||
|
||
It is instrumented using the [Official OpenTelemetry Node.js SDK](https://www.npmjs.com/package/@opentelemetry/sdk-node) wrapping the application code to send telemetry data to the OpenTelemetry Collector. | ||
|
||
The following is the instrumentation code from the `src/tracing.js` file. | ||
|
||
```js | ||
const opentelemetry = require('@opentelemetry/sdk-node') | ||
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node') | ||
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc'); | ||
|
||
const sdk = new opentelemetry.NodeSDK({ | ||
traceExporter: new OTLPTraceExporter({ url: 'http://otel-collector:4317' }), | ||
instrumentations: [getNodeAutoInstrumentations()], | ||
serviceName: 'tracetest-azure-app-insights-collector' | ||
}) | ||
sdk.start() | ||
``` | ||
|
||
To start the server, run this command: | ||
|
||
```bash | ||
npm start | ||
``` | ||
|
||
As you can see the `Dockerfile` uses the command above. | ||
|
||
```Dockerfile | ||
FROM node:slim | ||
WORKDIR /usr/src/app | ||
|
||
COPY ./src/package*.json ./ | ||
|
||
RUN npm install | ||
COPY ./src . | ||
|
||
EXPOSE 3000 | ||
CMD [ "npm", "start" ] | ||
``` | ||
|
||
## Tracetest | ||
|
||
The `docker-compose.yaml` includes three other services. | ||
|
||
- **Postgres** - Postgres is a prerequisite for Tracetest to work. It stores trace data when running the trace-based tests. | ||
- [**Tracetest**](https://tracetest.io/) - Trace-based testing that generates end-to-end tests automatically from traces. | ||
- [**OpenTelemetry Collector Contrib**](https://github.com/open-telemetry/opentelemetry-collector-contrib) - The official Open Telemetry Distribution for packages outside of the core collector. | ||
|
||
```yaml | ||
services: | ||
postgres: | ||
environment: | ||
POSTGRES_PASSWORD: postgres | ||
POSTGRES_USER: postgres | ||
healthcheck: | ||
test: | ||
- CMD-SHELL | ||
- pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB" | ||
timeout: 5s | ||
interval: 1s | ||
retries: 60 | ||
image: postgres:14 | ||
networks: | ||
default: null | ||
ports: | ||
- mode: ingress | ||
target: 5432 | ||
published: 5432 | ||
protocol: tcp | ||
tracetest: | ||
command: --provisioning-file /app/provision.yaml | ||
platform: linux/amd64 | ||
depends_on: | ||
postgres: | ||
condition: service_healthy | ||
environment: | ||
TRACETEST_DEV: ${TRACETEST_DEV} | ||
extra_hosts: | ||
host.docker.internal: host-gateway | ||
healthcheck: | ||
test: | ||
- CMD | ||
- wget | ||
- --spider | ||
- localhost:11633 | ||
timeout: 3s | ||
interval: 1s | ||
retries: 60 | ||
image: kubeshop/tracetest:${TAG:-latest} | ||
networks: | ||
default: null | ||
ports: | ||
- mode: ingress | ||
target: 11633 | ||
published: 11633 | ||
protocol: tcp | ||
volumes: | ||
- type: bind | ||
source: tracetest/tracetest.yaml | ||
target: /app/tracetest.yaml | ||
- type: bind | ||
source: tracetest/tracetest-provision.yaml | ||
target: /app/provision.yaml | ||
otel-collector: | ||
image: otel/opentelemetry-collector-contrib:latest | ||
command: | ||
- "--config" | ||
- "/otel-local-config.yaml" | ||
volumes: | ||
- ./collector.config.yaml:/otel-local-config.yaml | ||
environment: | ||
INSTRUMENTATION_KEY: ${INSTRUMENTATION_KEY} | ||
ports: | ||
- 4317:4317 | ||
networks: | ||
default: | ||
name: _default | ||
``` | ||
|
||
Tracetest depends on Postgres and requires config files to be loaded via a volume. The volumes are mapped from the root directory into the `root` directory and the respective config files. | ||
|
||
The `tracetest.config.yaml` file contains the basic setup of connecting Tracetest to the Postgres instance. | ||
|
||
```yaml | ||
postgres: | ||
host: postgres | ||
user: postgres | ||
password: postgres | ||
port: 5432 | ||
dbname: postgres | ||
params: sslmode=disable | ||
``` | ||
|
||
The `tracetest.provision.yaml` file defines the trace data store, set to Azure App Insights using the collector connection, meaning the traces will be sent to the OpenTelemetry collector to be processed and routed to both Tracetest and the Azure cloud. | ||
|
||
But how does Tracetest fetch traces? | ||
|
||
The OpenTelemetry collector is configured with the `azuremonitor` and the `otlp/tracetest` exporter and sending telemetry data to both the Azure Cloud and Tracetest. | ||
|
||
```yaml | ||
receivers: | ||
otlp: | ||
protocols: | ||
grpc: | ||
http: | ||
|
||
processors: | ||
batch: | ||
|
||
exporters: | ||
azuremonitor: | ||
instrumentation_key: ${INSTRUMENTATION_KEY} | ||
otlp/tracetest: | ||
endpoint: tracetest:4317 | ||
tls: | ||
insecure: true | ||
|
||
service: | ||
pipelines: | ||
traces/tracetest: | ||
receivers: [otlp] | ||
processors: [batch] | ||
exporters: [otlp/tracetest] | ||
traces/appinsights: | ||
receivers: [otlp] | ||
exporters: [azuremonitor] | ||
``` | ||
|
||
The `tracetest/tracetest.provision.yaml` file defines the trace data store, set to the Azure App Insights with the `collector` as connection type. | ||
|
||
```yaml | ||
type: DataStore | ||
spec: | ||
name: azureappinsights | ||
type: azureappinsights | ||
azureappinsights: | ||
connectionType: collector | ||
useAzureActiveDirectoryAuth: false | ||
``` | ||
|
||
## Run Both the Node.js App and Tracetest | ||
|
||
To start both the Node.js app and Tracetest, run this command: | ||
|
||
```bash | ||
docker compose -f ./docker-compose.yaml -f ./tracetest/docker-compose.yaml up -d | ||
``` | ||
|
||
This will start your Tracetest instance on `http://localhost:11633/`. Open it and start creating tests! | ||
Make sure to use the `http://app:3000/` URL in your test creation because your Node.js app and Tracetest are in the same network. | ||
|
||
## Learn More | ||
|
||
Please visit our [examples in GitHub](https://github.com/kubeshop/tracetest/tree/main/examples) and join our [Discord Community](https://discord.gg/8MtcMrQNbX) for more info! |
Oops, something went wrong.