Skip to content

Latest commit

 

History

History
412 lines (324 loc) · 14.4 KB

running-tracetest-with-dynatrace.mdx

File metadata and controls

412 lines (324 loc) · 14.4 KB
id title description hide_table_of_contents keywords image
running-tracetest-with-dynatrace
OpenTelemetry Demo and Dynatrace
Quick start how to configure the OpenTelemetry Demo to use Tracetest for enhancing your E2E and integration tests with trace-based testing with Dynatrace as a trace data store.
false
tracetest
trace-based testing
observability
distributed tracing
testing

:::note Check out the source code on GitHub here. :::

Tracetest is a testing tool based on OpenTelemetry 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.

Dynatrace is a unified Observability and security platform that allows you to monitor and secure your full stack on one AI-powered data platform. From infrastructure and application observability to realtime security analytics and protection, digital experience monitoring and business analytics. All underpinned by a composable app-based platform with a custom Observability-driven workflow engine.

OpenTelemetry Demo v1.3.0 with Dynatrace, OpenTelemetry and Tracetest

This is a simple sample app on how to configure the OpenTelemetry Demo v1.3.0 to use Tracetest for enhancing your E2E and integration tests with trace-based testing and Dynatrace as a trace data store.

Prerequisites

You will need Docker and Docker Compose installed on your machine to run this sample app! Additionally, you will need a Dynatrace account and an API token. Sign up for a free Dynatrace trial.

Project Structure

The project is built with Docker Compose. It contains two distinct docker-compose.yaml files.

1. OpenTelemetry Demo

The docker-compose.yaml file and .env file in the root directory are for the OpenTelemetry Demo.

2. Tracetest

The docker-compose.yaml file, collector.config.yaml, tracetest-provision.yaml, and tracetest-config.yaml in the tracetest directory are for setting up Tracetest and the OpenTelemetry Collector.

The tracetest directory is self-contained and will run all the prerequisites for enabling OpenTelemetry traces and trace-based testing with Tracetest, as well as routing all traces the OpenTelemetry Demo generates to Dynatrace.

Docker Compose Network

All services in the docker-compose.yaml are on the same network, defined by the networks section on each file, and will be reachable by hostname from within other services. E.g. tracetest:4317 in the collector.config.yaml will map to the tracetest service, where port 4317 is the port where Tracetest accepts traces.

OpenTelemetry Demo

The OpenTelemetry Demo is a sample microservice-based app with the purpose to demo how to correctly set up OpenTelemetry distributed tracing.

Read more about the OpenTelemetry Demo here.

The docker-compose.yaml contains 14 services for the demo and 3 supporting dependent services.

To start the OpenTelemetry Demo by itself, run this command:

docker compose up

This will start the OpenTelemetry Demo. Open up http://localhost:8084 to make sure it's working. But, you're not sending the traces anywhere.

Let's fix this by configuring Tracetest and the OpenTelemetry Collector to forward trace data to both Dynatrace and Tracetest.

Tracetest

The docker-compose.yaml in the tracetest directory is configured with three services.

  • Postgres - Postgres is a prerequisite for Tracetest to work. It stores trace data when running trace-based tests.
  • OpenTelemetry Collector - A vendor-agnostic implementation of how to receive, process and export telemetry data. To support sending traces to Dynatrace, we are using the contrib version, which contains vendor-related code.
  • Tracetest - Trace-based testing that generates end-to-end tests automatically from traces.
version: "3.9"

networks:
  default:
    name: opentelemetry-demo
    driver: bridge

services:
  tracetest:
    restart: unless-stopped
    image: kubeshop/tracetest:${TAG:-latest}
    container_name: tracetest
    platform: linux/amd64
    ports:
      - 11633:11633
    extra_hosts:
      - "host.docker.internal:host-gateway"
    volumes:
      - type: bind
        source: ./tracetest/tracetest-config.yaml
        target: /app/tracetest.yaml
      - type: bind
        source: ./tracetest/tracetest-provision.yaml
        target: /app/provisioning.yaml
    command: --provisioning-file /app/provisioning.yaml
    healthcheck:
      test: ["CMD", "wget", "--spider", "localhost:11633"]
      interval: 1s
      timeout: 3s
      retries: 60
    depends_on:
      tt-postgres:
        condition: service_healthy
      otel-collector:
        condition: service_started
    environment:
      TRACETEST_DEV: ${TRACETEST_DEV}

  tt-postgres:
    image: postgres:14
    container_name: tt-postgres
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
    healthcheck:
      test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
      interval: 1s
      timeout: 5s
      retries: 60

  otel-collector:
    image: otel/opentelemetry-collector-contrib:0.82.0
    container_name: otel-collector
    restart: unless-stopped
    command:
      - "--config"
      - "/otel-local-config.yaml"
    volumes:
      - ./tracetest/collector.config.yaml:/otel-local-config.yaml

Tracetest depends on both Postgres and the OpenTelemetry Collector. Both Tracetest and the OpenTelemetry Collector require config files to be loaded via a volume. The volumes are mapped from the root directory into the tracetest directory and the respective config files.

To start both the OpenTelemetry Demo and Tracetest we will run this command:

docker-compose -f docker-compose.yaml -f tracetest/docker-compose.yaml up

The tracetest-config.yaml file contains the basic setup of connecting Tracetest to the Postgres instance and telemetry exporter. The exporter is set to the OpenTelemetry Collector.

# tracetest-config.yaml

---
postgres:
  host: tt-postgres
  user: postgres
  password: postgres
  port: 5432
  dbname: postgres
  params: sslmode=disable

telemetry:
  exporters:
    collector:
      serviceName: tracetest
      sampling: 100
      exporter:
        type: collector
        collector:
          endpoint: otel-collector:4317

server:
  telemetry:
    exporter: collector
    applicationExporter: collector

The tracetest-provision.yaml file contains the setup of the demo APIs that Tracetest can use as an example for tests, the polling profiles that say how Tracetest should fetch traces from the data store and the configuration for the data store, in our case, Dynatrace.

---
type: Demo
spec:
  name: "OpenTelemetry Shop"
  enabled: true
  type: otelstore
  opentelemetryStore:
    frontendEndpoint: http://frontend:8084
    productCatalogEndpoint: productcatalogservice:3550
    cartEndpoint: cartservice:7070
    checkoutEndpoint: checkoutservice:5050

---
type: PollingProfile
spec:
  name: Default
  strategy: periodic
  default: true
  periodic:
    retryDelay: 5s
    timeout: 10m

---
type: DataStore
spec:
  name: Dynatrace
  type: dynatrace

---
type: TestRunner
spec:
  id: current
  name: default
  requiredGates:
    - analyzer-score
    - test-specs

Sending Traces to Tracetest and Dynatrace

The collector.config.yaml explains that. It receives traces via either grpc or http. Then, exports them to Tracetest's OTLP endpoint tracetest:4317 in one pipeline, and to Dynatrace in another.

Make sure to add your Dynatrace API Key to the otlp exporter (needs the openTelemetryTrace.ingest permission).

receivers:
  otlp:
    protocols:
      http:
      grpc:
  hostmetrics:
    collection_interval: 10s
    scrapers:
      paging:
        metrics:
          system.paging.utilization:
            enabled: true
      cpu:
        metrics:
          system.cpu.utilization:
            enabled: true
      disk:
      filesystem:
        metrics:
          system.filesystem.utilization:
            enabled: true
      load:
      memory:
      network:
      processes:
  # The prometheus receiver scrapes metrics needed for the OpenTelemetry Collector Dashboard.
  prometheus:
    config:
      scrape_configs:
      - job_name: 'otelcol'
        scrape_interval: 10s
        static_configs:
        - targets: ['0.0.0.0:8888']

processors:
  batch: # this configuration is needed to guarantee that the data is sent correctly to Dynatrace
    send_batch_max_size: 100
    send_batch_size: 10
    timeout: 10s

exporters:
  # OTLP for Tracetest
  otlp/tracetest:
    endpoint: tracetest:4317 # Send traces to Tracetest.
                              # Read more in docs here: https://docs.tracetest.io/configuration/connecting-to-data-stores/opentelemetry-collector
    tls:
      insecure: true
  # OTLP for Dynatrace
  otlphttp/dynatrace:
    endpoint: https://abc12345.live.dynatrace.com/api/v2/otlp
    headers:
      Authorization: "Api-Token dt0c01.sample.secret"  # Requires "openTelemetryTrace.ingest" permission

service:
  pipelines:
    traces/tracetest:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlp/tracetest]
    traces/dynatrace:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlphttp/dynatrace]
    metrics:
      receivers: [hostmetrics, otlp]
      processors: [batch]
      exporters: [otlphttp/dynatrace]

Run Both the OpenTelemetry Demo App and Tracetest

To start both OpenTelemetry and Tracetest, run this command:

docker-compose -f docker-compose.yaml -f tracetest/docker-compose.yaml up

:::note Heads up! Please note starting the demo for the first time will take a few minutes. :::

This will start your Tracetest instance on http://localhost:11633/.

Open the URL and start creating tests in the Web UI! Make sure to use the endpoints within your Docker network like http://frontend:8084/ when creating tests.

This is because your OpenTelemetry Demo and Tracetest are in the same network.

Note: View the demo section in the tracetest.config.yaml to see which endpoints from the OpenTelemetry Demo are available for running tests.

Here's a sample of a failed test run, which happens if you add this assertion:

attr:tracetest.span.duration  < 10ms

Increasing the duration to a more reasonable 500ms will make the test pass.

Run Tracetest Tests with the Tracetest CLI

First, install the CLI. Then, configure the CLI:

tracetest configure --server-url http://localhost:11633

Once configured, you can run a test against the Tracetest instance via the terminal.

Check out the http-test.yaml file.

# http-test.yaml

type: Test
spec:
  id: JBYAfKJ4R
  name: OpenTelemetry Shop - List Products
  description: List Products available on OTel shop
  trigger:
    type: http
    httpRequest:
      url: http://frontend:8084/api/products
      method: GET
      headers:
      - key: Content-Type
        value: application/json
  specs:
  - selector: span[tracetest.span.type="general" name="Tracetest trigger"]
    assertions:
    - attr:tracetest.response.status   =   200
    - attr:tracetest.span.duration  <  10ms
  - selector: span[tracetest.span.type="rpc" name="grpc.hipstershop.ProductCatalogService/ListProducts"]
    assertions:
    - attr:rpc.grpc.status_code = 0
  - selector: span[tracetest.span.type="rpc" name="hipstershop.ProductCatalogService/ListProducts"
      rpc.system="grpc" rpc.method="ListProducts" rpc.service="hipstershop.ProductCatalogService"]
    assertions:
    - attr:rpc.grpc.status_code = 0

This file defines a test the same way you would through the Web UI.

To run the test, run this command in the terminal:

tracetest run test -f ./http-test.yaml

This test will fail just like the sample above due to the attr:tracetest.span.duration < 10ms assertion.

✘ OpenTelemetry Shop - List Products (http://localhost:11633/test/JBYAfKJ4R/run/1/test) - trace id: b9db3e805490f6e1d9aff7c48100d367
	✘ span[tracetest.span.type="general" name="Tracetest trigger"]
		✘ #bf9abd7861371975
			✔ attr:tracetest.response.status   =   200 (200)
			✘ attr:tracetest.span.duration  <  10ms (1.3s) (http://localhost:11633/test/JBYAfKJ4R/run/1/test?selectedAssertion=0&selectedSpan=bf9abd7861371975)
	✔ span[tracetest.span.type="rpc" name="grpc.hipstershop.ProductCatalogService/ListProducts"]
		✔ #52a4bd4cbace9c4b
			✔ attr:rpc.grpc.status_code = 0 (0)
	✔ span[tracetest.span.type="rpc" name="hipstershop.ProductCatalogService/ListProducts" rpc.system="grpc" rpc.method="ListProducts" rpc.service="hipstershop.ProductCatalogService"]
		✔ #533d2199d7e26437
			✔ attr:rpc.grpc.status_code = 0 (0)

	✘ Required gates
		✔ analyzer-score
		✘ test-specs

If you edit the duration as in the Web UI example above, the test will pass!

View Trace Spans Over Time in Dynatrace

All distributed traces (whether generated by OpenTelemetry or the OneAgent) are available in Dynatrace in the distributed traces app.

You can also drill down into a particular trace.

The combination of Dynatrace and Tracetest is extremely powerful. Ingest your traces to Dynatrace for long term storage at planetary scale and encourage your developers to incorporate trace-based testing into their workflows with Tracetest.

Learn more

Feel free to check out our examples in GitHub and join our Slack Community for more info!