I am trying to integrate OpenTelemetry with a Spring Boot application and export metrics to VictoriaMetrics. Below is my setup:
Java Classes & Configurations
RestartMetrics.java
public class RestartMetrics {
private static final String SERVICE_NAME = "springboot-service"; // Define service name
private final LongCounter restartCounter;
public RestartMetrics(MeterProvider meterProvider) {
Meter meter = meterProvider.get(SERVICE_NAME + ".restart.metrics");
this.restartCounter = meter.counterBuilder("service.restart.count")
.setDescription("Tracks the number of service restarts")
.build();
}
@PostConstruct
public void init() {
restartCounter.add(1, Attributes.of(AttributeKey.stringKey("service.name"), SERVICE_NAME));
}
}
OpenTelemetryConfig.java
public class OpenTelemetryConfig {
@Bean
public MeterProvider meterProvider() {
// Create an OTLP gRPC metric exporter
OtlpGrpcMetricExporter metricExporter = OtlpGrpcMetricExporter.builder()
.setEndpoint("http://localhost:4317") // OpenTelemetry Collector endpoint
.build();
// Create a PeriodicMetricReader to export metrics periodically
PeriodicMetricReader metricReader = PeriodicMetricReader.builder(metricExporter)
.setInterval(Duration.ofMillis(5000)) // Export interval as Duration
.build();
// Create and return a MeterProvider
return SdkMeterProvider.builder()
.registerMetricReader(metricReader)
.build();
}
}
otel-collector-config.yaml
receivers:
otlp:
protocols:
grpc:
endpoint: 0.0.0.0:4317
http:
endpoint: 0.0.0.0:4318
exporters:
prometheusremotewrite:
endpoint: http://localhost:8428/api/v1/write
resource_to_telemetry_conversion:
enabled: true
service:
pipelines:
metrics:
receivers: [otlp]
exporters: [prometheusremotewrite]
application.properties
otel.metrics.exporter=otlp
otel.exporter.otlp.endpoint=http://localhost:4317
otel.service.name=springboot-app
otel.exporter.otlp.protocol=http/protobuf
Dependencies (pom.xml)
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
<version>1.37.0</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<version>1.37.0</version>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
<version>1.36.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.11.0</version>
</dependency>
Running Services
VictoriaMetrics
docker run -d -p 8428:8428 --name victoriametrics victoriametrics/victoria-metrics
OpenTelemetry Collector
docker run -v /Users/smith/Documents/Workspace/otel-collector-config.yaml:/etc/otelcol/config.yml -p 4317:4317 otel/opentelemetry-collector
Logs from OpenTelemetry Collector
{"otelcolponent.id": "debug", "otelcolponent.kind": "Exporter", "otelcol.signal": "metrics"}
2025-03-17T07:22:47.997Z info Metrics
{"otelcolponent.id": "debug", "otelcolponent.kind": "Exporter", "otelcol.signal": "metrics", "resource metrics": 1, "metrics": 1, "data points": 1}
2025-03-17T07:22:47.997Z info ResourceMetrics #0
Resource SchemaURL:
Resource attributes:
-> service.name: Str(unknown_service:java)
-> telemetry.sdk.language: Str(java)
-> telemetry.sdk.name: Str(opentelemetry)
-> telemetry.sdk.version: Str(1.37.0)
ScopeMetrics #0
ScopeMetrics SchemaURL:
InstrumentationScope springboot-service.restart.metrics
Metric #0
Descriptor:
-> Name: service.restart.count
-> Description: Tracks the number of service restarts
-> Unit:
-> DataType: Sum
-> IsMonotonic: true
-> AggregationTemporality: Cumulative
NumberDataPoints #0
Data point attributes:
-> service.name: Str(springboot-service)
StartTimestamp: 2025-03-17 07:05:17.295034 +0000 UTC
Timestamp: 2025-03-17 07:22:47.899379 +0000 UTC
Value: 1
Issues
- Metrics are not being sent to VictoriaMetrics. There are no logs related to export, and I verified that VictoriaMetrics is running on
localhost:8428
. - Service name appears as
unknown_service:java
instead of the expectedspringboot-app
, even though it is specified inapplication.properties
.
Questions
- Why are my metrics not being sent to VictoriaMetrics?
- How can I fix the
unknown_service:java
issue and correctly set the service name?
Any insights or suggestions would be appreciated. Thanks!
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744573833a4581615.html
评论列表(0条)