Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agent/php_globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ static void nr_php_per_process_globals_dispose(void) {
nr_free(nr_php_per_process_globals.env_labels);
nr_free(nr_php_per_process_globals.apache_add);
nr_free(nr_php_per_process_globals.docker_id);
nr_free(nr_php_per_process_globals.agent_control_health_location);

nr_memset(&nr_php_per_process_globals, 0, sizeof(nr_php_per_process_globals));
}
Expand Down
5 changes: 5 additions & 0 deletions agent/php_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ typedef struct _nrphpglobals_t {
/* Original PHP SAPI header callback */
nrphphdrfn_t orig_header_handler;

/* NR Control configuration settings */
int agent_control_enabled;
char* agent_control_health_location;
nrtime_t agent_control_frequency;

struct {
uint8_t no_sql_parsing;
uint8_t show_sql_parsing;
Expand Down
26 changes: 26 additions & 0 deletions agent/php_minit.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "nr_app.h"
#include "nr_banner.h"
#include "nr_daemon_spawn.h"
#include "util_health.h"
#include "util_logging.h"
#include "util_memory.h"
#include "util_signals.h"
Expand Down Expand Up @@ -319,6 +320,25 @@ static void nr_php_check_high_security_log_forwarding(TSRMLS_D) {
}
}

/*
* Check the INI values for 'agent_control_enabled' and
* `agent_control_health_location`, log a warning and set agent_control_enabled
* to 'false' on an invalid configuration state .
*/
static void nr_php_check_agent_control_health_file() {
if (NR_PHP_PROCESS_GLOBALS(agent_control_enabled)
&& nr_strempty(NR_PHP_PROCESS_GLOBALS(agent_control_health_location))) {
nrl_warning(NRL_INIT,
"NR Control support will be DISABLED because a valid health "
"file location is not set. Please check "
"newrelic.agent_control.health.delivery_location in the agent "
"configuration file or the "
"NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION environment "
"variable.");
NR_PHP_PROCESS_GLOBALS(agent_control_enabled) = 0;
}
}

static char* nr_php_get_agent_specific_info(void) {
const char* php_version;
const char* zend_type;
Expand Down Expand Up @@ -651,6 +671,8 @@ PHP_MINIT_FUNCTION(newrelic) {
nr_php_check_logging_config(TSRMLS_C);
nr_php_check_high_security_log_forwarding(TSRMLS_C);

nr_php_check_agent_control_health_file();

/*
* Save the original PHP hooks and then apply our own hooks. The agent is
* almost fully operational now. The last remaining initialization that
Expand Down Expand Up @@ -719,6 +741,10 @@ PHP_MINIT_FUNCTION(newrelic) {
nr_wordpress_minit();
nr_php_set_opcode_handlers();

if (NR_PHP_PROCESS_GLOBALS(agent_control_enabled)) {
nrh_set_start_time();
}

nrl_debug(NRL_INIT, "MINIT processing done");
#if ZEND_MODULE_API_NO >= ZEND_8_0_X_API_NO /* PHP 7.4+ */
NR_PHP_PROCESS_GLOBALS(zend_offset) = zend_get_resource_handle(dummy);
Expand Down
103 changes: 103 additions & 0 deletions agent/php_nrini.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "nr_version.h"
#include "nr_log_level.h"
#include "util_buffer.h"
#include "util_health.h"
#include "util_json.h"
#include "util_logging.h"
#include "util_memory.h"
Expand Down Expand Up @@ -515,6 +516,89 @@ static PHP_INI_MH(nr_high_security_mh) {
return SUCCESS;
}

static PHP_INI_MH(nr_agent_control_enabled_mh) {
int val;

(void)entry;
(void)NEW_VALUE_LEN;
(void)mh_arg1;
(void)mh_arg2;
(void)mh_arg3;
(void)stage;
NR_UNUSED_TSRMLS;

val = nr_bool_from_str(NEW_VALUE);

if (-1 == val) {
return FAILURE;
}

if (val) {
NR_PHP_PROCESS_GLOBALS(agent_control_enabled) = 1;
} else {
NR_PHP_PROCESS_GLOBALS(agent_control_enabled) = 0;
}

return SUCCESS;
}

static PHP_INI_MH(nr_agent_control_location_mh) {
(void)entry;
(void)mh_arg1;
(void)mh_arg2;
(void)mh_arg3;
(void)stage;
NR_UNUSED_TSRMLS;

nr_free(NR_PHP_PROCESS_GLOBALS(agent_control_health_location));

if (0 == NEW_VALUE_LEN) {
return SUCCESS;
}

if (7 >= NEW_VALUE_LEN) {
// missing or malformed 'file://' prefix
return FAILURE;
}

NR_PHP_PROCESS_GLOBALS(agent_control_health_location)
= nrh_get_health_location(NEW_VALUE);

if (NULL == NR_PHP_PROCESS_GLOBALS(agent_control_health_location)) {
nrl_warning(NRL_INIT,
"failed to set health file location for provided uri '%s'",
NEW_VALUE);
return FAILURE;
}

return SUCCESS;
}

static PHP_INI_MH(nr_agent_control_frequency_mh) {
nrtime_t val;

(void)entry;
(void)mh_arg1;
(void)mh_arg2;
(void)mh_arg3;
(void)stage;
NR_UNUSED_TSRMLS;

if (0 != NEW_VALUE_LEN) {
val = nr_parse_time_from_config(NEW_VALUE);
if (val < 1) {
val = 1;
} else if (val > 300) {
val = 300;
}
NR_PHP_PROCESS_GLOBALS(agent_control_frequency) = val;
} else {
NR_PHP_PROCESS_GLOBALS(agent_control_frequency) = 5;
}

return SUCCESS;
}

static PHP_INI_MH(nr_preload_framework_library_detection_mh) {
int val;

Expand Down Expand Up @@ -2169,6 +2253,25 @@ PHP_INI_ENTRY_EX("newrelic.daemon.utilization.detect_kubernetes",
NR_PHP_SYSTEM,
NR_PHP_UTILIZATION_MH_NAME(kubernetes),
nr_enabled_disabled_dh)

PHP_INI_ENTRY_EX("newrelic.agent_control.enabled",
"0",
NR_PHP_SYSTEM,
nr_agent_control_enabled_mh,
0)

PHP_INI_ENTRY_EX("newrelic.agent_control.health.delivery_location",
"file:///newrelic/apm/health",
NR_PHP_SYSTEM,
nr_agent_control_location_mh,
0)

PHP_INI_ENTRY_EX("newrelic.agent_control.health.frequency",
"5s",
NR_PHP_SYSTEM,
nr_agent_control_frequency_mh,
0)

/*
* This daemon flag is for internal development use only. It should not be
* documented to customers.
Expand Down
25 changes: 25 additions & 0 deletions agent/scripts/newrelic.ini.template
Original file line number Diff line number Diff line change
Expand Up @@ -1393,3 +1393,28 @@ newrelic.daemon.logfile = "/var/log/newrelic/newrelic-daemon.log"
; requires that this value when the account ID is not part of the function name.
;
;newrelic.cloud.aws.account_id = ""

; Setting: newrelic.agent_control.enabled
; Type : boolean
; Scope : per-directory
; Default: false
; Info : Boolean value that denotes if agent control functionality should be enabled.
;
;newrelic.agent_control.enabled = false

; Setting: newrelic.agent_control.health.delivery_location
; Type : string
; Scope : per-directory
; Default: file:///newrelic/apm/health
; Info : String URI that specifies the fully qualified directory path for the
; health file(s) to be written to.
;
;newrelic.agent_control.health.delivery_location = "file:///newrelic/apm/health"

; Setting: newrelic.agent_control.health.frequency
; Type : time specification string (in seconds, only)
; Scope : per-directory
; Info : Integer that specifies the interval, in seconds, of how often the health
; file will be written.
;
;newrelic.agent_control.health.frequency = 5s
2 changes: 2 additions & 0 deletions axiom/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ OBJS := \
nr_span_queue.o \
nr_synthetics.o \
nr_txn.o \
nr_uuid.o \
nr_version.o \
nr_php_packages.o \
util_apdex.o \
Expand All @@ -136,6 +137,7 @@ OBJS := \
util_hashmap.o \
util_json.o \
util_logging.o \
util_health.o \
util_labels.o \
util_matcher.o \
util_md5.o \
Expand Down
29 changes: 29 additions & 0 deletions axiom/nr_uuid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdlib.h>
#include <time.h>

#include "nr_uuid.h"
#include "util_memory.h"

static const char hex_values[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char* nr_uuid_create(int seed) {
char* uuid = nr_zalloc(NR_UUID_SIZE + 1);
if (0 < seed) {
srand(seed);
} else {
srand(time(NULL));
}

for (int i = 0; i < NR_UUID_SIZE; i++) {
int r = rand() % NR_UUID_RANGE;

uuid[i] = hex_values[r];
}

return uuid;
}
28 changes: 28 additions & 0 deletions axiom/nr_uuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

/*
* This file contains functions for simple uuid generation.
*/
#ifndef NR_UUID_HDR
#define NR_UUID_HDR

#define NR_UUID_SIZE 32
#define NR_UUID_RANGE 16

/**
* nr_uuid_create: pseudo-implementation of uuid generation logic
*
* This function will simply return a randomly generated 32 character hex
* string. It does not implement the spec for UUID generation, which requires
* specific adherence to implementation details and setting bits within the UUID
* to signify which UUID generation variant was used.
*
* Params: int seed (<1 for time based seed)
*
* Returns: 32 byte hex string (must be freed by caller)
*/
char* nr_uuid_create(int seed);
#endif
5 changes: 5 additions & 0 deletions axiom/tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ clang_header.h
# vi/ex scripts
*.ex

# test file generation
health-*.yml

# Test binaries
test_agent
test_analytics_events
Expand Down Expand Up @@ -63,6 +66,7 @@ test_guid
test_hash
test_hashmap
test_header
test_health
test_helgrind
test_json
test_labels
Expand Down Expand Up @@ -121,4 +125,5 @@ test_threads
test_time
test_txn
test_url
test_uuid
test_vector
2 changes: 2 additions & 0 deletions axiom/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ TESTS := \
test_hash \
test_hashmap \
test_header \
test_health \
test_json \
test_labels \
test_log_event \
Expand Down Expand Up @@ -198,6 +199,7 @@ TESTS := \
test_time \
test_txn \
test_url \
test_uuid \
test_vector

#
Expand Down
Loading