Skip to content

OpenResty

Configuration

Configure DDTrace

Download the Tracing module

Visit https://github.com/DataDog/nginx-datadog/releases, and download the module openresty-ngx_http_datadog_module-<processor architecture>-<version>.so.tgz that matches your current processor architecture and version from the Assets section.

Install Tracing

  • Create the module path
cd /usr/local/openresty/nginx
mkdir modules && cd $_
  • Extract
root@root:/usr/local/openresty/nginx/modules$  tar xzvf openresty-ngx_http_datadog_module-<processor architecture>-<version>.so.tgz

Configure Nginx

Add the following content to /usr/local/openresty/nginx/samples/nginx.conf:

# Load the module
load_module modules/ngx_http_datadog_module.so;

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    # Configure Datakit URL
    datadog_agent_url http://localhost:9529;
    # Configure service name
    datadog_service_name openresty-demo;
    # Configure environment name
    datadog_environment test;
    # Configure version name
    datadog_version 1.0.0;
    # Configure sampling rate 0-1.0
    datadog_sample_rate 1.0;
    # Configure tags
    datadog_tag foo bar;

    include       mime.types;
    default_type  application/octet-stream;

    # Add trace_id and span_id to logs via $datadog_trace_id and $datadog_span_id, both in hexadecimal
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      '"$datadog_trace_id" "$datadog_span_id"';

    access_log  logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Restart OpenResty

Skipped

Enable DataKit Collection

  • Enable ddtrace
cd /usr/local/datakit/conf.d/samples/
cp ddtrace.conf.sample ddtrace.conf
  • Restart DataKit
datakit service -R

Configure OpenTelemetry

Build the ngx_otel_module

  • Install build tools and dependencies
sudo apt update

sudo apt install cmake build-essential libssl-dev zlib1g-dev libpcre3-dev

sudo apt install pkg-config libc-ares-dev libre2-dev # for gRPC
  • Determine the Nginx version and compilation configuration used by OpenResty
openresty -V

Example output:

openresty -V
#nginx version: openresty/1.25.3.2
#built with OpenSSL 1.1.1w  11 Sep 2023
#TLS SNI support enabled
#configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' --add-module=../ngx_devel_kit-0.3.3 ············
  • Download the corresponding Nginx source code
git clone https://github.com/nginx/nginx.git
cd nginx
git checkout {OPENRESTY_NGINX_VERSION}  # Switch to the corresponding version Tag

Replace {OPENRESTY_NGINX_VERSION} with the corresponding version Tag. For example, for version 1.25.3.2, the corresponding version Tag is release-1.25.3.

  • Configure Nginx compilation parameters

Execute the following command in the root directory of the nginx source code

./auto/configure {NGINX_CONFIGURE_ARGUMENTS} --with-compat 

{NGINX_CONFIGURE_ARGUMENTS} is the content of the obtained configure arguments, and remove the parameters related to --add-module.

  • Download the ngx_otel_module source code
cd ..
git clone https://github.com/nginxinc/nginx-otel.git
cd /nginx-otel
  • Compile the ngx_otel_module

Create and enter the build directory to compile. After compilation, the ngx_otel_module.so file will be generated in the build directory.

mkdir build
cd build
cmake -DNGX_OTEL_NGINX_BUILD_DIR=/nginx/objs ..
make

Enable the ngx_otel_module

  • Copy the ngx_otel_module.so file to the nginx modules directory
mkdir -p /usr/local/openresty/nginx/modules/
cp ngx_otel_module.so /usr/local/openresty/nginx/modules/
  • Configure nginx.conf

To enable APM for OpenResty, you need to load the ngx_otel_module and add configuration items in the Nginx main configuration file /usr/local/openresty/nginx/conf/nginx.conf. Note that the ngx_otel_module currently only supports gRPC reporting, not HTTP reporting. For more parameter configuration information about the ngx_otel_module, please refer to the ngx_otel_module documentation

load_module modules/ngx_otel_module.so; # Load the ngx_otel_module
...
http {
    ...

    otel_exporter {
        endpoint "127.0.0.1:4317"; # gRPC endpoint
        #header Authentication "${GRPC_TOKEN}"; # The authentication Token obtained in the prerequisites
    }

    otel_trace on;                     # Enable APM
    otel_service_name openresty-otel;  # Application name
    otel_trace_context propagate;      # Inject Trace context into downstream services
    ...
}
  • Check if the nginx.conf configuration is correct
/usr/local/openresty/nginx/sbin/nginx -t
  • Reload the configuration
# Method 1: Use the openresty command to reload the configuration
openresty -s reload

# Method 2: Use the nginx command to reload the configuration
/usr/local/openresty/nginx/sbin/nginx -s reload

Enable DataKit Collection

  • Enable the OpenTelemetry collector
# Enter the collector configuration file directory
cd /usr/local/datakit/conf.d/samples
# Enable the configuration file
cp opentelemetry.conf.sample opentelemetry.conf
  • Restart Datakit
datakit service -R

Feedback

Is this page helpful? ×