DataDog integration with Nginx using Lua

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 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 :

Script file looks like :

local push_data (message) 
 local conf = { host = "", 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)

 if message.status then
 logger:counter("app.uri.status_code", 1, 1, "status_code:"..message.status..",uri:"..maskUri)

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 =, push_data, message)
if not ok then
 ngx.log(ngx.ERR, "failed to create timer: ", err)



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s