-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agents: add metrics transform API to agent
Allow users to register a callback that will transform the metrics into a std::string that will be written to the endpoint. In order to do this some of the internals needed to be rewritten so that config() could be called from any thread. The internal usage of config() has been renamed since it will always be called from the StatsDAgent thread.
- Loading branch information
1 parent
0282a00
commit 7df5555
Showing
5 changed files
with
319 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <node.h> | ||
#include <v8.h> | ||
#include <uv.h> | ||
#include <nsolid.h> | ||
#include <statsd_agent.h> | ||
|
||
#include <assert.h> | ||
#include <atomic> | ||
|
||
using v8::FunctionCallbackInfo; | ||
using v8::String; | ||
using v8::Value; | ||
|
||
using node::nsolid::ProcessMetrics; | ||
using node::nsolid::ThreadMetrics; | ||
using node::nsolid::statsd::SharedStatsDAgent; | ||
using node::nsolid::statsd::StatsDAgent; | ||
|
||
SharedStatsDAgent agent; | ||
|
||
static std::vector<std::string> pm(ProcessMetrics::MetricsStor stor) { | ||
std::vector<std::string> sv; | ||
std::string ms = ""; | ||
#define V(Type, CName, JSName, MType, Unit) \ | ||
ms = "proc."; \ | ||
ms += #JSName ":"; \ | ||
ms += isnan(stor.CName) ? "null" : std::to_string(stor.CName); \ | ||
ms += "|g\n"; \ | ||
sv.push_back(ms); | ||
NSOLID_PROCESS_METRICS_NUMBERS(V) | ||
#undef V | ||
return sv; | ||
} | ||
|
||
static std::vector<std::string> tm(ThreadMetrics::MetricsStor stor) { | ||
std::vector<std::string> sv; | ||
std::string ms = ""; | ||
#define V(Type, CName, JSName, MType, Unit) \ | ||
ms = "thread."; \ | ||
ms += #JSName ":"; \ | ||
ms += isnan(stor.CName) ? "null" : std::to_string(stor.CName); \ | ||
ms += "|g|#threadId:"; \ | ||
ms += std::to_string(stor.thread_id); \ | ||
ms += "\n"; \ | ||
sv.push_back(ms); | ||
NSOLID_ENV_METRICS_NUMBERS(V) | ||
#undef V | ||
return sv; | ||
} | ||
|
||
static void SetAddHooks(const FunctionCallbackInfo<Value>& args) { | ||
assert(args[0]->IsString()); | ||
const String::Utf8Value url(args.GetIsolate(), args[0]); | ||
agent = node::nsolid::statsd::StatsDAgent::Create(); | ||
agent->config({{"statsd", *url}, | ||
{"interval", 3000}, | ||
{"pauseMetrics", false}}); | ||
agent->set_pmetrics_transform_cb(pm); | ||
agent->set_tmetrics_transform_cb(tm); | ||
} | ||
|
||
static void GetStatus(const FunctionCallbackInfo<Value>& args) { | ||
if (!agent) | ||
return args.GetReturnValue().SetNull(); | ||
args.GetReturnValue().Set(String::NewFromOneByte( | ||
args.GetIsolate(), | ||
reinterpret_cast<const uint8_t*>(agent->status().c_str()), | ||
v8::NewStringType::kNormal).ToLocalChecked()); | ||
} | ||
|
||
|
||
NODE_MODULE_INIT(/* exports, module, context */) { | ||
NODE_SET_METHOD(exports, "setAddHooks", SetAddHooks); | ||
NODE_SET_METHOD(exports, "getStatus", GetStatus); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
'targets': [{ | ||
'target_name': 'binding', | ||
'sources': [ 'binding.cc' ], | ||
'includes': ['../common.gypi'], | ||
'defines': [ 'NODE_WANT_INTERNALS=1' ], | ||
'include_dirs': [ | ||
'../../../src/', | ||
'../../../deps/nsuv/include/', | ||
'../../../agents/statsd/src/', | ||
], | ||
'target_defaults': { | ||
'default_configuration': 'Release', | ||
'configurations': { | ||
'Debug': { | ||
'defines': [ 'DEBUG', '_DEBUG' ], | ||
'cflags': [ '-g', '-O0', '-fstandalone-debug' ], | ||
} | ||
}, | ||
}, | ||
}], | ||
} |
Oops, something went wrong.