ex_distributed_tracing_server.c
/*
* This application polls for a request file. If a request exists, its
* contents are read and treated as base64 encoded distributed trace
* payload. This payload is then accepted, the request file is deleted
* and a response file is written.
*
* The client sending the request is contained in
* ex_distributed_tracing_client.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "common.h"
#include "libnewrelic.h"
#define REQUEST_FILE "/tmp/request"
#define RESPONSE_FILE "/tmp/response"
static char* read_file(FILE* fp) {
char* buf = NULL;
size_t len;
if (fp) {
fseek(fp, 0, SEEK_END);
len = ftell(fp);
fseek(fp, 0, SEEK_SET);
buf = malloc(len + 1);
if (buf) {
buf[0] = '\0';
fread(buf, 1, len, fp);
buf[len] = '\0';
}
}
return buf;
}
int main(void) {
example_init();
/* Initialize the config */
char* app_name = get_app_name();
if (NULL == app_name) {
return -1;
}
char* license_key = get_license_key();
if (NULL == license_key) {
return -1;
}
= newrelic_create_app_config(app_name, license_key);
customize_config(&config);
/* Change the transaction tracer threshold to ensure a trace is generated */
/* Enable distributed tracing */
config->distributed_tracing.enabled = true;
/* Wait up to 10 seconds for the SDK to connect to the daemon */
newrelic_app_t* app = newrelic_create_app(config, 10000);
printf("Server started, ready to accept requests.\n");
/* Remove left over response files */
unlink(RESPONSE_FILE);
while (true) {
/* Read and handle a request */
FILE* request_file = fopen(REQUEST_FILE, "r");
if (request_file) {
printf("Request received.\n");
/* Start a web transaction */
= newrelic_start_web_transaction(app, "ExampleDTServer");
sleep(1);
/*
* Read the payload from the request file and accept it
*
* NOTE: As a convention when communicating by HTTP/HTTPS, applications
* which are instrumented with New Relic send and receive DT
* payloads via the `newrelic` request header.
*/
char* payload = read_file(request_file);
if (payload) {
printf("Accept payload.\n");
txn, read_file(request_file), NEWRELIC_TRANSPORT_TYPE_OTHER);
free(payload);
}
/* Close the request file */
fclose(request_file);
unlink(REQUEST_FILE);
/* Write a response file to indicate that the request was handled */
FILE* response_file = fopen(RESPONSE_FILE, "w");
if (response_file) {
printf("Writing response.\n\n");
fclose(response_file);
} else {
newrelic_notice_error(txn, 50, "Cannot send response",
"Error.class.response");
printf("Error writing response.\n\n");
}
/* End web transaction */
}
}
return 0;
}
newrelic_transaction_tracer_threshold_t threshold
Whether to consider transactions for trace generation based on the apdex configuration or a specific ...
Definition: libnewrelic.h:209
struct _newrelic_txn_t newrelic_txn_t
A New Relic transaction.
Definition: libnewrelic.h:106
Definition: libnewrelic.h:191
newrelic_txn_t * newrelic_start_web_transaction(newrelic_app_t *app, const char *name)
Start a web based transaction.
Configuration used to describe application name, license key, as well as optional transaction tracer ...
Definition: libnewrelic.h:347
struct _nr_app_and_info_t newrelic_app_t
A New Relic application. Once an application configuration is created with newrelic_create_app_config...
Definition: libnewrelic.h:95
newrelic_transaction_tracer_config_t transaction_tracer
Optional. The transaction tracer configuration.
Definition: libnewrelic.h:390
newrelic_app_config_t * newrelic_create_app_config(const char *app_name, const char *license_key)
Create a populated application configuration.
bool newrelic_accept_distributed_trace_payload_httpsafe(newrelic_txn_t *transaction, const char *payload, const char *transport_type)
Accept a distributed trace payload, an http-safe, base64-encoded string.
newrelic_app_t * newrelic_create_app(const newrelic_app_config_t *config, unsigned short timeout_ms)
Create an application.
newrelic_time_us_t duration_us
If the SDK configuration threshold is set to NEWRELIC_THRESHOLD_IS_OVER_DURATION, this field specifie...
Definition: libnewrelic.h:218
newrelic_distributed_tracing_config_t distributed_tracing
Optional. Distributed tracing configuration.
Definition: libnewrelic.h:408
bool newrelic_destroy_app_config(newrelic_app_config_t **config)
Destroy the application configuration.
bool newrelic_end_transaction(newrelic_txn_t **transaction_ptr)
End a transaction.
This is the New Relic C SDK! If your application does not use other New Relic APM agent languages,...
void newrelic_notice_error(newrelic_txn_t *transaction, int priority, const char *errmsg, const char *errclass)
Record an error in a transaction.
bool enabled
Specifies whether or not distributed tracing is enabled.
Definition: libnewrelic.h:323
bool newrelic_destroy_app(newrelic_app_t **app)
Destroy the application.