Integration of Ngnix and Datadog using Lua.
Nginx : Nginx is a web server which can also be used as a reverse proxy, load balancer and HTTP cache.
DataDog : Datadog is a monitoring service for cloud-scale applications, bringing together data from servers, databases, tools, and services to present a unified view of an entire stack.
Lua : Lua is a lightweight, multi-paradigm programming language designed primarily for embedded systems and clients.
- Install Datadog client at your server.
- Download ngx_lua_datadog from https://github.com/simplifi/ngx_lua_datadog and follow the steps
- Create one your lua script file which is used to send metrics , there import ngx_lua_datadog.
Now go to your ngnix.conf or config file in sites-enabled. Append the directives there..
lua_package_path :’$prefix/lua/?.lua;;’; // To define the path of lua files, Sets the Lua module search path used by script
log_by_lua_file lua/datadog/api_latency.lua; // It runs the lua script here script call and send metrices to datadog agent .
Helping Links :
https://github.com/openresty/lua-nginx-module#lua_package_path
https://github.com/simplifi/ngx_lua_datadog
Script file looks like :
local push_data (message) local conf = { host = "127.0.0.1", port = 8125, namespace = "Lua_Stats_App_Inventory", timeout = 1} local statsd_logger = require "ngx_lua_datadog" local logger, err = statsd_logger:new(conf) if err then ngx_log(ngx.ERR, "failed to create Statsd logger: ", err) end if message.status then logger:counter("app.uri.status_code", 1, 1, "status_code:"..message.status..",uri:"..maskUri) end end local message = {} local urt = ngx.var.upstream_response_time local sourceIp = ngx.var.remote_addr message["uri"] = ngx.var.uri message["urt"] = ngx.var.upstream_response_time message["sourceIp"] = ngx.var.remote_addr message["status"] = ngx.var.status message["status_code"] = ngx.var.status_code local ok, err = ngx.timer.at(0, push_data, message) if not ok then ngx.log(ngx.ERR, "failed to create timer: ", err)