Skip to content

This Lua library is a luajit ffi-based wrapper around newrelic agent SDK for the ngx_lua nginx module

License

Notifications You must be signed in to change notification settings

saks/lua-resty-newrelic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Name

lua-resty-newrelic - Lua newrelic SDK for the ngx_lua based on the C SDK.

Table of Contents

Status

This library is considered production ready. By default SDK will send all data using SSL. Also, SDK provides an option to turn on so called "HIGH_SECURITY" mode. So far, it looks like it doesn't work even if you set "NEWRELIC_ENABLE_HIGH_SECURITY" env var to "1".

Description

This Lua library is a luajit ffi-based wrapper around newrelic agent SDK for the ngx_lua nginx module:

This library can only be used with luajit, NOT lua, because uses luajit ffi.

Synopsis

    http {
        # you do not need the following line if you are using
        # the OpenResty bundle:
        lua_package_path "/path/to/resty-newrelic/lib/?.lua;;";

        env NEWRELIC_APP_LICENSE_KEY=<your newrelic lincense key>;
        env NEWRELIC_APP_NAME=<your newrelic application name>;

        init_worker_by_lua_block {
            require('resty.newrelic_agent').enable()
        }

        server {
            location /test {
                rewrite_by_lua_block {
                    require('resty.newrelic_agent').start_web_transaction()
                }

                # here you can use any directive that generates response body like: try_files,
                # proxy_pass, content_by_lua_*, etc.
                content_by_lua_block {
                    local newrelic = require 'resty.newrelic'
                    local newrelic_agent = require 'resty.newrelic_agent'
                    local nr_transaction_id = ngx.ctx.nr_transaction_id

                    local call_remote_host = function(uri)
                      ngx.log(ngx.ERR, 'Calling ' .. uri .. ' host...')
                      ngx.sleep(math.random())
                    end

                    local redis = require 'resty.redis'
                    local red = redis:new()
                    red:set_timeout(math.random(1000))

                    -- track database query
                    local redis_connect_segment_id = newrelic.begin_datastore_segment(
                    nr_transaction_id, newrelic.NEWRELIC_ROOT_SEGMENT, 'redis', 'connect')
                    local connect_ok, connect_err = red:connect('127.0.0.1', 6379)
                    newrelic.end_segment(nr_transaction_id, redis_connect_segment_id)

                    -- increment custom metric
                    if connect_ok then
                      newrelic.record_metric('redis_client/new_connect', 1)
                    else
                      -- log error to newrelic
                      newrelic_agent.notice_error('Failed to connect to redis',
                      connect_err, debug.traceback(), '\n')
                    end

                    -- track remote call
                    local uri = 'http://google.com'
                    local external_segment_id = newrelic.begin_external_segment(nr_transaction_id,
                    newrelic.NEWRELIC_ROOT_SEGMENT, uri, 'google home page')
                    call_remote_host(uri)
                    newrelic.end_segment(nr_transaction_id, external_segment_id)
                }

                log_by_lua_block {
                    require('resty.newrelic_agent').finish_web_transaction()
                }
            }
        }
    }

For experimentation purposes you can play with docker container in the examples directory.

NEWRELIC_APP_LICENSE_KEY=<your license key> docker-compose run --rm server
IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' examples_server_run_1)
curl $IP/test/1

Back to TOC

Installation

If you are using the OpenResty bundle (http://openresty.org ), then just download lua file newrelic.lua and newrelic_agent.lua files to the directiory where nginx will find it. Another way to install is to use luarocks package. And you can just use it in your Lua code, as in

    local newrelic = require "resty.newrelic"
    ...

If you are using your own nginx + ngx_lua build, then you need to configure the lua_package_path directive to add the path of your resty-newrelic source tree to ngx_lua's LUA_PATH search path, as in

    # nginx.conf
    http {
        lua_package_path "/path/to/resty-newrelic/lib/?.lua;;";
        ...
    }

Ensure that the system account running your Nginx ''worker'' proceses have enough permission to read the .lua file.

Back to TOC

Bugs and Patches

Please report bugs or submit patches by

  1. creating a ticket on the GitHub Issue Tracker,

Back to TOC

Author

Aliaksandr "saksmlz" Rahalevich <saksmlz__at__gmail.com>.

Back to TOC

Copyright and License

This module is licensed under the BSD license.

Copyright (C) 2016, by Aliaksandr Rahalevich (saksmlz) <saksmlz__at__gmail.com>.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Back to TOC

About

This Lua library is a luajit ffi-based wrapper around newrelic agent SDK for the ngx_lua nginx module

Resources

License

Stars

Watchers

Forks

Packages

No packages published