Observe metrics and tracing using New Relic¶
The sample shop service will be used in this guide. Follow the steps given below to observe BI 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 the New Relic extension¶
Create the sample shop service. To include the New Relic extension into the executable, the ballerinax/newrelic module needs to be imported into your BI project. Navigate to file explorer and add the following to main.bal file.
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 - Enable observability for the project¶
Observability can be enabled in a BI project by adding the following section to the Ballerina.toml file by navigating to the file explorer view.
[build-options]
observabilityIncluded=true
Step 4 - Configure runtime configurations for observability¶
Tracing and metrics can be enabled in your BI project using configurations similar to the following. Navigate to file explorer and add the following to Config.toml file.
[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 key | Description | Default value | Possible values |
|---|---|---|---|
| ballerinax.newrelic. apiKey | API key generated by the user in the New Relic platform. This configuration is mandatory. | None |
|
| ballerinax.newrelic. tracingSamplerType | Type of the sampling methods used in the New Relic tracer. | const | const, probabilistic, or ratelimiting. |
| ballerinax.newrelic. tracingSamplerParam | It is a floating value. Based on the sampler type, the effect of the sampler param varies | 1.0 | For 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. tracingReporterFlushInterval | The New Relic tracing client will be sending the spans to the agent at this interval. | 15000 | Any positive integer value. |
| ballerinax.newrelic. tracingReporterBufferSize | Queue size of the New Relic tracing client. | 10000 | Any positive integer value. |
| ballerinax.newrelic. metricReporterFlushInterval | The New Relic client will be sending the metrics to the agent at this interval. | 15000 | Any positive integer value. |
| ballerinax.newrelic. metricReporterClientTimeout | Queue size of the New Relic metric client. | 10000 | Any positive integer value. |
Step 5 - Run the BI service¶
When observability is enabled, the BI runtime collects tracing and metrics data and both metrics and traces will be published to New Relic.
Start the service.
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 6 - Send requests¶
Send requests to http://localhost:8090/shop/products.
Example cURL commands:
$ curl -X GET http://localhost:8090/shop/products
$ curl -X POST http://localhost:8090/shop/product \
-H "Content-Type: application/json" \
-d '{
"id": 4,
"name": "Laptop Charger",
"price": 50.00
}'
$ curl -X POST http://localhost:8090/shop/order \
-H "Content-Type: application/json" \
-d '{
"productId": 1,
"quantity": 1
}'
$ curl -X GET http://localhost:8090/shop/order/0
Step 7 - 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.
You can create a dashboard from the metrics provided by BI in the New Relic platform.
Step 8 - View tracing on the New Relic platform¶
You can view the traces that were published to the New Relic platform in New Relic traces.


