Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attempt to call upvalue 'Statsd' (a table value) #9

Closed
ramamoob opened this issue Oct 22, 2015 · 11 comments
Closed

attempt to call upvalue 'Statsd' (a table value) #9

ramamoob opened this issue Oct 22, 2015 · 11 comments

Comments

@ramamoob
Copy link

Hi,
I am new to lua. I am following the steps provided in README.md. Whenever I try to run my code I am getting the runtime error as mentioned above.

Here is my code snippet.

local Statsd = require "statsd"
statsd_obj = Statsd({namespace = "test.stats"})

statsd_obj:increment(message["request"]["uri"])

Can you please help?

From the error it seems like I am trying to call Statsd as a function where as it is a table.
How can I call the Statsd as a function?

@moteus
Copy link
Contributor

moteus commented Oct 23, 2015

I can suggest you use different statsd library. Because this library return function
Try figureout which one file loaded by local Statsd = require "statsd" and checkout source.
I use this library without any issues to collect system info from ~10 servers.

@ramamoob
Copy link
Author

Thanks for your response. Can you please let me know how you are instantiating the statsd object and calling its methods in your code.

@moteus
Copy link
Contributor

moteus commented Oct 23, 2015

I think this is almost complite example. I run it as service.

@ramamoob
Copy link
Author

Thanks for your example. I am still running into the same issue. I followed your guidance as shown in your example. Below is my code snippet.

local CONFIG = {

      STATSD = {
        NAMESPACE = "test.stats";
        HOST = "127.0.0.1";
        PORT = "8125";
        SEND_TABLE = false;
     };
  };

local Statsd = assert(require "statsd"
  {
      host= CONFIG.STATSD.HOST;
      port = CONFIG.STATSD.PORT;
      namespace = CONFIG.STATSD.NAMESPACE;
  })

  Statsd:increment(message["status"], 1)

During runtime I get this error where I am trying to create the local variable "Statsd".

 attempt to call a table value

Do you know what I am doing wrong?

@moteus
Copy link
Contributor

moteus commented Oct 23, 2015

You use differen library i think

@ramamoob
Copy link
Author

This is the library I am running.

statsd 3.0.2-1 - Statsd client.

Statsd client for Lua 5.1+. Uses the luasocket library for UDP.

License:        MIT/X11
Homepage:       https://github.com/stvp/lua-statsd-client
Installed in:   /usr/local

Modules:
        statsd (/usr/local/share/lua/5.1/statsd.lua)

Depends on:
        luasocket

@moteus
Copy link
Contributor

moteus commented Oct 23, 2015

  1. try find out what table contain. e.g. print all key/values
    for k,v in pairs(statsd) do print(k,v) end
  2. try find all statusd.lua files and findout which one loaded when you call require.
    (it also can be .so/.dll)
    Contains of statsd can help in this.
    search in directories which you have in package.path/cpath

@ramamoob
Copy link
Author

I checked what is in the table. I am printing the key and the type of the value. The output here is from just require 'statsd'.

2015/10/23 19:47:20 [error] 20884#0: [lua] log.lua:38: Type of statsd: table, context: ngx.timer, client: X.X.X.X, server: 0.0.0.0:8000
2015/10/23 19:47:20 [error] 20884#0: [lua] log.lua:39: incr - function, context: ngx.timer, client: X.X.X.X, server: 0.0.0.0:8000
2015/10/23 19:47:20 [error] 20884#0: [lua] log.lua:39: flush - function, context: ngx.timer, client: X.X.X.X, server: 0.0.0.0:8000
2015/10/23 19:47:20 [error] 20884#0: [lua] log.lua:39: buffer - table, context: ngx.timer, client: X.X.X.X, server: 0.0.0.0:8000
2015/10/23 19:47:20 [error] 20884#0: [lua] log.lua:39: count - function, context: ngx.timer, client: X.X.X.X, server: 0.0.0.0:8000
2015/10/23 19:47:20 [error] 20884#0: [lua] log.lua:39: register - function, context: ngx.timer, client: X.X.X.X, server: 0.0.0.0:8000
2015/10/23 19:47:20 [error] 20884#0: [lua] log.lua:39: time - function, context: ngx.timer, client: X.X.X.X, server: 0.0.0.0:8000

I also see that the statsd.lua is loaded from the location

/usr/local/share/lua/5.1/statsd.lua

It is the exact same code as the one in this repo.

@ramamoob
Copy link
Author

I am able to set the table values like this

local statsd = require "statsd"
  statsd["host"] = CONFIG.STATSD.HOST
  statsd["port"] = CONFIG.STATSD.PORT
  statsd["namespace"] = CONFIG.STATSD.NAMESPACE

When I try to call the function

statsd.increment(message["status"], 1)

I get the error method

attempt to call field 'increment' (a nil value)

Instead if I do this

statsd:increment(message["status"],1)

then I get the error

attempt to call method 'increment' (a nil value)

@ramamoob
Copy link
Author

I think I made some progress. Right now I can confirm that it is loading the correct statsd.lua. But is failing within that code on line #22.

2015/10/23 20:45:42 [error] 4187#0: lua entry thread aborted: runtime error: /usr/local/share/lua/5.1/statsd.lua:22: attempt to index local 'stat' (a nil value)
stack traceback:
coroutine 0:
        /usr/local/share/lua/5.1/statsd.lua: in function 'make_statsd_message'
        /usr/local/share/lua/5.1/statsd.lua:67: in function 'increment'

Which is

https://github.com/stvp/lua-statsd-client/blob/master/src/statsd.lua#L22

@ramamoob
Copy link
Author

ramamoob commented Nov 1, 2015

you can close this case. Thanks for all your help.

@ramamoob ramamoob closed this as completed Nov 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants