load_module modules/ngx_otel_module.so;

events {}

http {
    otel_exporter {
        endpoint otel-collector:4317;
    }

    otel_service_name nginx;

    map $status $log_level {
        ~^[45] error;
        default info;
    }

    map $status $log_target {
        ~^[45] nginx.request_error;
        default nginx.access;
    }

    log_format otel_request 'ts=$time_iso8601 level=$log_level target=$log_target '
                            'client.address=$remote_addr method=$request_method path="$uri" '
                            'status=$status request_time=$request_time '
                            'connection=$connection '
                            'upstream_addr="$upstream_addr" upstream_status="$upstream_status" '
                            'upstream_response_time="$upstream_response_time" '
                            'referer="$http_referer" user_agent="$http_user_agent" '
                            'trace_id=$otel_trace_id span_id=$otel_span_id parent_id=$otel_parent_id';

    access_log /dev/stdout otel_request;
    error_log /dev/stderr warn;

    upstream sqlpage {
        server sqlpage:8080;
    }

    server {
        listen 80;

        location / {
            otel_trace on;
            otel_span_name "$request_method $uri";
            otel_trace_context propagate;
            proxy_pass http://sqlpage;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}
