trace_api.c
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef OS_WINDOWS
#include <windows.h>
#else
#include <sys/time.h>
#endif
/*
* A dummy example that needs to be replaced once this library can actually do
* something.
*/
int main() {
const char* api_key = getenv("NEW_RELIC_API_KEY");
if (!api_key) {
fprintf(stderr, "NEW_RELIC_API_KEY not set\n");
exit(1);
}
/* Initialize a logger. */
nrt_log_init(NRT_LOG_DEBUG, "stdout");
/* Initialize a configuration. */
/* Initialize a new client with the given API key. */
nrt_client_t* client = nrt_client_new(&cfg);
assert(NULL == cfg);
/* Create an empty span batch */
/* Create a span and add it to the batch */
{
#ifdef OS_WINDOWS
SYSTEMTIME time;
GetSystemTime(&time);
nrt_time_t time_ms = (time.wSecond * 1000) + time.wMilliseconds;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
nrt_time_t time_ms = (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000;
#endif
nrt_span_t* span
= nrt_span_new("e9f54a2c322d7578", "1b1bf29379951c1d", time_ms);
nrt_span_set_name(span, "Root span");
nrt_span_set_duration(span, 2000);
nrt_span_set_parent_id(span, "parent_id");
nrt_span_set_service_name(span, "Telemetry Application");
nrt_attributes_set_int(attrs, "int", -6);
nrt_attributes_set_uint(attrs, "uint", 6);
nrt_attributes_set_double(attrs, "double", 3.14159);
nrt_attributes_set_string(attrs, "string", "value");
nrt_attributes_set_bool(attrs, "bool", true);
nrt_span_set_attributes(span, &attrs);
assert(NULL == attrs);
nrt_span_batch_record(batch, &span);
assert(NULL == span);
}
/* Queue the span batch */
nrt_client_send(client, &batch);
assert(NULL == batch);
/* Wait for the batch to be sent and shut down the client. */
assert(NULL == client);
}
nrt_span_batch_t * nrt_span_batch_new()
Create a new span batch.
nrt_client_t * nrt_client_new(nrt_client_config_t **config)
Create a new client.
struct _nrt_client_config_t nrt_client_config_t
A configuration object used to initialize a nrt_client_t.
Definition: newrelic-telemetry-sdk.h:28
bool nrt_attributes_set_string(nrt_attributes_t *attributes, const char *key, const char *value)
Add a string attribute to an attribute collection.
bool nrt_span_set_attributes(nrt_span_t *span, nrt_attributes_t **attributes)
Set attributes on a span.
bool nrt_attributes_set_int(nrt_attributes_t *attributes, const char *key, int64_t value)
Add an int attribute to an attribute collection.
bool nrt_log_init(nrt_log_level_t level, const char *filename)
Initialize logging.
This is a helper library that supports sending New Relic data from within your C/C++ application.
struct _nrt_client_t nrt_client_t
A Client is capable of both queuing and sending span and metrics batches to a configured New Relic co...
Definition: newrelic-telemetry-sdk.h:34
nrt_client_config_t * nrt_client_config_new(const char *key)
Create a new client configuration with an Insights API key.
bool nrt_span_set_parent_id(nrt_span_t *span, const char *parent_id)
Set the parent_id of a span.
bool nrt_span_set_service_name(nrt_span_t *span, const char *service_name)
Set the service name of a span.
struct _nrt_span_batch_t nrt_span_batch_t
A span batch.
Definition: newrelic-telemetry-sdk.h:44
bool nrt_attributes_set_bool(nrt_attributes_t *attributes, const char *key, bool value)
Add a bool attribute to an attribute collection.
struct _nrt_span_t nrt_span_t
A span.
Definition: newrelic-telemetry-sdk.h:53
uint64_t nrt_time_t
Indicates a point in time or a duration.
Definition: newrelic-telemetry-sdk.h:68
bool nrt_attributes_set_uint(nrt_attributes_t *attributes, const char *key, uint64_t value)
Add an unsigned int attribute to an attribute collection.
bool nrt_span_batch_record(nrt_span_batch_t *batch, nrt_span_t **span)
Add a span to a span batch.
bool nrt_span_set_name(nrt_span_t *span, const char *name)
Set the name of a span.
struct _nrt_attributes_t nrt_attributes_t
A collection of attributes.
Definition: newrelic-telemetry-sdk.h:60
bool nrt_span_set_duration(nrt_span_t *span, nrt_time_t duration)
Set the duration for a span.
nrt_attributes_t * nrt_attributes_new()
Create a new attribute collection.
bool nrt_attributes_set_double(nrt_attributes_t *attributes, const char *key, double value)
Add a double attribute to an attribute collection.
void nrt_client_shutdown(nrt_client_t **client)
Shutdown a client.
nrt_span_t * nrt_span_new(const char *id, const char *trace_id, uint64_t timestamp)
Create a new span.
bool nrt_client_send(nrt_client_t *client, nrt_span_batch_t **batch)
Send a span batch.