I've tried to find clarification about the best practices for configuring lua modules but haven't been able to find anything definitive. For this example, I'm using a Datadog module that needs to be configured with a few settings before it can be used. I'm under the impression that you want everything in each location block so something like this:
location /location1 {
access_log off;
content_by_lua_block {
local resty_dogstatsd = require('resty_dogstatsd')
local dogstatsd = resty_dogstatsd.new({
statsd = {
host = "127.0.0.1",
port = 8125,
namespace = "location_test",
},
tags = {
"application:lua",
},
})
dogstatsd:increment('request', 1, 0.25, {'endpoint:location1'})
ngx.say("hello from location 1")
}
}
location /location2 {
access_log off;
content_by_lua_block {
local resty_dogstatsd = require('resty_dogstatsd')
local dogstatsd = resty_dogstatsd.new({
statsd = {
host = "127.0.0.1",
port = 8125,
namespace = "location_test",
},
tags = {
"application:lua",
},
})
dogstatsd:increment('request', 1, 0.25, {'endpoint:location2'})
ngx.say("hello from location 2")
}
}
Is this correct? It feels like there should be a way to share the initialization code between locations but I understand there may be issues that I'm not seeing that would prohibit this.