OpenTelemetry middleware

OpenTelemetry middleware is a unified standard for the tracing, logging and metrics information. At the moment, only tracing information is stable and safe to use in production.

PHP Library

PHP library in the alpha stage at the moment: link.
Thanks to Brett McBride, he created a rr-otel PHP demo.

Original issue

Configuration

OT is a middleware plugin which currently working with gRPC and HTTP plugins.

Example configuration for HTTP:

yaml
version: "2.7"

rpc:
  listen: tcp://127.0.0.1:6001

server:
  command: "php otel_worker.php"
  env:
    - OTEL_SERVICE_NAME: php
    - OTEL_TRACES_EXPORTER: otlp
    - OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
    - OTEL_EXPORTER_OTLP_ENDPOINT: http://127.0.0.1:4318
    - OTEL_PHP_TRACES_PROCESSOR: simple
  relay: pipes

http:
  address: 127.0.0.1:15389
  middleware: [gzip, otel]
  otel:
    insecure: true
    compress: false
    exporter: otlp
    service_name: rr_test
    service_version: 1.0.0
    endpoint: 127.0.0.1:4318
  pool:
    num_workers: 10

logs:
  encoding: console
  level: debug
  mode: production

otel contains the following keys:

  1. insecure: boolean, default false. Use insecure endpoints (http/https) or insecure gRPC.
  2. compress: boolean, default false. Use gzip to compress the spans.
  3. exporter: string, default otlp. Provides functionality to emit telemetry to consumers. Possible values: otlp (used for new_relic, datadog), zipkin, stdout, jaeger or jaeger_agent to use a Jaeger agent UDP endpoint.
  4. custom_url: string, default empty. Used for the http client to override the default URL.
  5. client: string, default http. Client to send the spans. Possible values: http, grpc.
  6. endpoint: string, default localhost:4318. Consumer's endpoint.
  7. service_name: string, default: RoadRunner. User's service name.
  8. service_version: string, default 1.0.0. User's service version.
  9. headers: key-values map, default empty. User defined headers. new_relic api-key should be here.

OpenTelemetry environment variables

Env variables are used for the PHP process and can be passed via the server env variables.

yaml
server:
  command: "php otel_worker.php"
  env:
    - OTEL_SERVICE_NAME: php
    - OTEL_TRACES_EXPORTER: otlp
    - OTEL_EXPORTER_OTLP_PROTOCOL: http/protobuf
    - OTEL_EXPORTER_OTLP_ENDPOINT: http://127.0.0.1:4318
    - OTEL_PHP_TRACES_PROCESSOR: simple
  relay: pipes

Using with DataDog

yaml
#docker-composer.yml
version: "3.6"

services:
  collector:
    image: otel/opentelemetry-collector-contrib
    command: ["--config=/etc/otel-collector-config.yml"]
    volumes:
      - ./otel-collector-config.yml:/etc/otel-collector-config.yml
    ports:
      - "4318:4318"

Below, you will find an example of a collector configuration that sends data to Zipkin, Datadog, or New Relic:

yaml
#otel-collector-config.yml
receivers:
  otlp:
    protocols:
      grpc:
      http:

processors:
  batch:
    timeout: 1s

exporters:
  logging:
    loglevel: debug

  zipkin:
    endpoint: "http://zipkin:9411/api/v2/spans"

  datadog:
    api:
      site: datadoghq.eu
      key: ...

  otlp:
    endpoint: https://otlp.eu01.nr-data.net:443
    headers:
      api-key: ...

service:
  pipelines:
    traces:
      receivers: [ otlp ]
      processors: [ batch ]
      exporters: [ zipkin, datadog, otlp, logging ]

Then configure your php application:

dotenv
# OpenTelemetry
OTEL_SERVICE_NAME=php-blog
OTEL_TRACES_EXPORTER=otlp
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318 # Collector address
OTEL_PHP_TRACES_PROCESSOR=simple
Edit this page