Observe metrics and tracing using New Relic

Users can observe Ballerina programs with New Relic, which is an observability platform designed to help organizations monitor, analyze, and troubleshoot their applications, infrastructure, and digital experiences in real-time. Both metrics and tracing in Ballerina can be viewed with New Relic.

The sample shop service will be used in this guide. Follow the steps given below to observe Ballerina tracing and metrics in New Relic.

Step 1 - Create a New Relic account and an API key

Sign up and Generate an API Key in New Relic.

To configure the API key in Newrelic:

Go to Profile -> API keys -> Insights Insert key -> Insert keys to create an account in New Relic.

Step 2 - Import Ballerina New Relic extension

To include the New Relic extension into the executable, the ballerinax/newrelic module needs to be imported into your Ballerina project main.bal file.

Copy
import ballerinax/newrelic as _;

New Relic extension has an Opentelemetry GRPC Span Exporter which will push tracing data as batches to the New Relic server endpoint (https://otlp.nr-data.net:4317) in opentelemetry format.

New Relic extension pushes metrics in New Relic metric format to the New Relic server endpoint (https://metric-api.newrelic.com/metric/v1).

Step 3 - Configure Ballerina runtime configurations

Tracing and metrics can be enabled in your Ballerina project using configurations similar to the following in your Config.toml file.

Copy
[ballerina.observe]
tracingEnabled=true
tracingProvider="newrelic"
metricsEnabled=true
metricsReporter="newrelic"

[ballerinax.newrelic]
apiKey="<NEW_RELIC_LICENSE_KEY>"    
tracingSamplerType="const"          
tracingSamplerParam=1               
tracingReporterFlushInterval=15000  
tracingReporterBufferSize=10000     
metricReporterFlushInterval=15000   
metricReporterClientTimeout=10000

The table below provides the descriptions of each configuration option and possible values that can be assigned.

Configuration keyDescriptionDefault valuePossible values
ballerinax.newrelic. apiKeyAPI key generated by the user in the New Relic platform. This configuration is mandatory.None
ballerinax.newrelic. tracingSamplerTypeType of the sampling methods used in the New Relic tracer.constconst, probabilistic, or ratelimiting.
ballerinax.newrelic. tracingSamplerParamIt is a floating value. Based on the sampler type, the effect of the sampler param varies1.0For const 0 (no sampling) or 1 (sample all spans), for probabilistic 0.0 to 1.0, for ratelimiting any positive integer (rate per second).
ballerinax.newrelic. tracingReporterFlushIntervalThe New Relic tracing client will be sending the spans to the agent at this interval.15000Any positive integer value.
ballerinax.newrelic. tracingReporterBufferSizeQueue size of the New Relic tracing client.10000Any positive integer value.
ballerinax.newrelic. metricReporterFlushIntervalThe New Relic client will be sending the metrics to the agent at this interval.15000Any positive integer value.
ballerinax.newrelic. metricReporterClientTimeoutQueue size of the New Relic metric client.10000Any positive integer value.

Step 4 - Run the Ballerina service

When Ballerina observability is enabled, the Ballerina runtime collects tracing and metrics data and both metrics and traces will be published to New Relic.

Run the following command to start the Ballerina service.

Copy
$ bal run

Compiling source

Running executable

ballerina: started publishing traces to New Relic on https://otlp.nr-data.net:4317
ballerina: started publishing metrics to New Relic on https://metric-api.newrelic.com/metric/v1

Step 5 - Send requests

Send requests to http://localhost:8090/shop/products.

Example cURL commands:

Copy
$ curl -X GET http://localhost:8090/shop/products
Copy
$ curl -X POST http://localhost:8090/shop/product \
-H "Content-Type: application/json" \
-d '{
    "id": 4, 
    "name": "Laptop Charger", 
    "price": 50.00
}'
Copy
$ curl -X POST http://localhost:8090/shop/order \
-H "Content-Type: application/json" \
-d '{
    "productId": 1, 
    "quantity": 1
}'
Copy
$ curl -X GET http://localhost:8090/shop/order/0

Step 6 - View metrics on the New Relic platform

You can view the metrics that were published to the New Relic platform in the New Relic query builder. You can view the metrics query data in graphical format, as shown below.

New Relic metric Query Builder

You can create a dashboard from the metrics provided by Ballerina in the New Relic platform.

Step 7 - View tracing on the New Relic platform

You can view the traces that were published to the New Relic platform in New Relic traces.

Ballerina traces on New Relic

Span tags on New Relic