Skip to content

Commit

Permalink
agents: improve appName support
Browse files Browse the repository at this point in the history
Add `app` field to ZMQ messages.
Update appName when it's dynamically changed.
  • Loading branch information
santigimeno committed Feb 4, 2025
1 parent 0246f46 commit 6236a21
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 25 deletions.
14 changes: 14 additions & 0 deletions agents/grpc/src/grpc_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "absl/log/initialize.h"
#include "opentelemetry/sdk/metrics/data/metric_data.h"
#include "opentelemetry/sdk/metrics/export/metric_producer.h"
#include "opentelemetry/sdk/resource/semantic_conventions.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_client.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_client_factory.h"
#include "opentelemetry/exporters/otlp/otlp_grpc_exporter.h"
Expand All @@ -33,6 +34,7 @@ using opentelemetry::sdk::metrics::ResourceMetrics;
using opentelemetry::sdk::metrics::ScopeMetrics;
using opentelemetry::sdk::resource::Resource;
using opentelemetry::sdk::resource::ResourceAttributes;
using opentelemetry::sdk::resource::SemanticConventions::kServiceName;
using opentelemetry::sdk::trace::Recordable;
using opentelemetry::v1::exporter::otlp::OtlpGrpcClient;
using opentelemetry::v1::exporter::otlp::OtlpGrpcClientFactory;
Expand Down Expand Up @@ -1110,6 +1112,18 @@ int GrpcAgent::config(const json& config) {
setup_blocked_loop_hooks();
}

if (utils::find_any_fields_in_diff(diff, { "/app" })) {
auto it = config_.find("app");
if (it != config_.end()) {
std::string app_name = it->get<std::string>();
ResourceAttributes attrs = {
{ kServiceName, app_name },
};

USE(otlp::UpdateResource(std::move(attrs)));
}
}

// Configure tracing flags
if (trace_flags_ == 0 ||
utils::find_any_fields_in_diff(diff, tracing_fields)) {
Expand Down
13 changes: 13 additions & 0 deletions agents/otlp/src/otlp_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace trace = OPENTELEMETRY_NAMESPACE::trace;
namespace resource = sdk::resource;
namespace instrumentationscope = sdk::instrumentationscope;
namespace detail = trace::propagation::detail;
using resource::ResourceAttributes;
using resource::SemanticConventions::kServiceName;
using resource::SemanticConventions::kServiceInstanceId;
using resource::SemanticConventions::kServiceVersion;
Expand Down Expand Up @@ -170,6 +171,18 @@ int OTLPAgent::config(const nlohmann::json& config) {
config_otlp_agent(config_);
}

if (utils::find_any_fields_in_diff(diff, { "/app" })) {
auto it = config_.find("app");
if (it != config_.end()) {
std::string app_name = it->get<std::string>();
ResourceAttributes attrs = {
{ kServiceName, app_name },
};

USE(otlp::UpdateResource(std::move(attrs)));
}
}

// Configure tracing flags
if (span_collector_ == nullptr ||
utils::find_any_fields_in_diff(diff, tracing_fields)) {
Expand Down
3 changes: 2 additions & 1 deletion agents/otlp/src/otlp_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ Resource* UpdateResource(ResourceAttributes&& attrs) {
// value "unknown_service". (See Resource::Create() method in the SDK).
auto resource = GetResource();
auto attributes = resource->GetAttributes();
if (attributes.find(kServiceName) != attributes.end()) {
if (attributes.find(kServiceName) != attributes.end() &&
attrs.find(kServiceName) == attrs.end()) {
attrs.SetAttribute(kServiceName,
std::get<std::string>(attributes[kServiceName]));
}
Expand Down
23 changes: 23 additions & 0 deletions agents/zmq/src/zmq_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ constexpr size_t span_msg_q_min_size = 200;

const char MSG_1[] = "{"
"\"agentId\":\"%s\""
",\"app\":\"%s\""
",\"requestId\": \"%s\""
",\"command\":\"%s\""
",\"recorded\":{\"seconds\":%" PRIu64",\"nanoseconds\":%" PRIu64"}"
Expand All @@ -89,6 +90,7 @@ const char MSG_1[] = "{"

const char MSG_2[] = "{"
"\"agentId\":\"%s\""
",\"app\":\"%s\""
",\"requestId\": null"
",\"command\":\"%s\""
",\"recorded\":{\"seconds\":%" PRIu64",\"nanoseconds\":%" PRIu64"}"
Expand All @@ -100,6 +102,7 @@ const char MSG_2[] = "{"

const char MSG_3[] = "{"
"\"agentId\":\"%s\""
",\"app\":\"%s\""
",\"requestId\": \"%s\""
",\"command\":\"%s\""
",\"duration\":%" PRIu64
Expand All @@ -111,6 +114,7 @@ const char MSG_3[] = "{"

const char MSG_4[] = "{"
"\"agentId\":\"%s\""
",\"app\":\"%s\""
",\"requestId\": \"%s\""
",\"command\":\"%s\""
",\"recorded\":{\"seconds\":%" PRIu64",\"nanoseconds\":%" PRIu64"}"
Expand All @@ -120,13 +124,15 @@ const char MSG_4[] = "{"

const char MSG_5[] = "{"
"\"agentId\":\"%s\""
",\"app\":\"%s\""
",\"recorded\":{\"seconds\":%" PRIu64",\"nanoseconds\":%" PRIu64"}"
",\"version\":%d"
",\"error\":{\"message\":\"%s\",\"code\":%d}"
"}";

const char MSG_6[] = "{"
"\"agentId\":\"%s\""
",\"app\":\"%s\""
",\"command\":\"exit\""
",\"exit_code\":%d"
",\"version\":%d"
Expand All @@ -136,6 +142,7 @@ const char MSG_6[] = "{"

const char MSG_7[] = "{"
"\"agentId\":\"%s\""
",\"app\":\"%s\""
",\"command\":\"exit\""
",\"exit_code\":%d"
",\"version\":%d"
Expand Down Expand Up @@ -1077,6 +1084,7 @@ int ZmqAgent::send_command_message(const char* command,
msg_size_,
MSG_2,
agent_id_.c_str(),
app_name_.c_str(),
command,
std::get<0>(recorded),
std::get<1>(recorded),
Expand All @@ -1090,6 +1098,7 @@ int ZmqAgent::send_command_message(const char* command,
msg_size_,
MSG_1,
agent_id_.c_str(),
app_name_.c_str(),
request_id,
command,
std::get<0>(recorded),
Expand Down Expand Up @@ -1271,6 +1280,13 @@ int ZmqAgent::config(const json& config) {
setup_blocked_loop_hooks();
}

if (utils::find_any_fields_in_diff(diff, { "/app" })) {
auto it = config_.find("app");
if (it != config_.end()) {
app_name_ = it->get<std::string>();
}
}

// Don't config other endpoints if command handle is not to be configured
if (command_handle_ != nullptr) {
if (ZmqHandle::needs_reset(diff, ZmqDataHandle::restart_fields)) {
Expand Down Expand Up @@ -1555,6 +1571,7 @@ void ZmqAgent::send_error_message(const std::string& msg,
msg_size_,
MSG_5,
agent_id_.c_str(),
app_name_.c_str(),
std::get<0>(recorded),
std::get<1>(recorded),
version_,
Expand All @@ -1571,6 +1588,7 @@ void ZmqAgent::send_error_message(const std::string& msg,
msg_size_,
MSG_5,
agent_id_.c_str(),
app_name_.c_str(),
std::get<0>(recorded),
std::get<1>(recorded),
version_,
Expand All @@ -1594,6 +1612,7 @@ int ZmqAgent::send_error_command_message(const std::string& req_id,
msg_size_,
MSG_4,
agent_id_.c_str(),
app_name_.c_str(),
req_id.c_str(),
command.c_str(),
std::get<0>(recorded),
Expand All @@ -1612,6 +1631,7 @@ int ZmqAgent::send_error_command_message(const std::string& req_id,
msg_size_,
MSG_4,
agent_id_.c_str(),
app_name_.c_str(),
req_id.c_str(),
command.c_str(),
std::get<0>(recorded),
Expand Down Expand Up @@ -1664,6 +1684,7 @@ void ZmqAgent::send_exit() {
msg_size_,
MSG_7,
agent_id_.c_str(),
app_name_.c_str(),
exit_code,
version_,
profile);
Expand All @@ -1675,6 +1696,7 @@ void ZmqAgent::send_exit() {
msg_size_,
MSG_6,
agent_id_.c_str(),
app_name_.c_str(),
exit_code,
version_,
jmsg.dump().c_str(),
Expand Down Expand Up @@ -2064,6 +2086,7 @@ void ZmqAgent::do_got_prof(ProfileType type,
msg_size_,
MSG_3,
agent_id_.c_str(),
app_name_.c_str(),
prof_stor.req_id.c_str(),
cmd,
uv_now(&loop_) - prof_stor.timestamp,
Expand Down
1 change: 1 addition & 0 deletions agents/zmq/src/zmq_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ class ZmqAgent {
std::atomic<bool> exiting_;

const std::string agent_id_;
std::string app_name_;

// For Auth
std::string auth_url_;
Expand Down
5 changes: 3 additions & 2 deletions test/agents/test-grpc-metrics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,10 @@ async function runTest({ getEnv }) {
};
const child = new TestClient([], opts);
const agentId = await child.id();
const config = await child.config();
const config = await child.config({ app: 'my_app_name', interval: 100 });
grpcServer.once('metrics', mustCall(async () => {
const metrics = await child.metrics();
assert.strictEqual(config.app, 'my_app_name');
const { data, requestId } = await grpcServer.metrics(agentId);
checkMetricsData(data.msg, data.metadata, requestId, agentId, config, metrics);
await child.shutdown(0);
Expand All @@ -257,7 +258,7 @@ const testConfigs = [
NODE_DEBUG_NATIVE: 'nsolid_grpc_agent',
NSOLID_GRPC_INSECURE: 1,
NSOLID_GRPC: `localhost:${port}`,
NSOLID_INTERVAL: 100,
NSOLID_INTERVAL: 10000,
};
},
},
Expand Down
3 changes: 3 additions & 0 deletions test/agents/test-otlp-metrics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
} = validators;

const __filename = fileURLToPath(import.meta.url);
const appName = 'my_app_name';

if (process.argv[2] === 'child') {
// Just to keep the worker alive.
Expand All @@ -26,6 +27,7 @@ if (process.argv[2] === 'child') {
if (isMainThread) {
nsolid.start({
tracingEnabled: false,
app: appName,
});

nsolid.setThreadName('main-thread');
Expand Down Expand Up @@ -601,6 +603,7 @@ if (process.argv[2] === 'child') {
if (message.type === 'nsolid') {
nsolidId = message.id;
nsolidAppName = message.appName;
assert.strictEqual(nsolidAppName, appName);
nsolidMetrics = message.metrics;
} else if (message.type === 'workerThreadId') {
context.threadList.push(message.id);
Expand Down
1 change: 1 addition & 0 deletions test/agents/test-zmq-info.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { TestPlayground } from '../common/nsolid-zmq-agent/index.js';
function checkInfoData(info, requestId, agentId, nsolidConfig = {}) {
assert.strictEqual(info.requestId, requestId);
assert.strictEqual(info.agentId, agentId);
assert.strictEqual(info.app, nsolidConfig.appName || 'untitled application');
assert.strictEqual(info.command, 'info');
// From here check at least that all the fields are present
assert.ok(info.recorded);
Expand Down
1 change: 1 addition & 0 deletions test/agents/test-zmq-metrics.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ function checkMetricsData(metrics, requestId, agentId, threads) {
console.dir(metrics, { depth: null });
assert.strictEqual(metrics.requestId, requestId);
assert.strictEqual(metrics.agentId, agentId);
assert.strictEqual(metrics.app, 'untitled application');
assert.strictEqual(metrics.command, 'metrics');
// From here check at least that all the fields are present
assert.ok(metrics.recorded);
Expand Down
1 change: 1 addition & 0 deletions test/agents/test-zmq-packages.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const expectedPackagesMains = [
function checkPackagesData(packages, requestId, agentId) {
assert.strictEqual(packages.requestId, requestId);
assert.strictEqual(packages.agentId, agentId);
assert.strictEqual(packages.app, 'untitled application');
assert.strictEqual(packages.command, 'packages');
// From here check at least that all the fields are present
assert.ok(packages.recorded);
Expand Down
1 change: 1 addition & 0 deletions test/agents/test-zmq-ping.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function checkPingData(data, requestId, agentId) {
console.dir(data, { depth: null });
assert.strictEqual(data.requestId, requestId);
assert.strictEqual(data.agentId, agentId);
assert.strictEqual(data.app, 'untitled application');
assert.strictEqual(data.command, 'ping');
// From here check at least that all the fields are present
validateObject(data.recorded, 'recorded');
Expand Down
1 change: 1 addition & 0 deletions test/agents/test-zmq-profile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
function checkProfileData(requestId, options, agentId, data, complete, onExit = false) {
assert.strictEqual(data.requestId, requestId);
assert.strictEqual(data.agentId, agentId);
assert.strictEqual(data.app, 'untitled application');
assert.strictEqual(data.command, 'profile');
if (onExit) {
assert.ok(data.duration < options.duration);
Expand Down
1 change: 1 addition & 0 deletions test/agents/test-zmq-reconfigure.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const {
function checkReconfigureData(reconfigure, requestId, agentId, nsolidConfig) {
assert.strictEqual(reconfigure.requestId, requestId);
assert.strictEqual(reconfigure.agentId, agentId);
assert.strictEqual(reconfigure.app, 'untitled application');
assert.strictEqual(reconfigure.command, 'reconfigure');
// From here check at least that all the fields are present
validateObject(reconfigure.recorded, 'recorded');
Expand Down
1 change: 1 addition & 0 deletions test/agents/test-zmq-snapshot.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { TestPlayground } from '../common/nsolid-zmq-agent/index.js';
function checkSnapshotData(requestId, options, agentId, data, complete) {
assert.strictEqual(data.requestId, requestId);
assert.strictEqual(data.agentId, agentId);
assert.strictEqual(data.app, 'untitled application');
assert.strictEqual(data.command, 'snapshot');
assert.strictEqual(data.complete, complete);
assert.strictEqual(data.threadId, options.threadId);
Expand Down
1 change: 1 addition & 0 deletions test/agents/test-zmq-startup-times.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
function checkStartupTimesData(data, requestId, agentId, additionalTimes = []) {
assert.strictEqual(data.requestId, requestId);
assert.strictEqual(data.agentId, agentId);
assert.strictEqual(data.app, 'untitled application');
assert.strictEqual(data.command, 'startup_times');
// From here check at least that all the fields are present
validateObject(data.recorded, 'recorded');
Expand Down
Loading

0 comments on commit 6236a21

Please sign in to comment.