Maven
Maven için şu satırı dahil
ederiz. Bunun amacı SpringBoot Metrics Actuator tarafından oluşturulan metriklerin DataDog' un okuyabileceği bir formata çevirmek
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-datadog</artifactId>
</dependency>
DatadogMeterRegistry Sınıfı
Kullanım
Örnek
@SpringBootApplication
public class SpringBootDatadogMetricsApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDatadogMetricsApplication.class, args);
}
@Bean
public MeterRegistry meterRegistry() {
// Configure Datadog-specific settings
DatadogConfig datadogConfig = new DatadogConfig() {
@Override
public String apiKey() {
return "YOUR_DATADOG_API_KEY";
}
@Override
public String get(String key) {
return null; // Use the default values for other configuration options
}
};
// Create and return a DatadogMeterRegistry
return new DatadogMeterRegistry(datadogConfig, Clock.SYSTEM);
}
// Define a sample counter bean
@Bean
public Counter sampleCounter(MeterRegistry meterRegistry) {
return Counter.builder("custom.counter")
.description("A custom counter metric")
.register(meterRegistry);
}
}
Counter nesnesini kullanmak için şöyle
yaparızfinal Counter sampleCounter; // Constructor Injection
...
double valueToRecord = // calculated value
sampleCounter.record(valueToRecord);
Eğer Counter nesnesini dinamik olarak yaratmak istersek şöyle
yaparız. Burada counter nesnesine tag veriliyor
@Service
public class SaleService {
final MeterRegistry meterRegistry;
public void makeSales(...) {
...
Product product = ...
Counter salesCounter = Counter.builder("sales.dollarAmount")
.description("sales")
.tag("productCategory", product.category, "customerAge", customer.age)
.register(meterRegistry);
salesCounter.record(product.price);
}
}
In Micrometer, tags are key-value pairs that provide additional context to your metrics. They help you organize and categorize your metrics, making it easier to filter and query them. Tags are especially useful when pushing metrics to monitoring systems like Datadog, as they allow you to create more detailed and customized visualizations and alerts.
For Example, let’s say you want to record time series metrics on successful amounts of sales of your products. You want to be able to make an analysis based on item categories as well as the age of the customer. While there may be a finite number of categories, you need to be able to support the introduction of a new category without any code change. In that case, you need to be able to construct theCounter object on the fly instead of registering it as a bean.
config metodu
Sometimes, you need to track some metrics based on the instance of the server the metric is coming from. These are usually infrastructure-related resources such as CPU, memory, and database connections. Let’s say you want to keep track of database connection usage. You need to pinpoint which cluster of services and which server in the cluster has high usage of database connections. This can be made possible by using common tags.
Örnek
@SpringBootApplication
public class SpringBootDatadogMetricsApplication {
@Value ..
String environment;
@Value ..
String serviceName;
@Bean
public MeterRegistry meterRegistry() {
// Configure Datadog-specific settings
DatadogConfig datadogConfig = new DatadogConfig() {
@Override
public String apiKey() {
return "YOUR_DATADOG_API_KEY";
}
@Override
public String get(String key) {
return null; // Use the default values for other configuration options
}
};
// Create and return a DatadogMeterRegistry and add common tags
DatadogMeterRegistry registry =
new DatadogMeterRegistry(datadogConfig, Clock.SYSTEM);
Config config = registry.config();
HashSet<Tags> tags = new HashSet<>();
tags.add(Tags.of("environment", environment));
tags.add(Tags.of("serviceName", serviceName));
config.commonTags(tags);
return registry;
}
}
EC2 Instance Metadata to Tags ve Adding Metadata for Tagging on ECS Fargate örnekleri de
burada