@Timed annotation in Spring 3.2 Observability returning zero values

39 Views Asked by At

I'm using the new ObservationApi in Spring 3.2.2. I have a controller method marked with the @Observed and some other service methods which are marked with @Timed annotation but my timings which are being pushed to Elastic via the ElasticMeterRegistry all appear as zero.

I believe I have everything configuration properly and am using the DefaultMeterObservationHandler although I understand the @Timed should be separate from this Observation which is generated in my @Observed method. My configuration is as follows:


`public class ObservabilityConfig {


  @Bean
  public ObservedAspect observedAspect(Supplier<ObservationRegistry> observationRegistrySupplier) {
    return new ObservedAspect(observationRegistrySupplier.get());
  }

  @Bean
  public TimedAspect timedAspect(MeterRegistry registry) {
    return new TimedAspect(registry);
  }

  @Bean
  public CountedAspect countedAspect(MeterRegistry registry) {
    return new CountedAspect(registry);
  }
}`

and am injecting my ObservationRegistry via a supplier:

@Profile("!local")
@Component
@RequiredArgsConstructor
@Slf4j
public class ObservationRegistrySupplierForNonLocal implements Supplier<ObservationRegistry> {

  private final MeterRegistry meterRegistry;
  private final String password;
  private final ObservationRegistry observationRegistry;

  @Autowired
  public ObservationRegistrySupplierForNonLocal(
      @Value("${management.elastic.metrics.export.password}") final String password,
      final MeterRegistry meterRegistry) {
    this.meterRegistry = meterRegistry;
    this.password = password;
    this.observationRegistry = configureRegistry(ObservationRegistry.create());
  }

  @Override
  public ObservationRegistry get() {
    return observationRegistry;
  }

  private ObservationRegistry configureRegistry(final ObservationRegistry registry) {
    log.info("Injecting {}", meterRegistry.getClass().getName());
    if (password.isBlank() || password.isEmpty()) {
      throw new IllegalArgumentException(
          "Password for Elastic (\"management.elastic.metrics.export.password\" is blank or empty - "
              + " this should be set in Vault and passed to SpringBoot in order to be able to successfully "
              + "push metrics to Elastic via the ElasticMeterRegistry");
    }
    registry
        .observationConfig()
        .observationHandler(new DefaultMeterObservationHandler(meterRegistry));
    return registry;
  }
}

I'm expecting no-zero values for the timings but am getting:

{
  "@timestamp": [
    "2024-02-02T17:22:04.542Z"
  ],
  "class": [
    "com.foo.RegistrationService"
  ],
  "class.keyword": [
    "com.foo.RegistrationService"
  ],
  "cluster": [
    "sit-east"
  ],
  "cluster.keyword": [
    "sit-east"
  ],
  "component": [
    "register"
  ],
  "component.keyword": [
    "my-service"
  ],
  "count": [
    0
  ],
  "error": [
    "none"
  ],
  "error.keyword": [
    "none"
  ],
  "max": [
    0
  ],
  "mean": [
    0
  ],
  "method": [
    "register"
  ],
  "method.keyword": [
    "register"
  ],
  "name": [
    "networkMerchantService"
  ],
  "region": [
    "useast1"
  ],
  "region.keyword": [
    "useast1"
  ],
  "sum": [
    0
  ],
  "type": [
    "timer"
  ],
  "type.keyword": [
    "timer"
  ],
  "_id": "RjvXao0BTyKpQUv-gOa3",
  "_index": "micrometer-metrics-register-service-2024.01.31-000001",
  "_score": null
}
0

There are 0 best solutions below