Skip to content

Commit

Permalink
Merge pull request #5 from musabbozkurt/add-new-relic
Browse files Browse the repository at this point in the history
add New Relic
  • Loading branch information
musabbozkurt authored Jul 29, 2024
2 parents 47b5bca + 19b9639 commit 9feb6d4
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NEW_RELIC_LICENSE_KEY=eu01xxhu5ic8N0TmUkXQLgb0VWufpuyvapd0Qz63
NEW_RELIC_ENDPOINT=https://otlp.eu01.nr-data.net
NEW_RELIC_ENABLED=false
NEW_RELIC_INFRA_AGENT_REPLICAS=0
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
* `Docker` should be installed
* `Maven` should be installed
* `pgAdmin`/`DBeaver` can be installed (Optional)
* `New Relic` is disabled by default. Properties can be set (Optional)
* If there is a `New Relic` account, log in to [New Relic](https://login.newrelic.com/login)
1. Generate `api-key` and replace the `NEW_RELIC_LICENSE_KEY` value with yours in the [.env](.env) file
2. Replace the `NEW_RELIC_ENDPOINT` value with yours in the [.env](.env) file
3. In the [.env](.env) file, if `api-key` and `endpoint` are present with the correct
values, `NEW_RELIC_ENABLED` can be `true` and `NEW_RELIC_INFRA_AGENT_REPLICAS` can be greater than zero(0)

-----

Expand Down Expand Up @@ -45,4 +51,11 @@
* Save & test
* Kafka-UI: http://localhost:9091/

-----

### References

* https://github.com/newrelic/micrometer-registry-newrelic?tab=readme-ov-file#archival-notice
* https://docs.micrometer.io/micrometer/reference/implementations/new-relic.html

-----
17 changes: 17 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ services:
- KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka:9093
- KAFKA_CLUSTERS_0_ZOOKEEPER=localhost:2181

new-relic-infra-agent:
container_name: new-relic-infra-agent
image: newrelic/infrastructure:latest
cap_add:
- SYS_PTRACE
network_mode: host
pid: host
privileged: true
volumes:
- "/:/host:ro"
- "/var/run/docker.sock:/var/run/docker.sock"
restart: unless-stopped
environment:
NRIA_LICENSE_KEY: ${NEW_RELIC_LICENSE_KEY}
deploy:
replicas: ${NEW_RELIC_INFRA_AGENT_REPLICAS}

volumes:
zookeeper_data:
driver: local
Expand Down
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,22 @@
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>

<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-micrometer-1.5</artifactId>
<version>2.6.0-alpha</version>
</dependency>

<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-reporter-brave</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.mb.inventorymanagementservice.config;

import io.micrometer.core.instrument.MeterRegistry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
import io.opentelemetry.instrumentation.micrometer.v1_5.OpenTelemetryMeterRegistry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.Aggregation;
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.export.AggregationTemporalitySelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader;
import io.opentelemetry.sdk.resources.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
@ConditionalOnProperty(value = "management.newrelic.metrics.export.enabled", havingValue = "true")
public class MonitoringConfig {

private static final String SERVICE_NAME_KEY = "service.name";
private static final String INSTRUMENTATION_PROVIDER_KEY = "instrumentation.provider";
private static final String INSTRUMENTATION_PROVIDER = "micrometer";
private static final String API_KEY = "api-key";

@Value("${spring.application.name}")
private String applicationName;

@Value("${management.newrelic.metrics.export.api-key}")
private String apiKey;

@Value("${management.newrelic.metrics.export.endpoint}")
private String endpoint;

@Bean
public OpenTelemetry openTelemetry() {
return OpenTelemetrySdk.builder()
.setMeterProvider(
SdkMeterProvider.builder()
.setResource(
Resource.getDefault().toBuilder()
.put(SERVICE_NAME_KEY, applicationName)
// Include instrumentation.provider=micrometer to enable micrometer metrics
// experience in New Relic
.put(INSTRUMENTATION_PROVIDER_KEY, INSTRUMENTATION_PROVIDER)
.build())
.registerMetricReader(
PeriodicMetricReader.builder(OtlpGrpcMetricExporter.builder()
.setEndpoint(endpoint)
.addHeader(API_KEY, apiKey)
// IMPORTANT: New Relic requires metrics to be delta temporality
.setAggregationTemporalitySelector(
AggregationTemporalitySelector.deltaPreferred())
// Use exponential histogram aggregation for histogram instruments
// to produce better data and compression
.setDefaultAggregationSelector(
DefaultAggregationSelector
.getDefault()
.with(InstrumentType.HISTOGRAM,
Aggregation.base2ExponentialBucketHistogram()))
.build())
// Match default micrometer collection interval of 60 seconds
.setInterval(Duration.ofSeconds(5))
.build())
.build())
.build();
}

@Bean
public MeterRegistry meterRegistry(OpenTelemetry openTelemetry) {
return OpenTelemetryMeterRegistry.builder(openTelemetry).build();
}
}
10 changes: 10 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ server:
spring:
application:
name: inventory-management-service

config:
import: optional:file:.env[.properties]

datasource:
hikari:
connection-timeout: 10000
Expand Down Expand Up @@ -105,6 +109,12 @@ management:
endpoint:
health:
show-details: always
newrelic:
metrics:
export:
api-key: ${NEW_RELIC_LICENSE_KEY}
endpoint: ${NEW_RELIC_ENDPOINT}
enabled: ${NEW_RELIC_ENABLED}

springdoc:
api-docs:
Expand Down

0 comments on commit 9feb6d4

Please sign in to comment.