chore: Added spans to health check (#37980)

## Description
> [!TIP]  
> _Add a TL;DR when the description is longer than 500 words or
extremely technical (helps the content, marketing, and DevRel team)._
>
> _Please also include relevant motivation and context. List any
dependencies that are required for this change. Add links to Notion,
Figma or any other documents that might be relevant to the PR._


Fixes #`Issue Number`  
_or_  
Fixes `Issue URL`
> [!WARNING]  
> _If no issue exists, please create an issue first, and check with the
maintainers if the issue is valid._

## Automation

/ok-to-test tags=""

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!CAUTION]  
> If you modify the content in this section, you are likely to disrupt
the CI result for your PR.

<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced improved health check functionality for Redis and MongoDB,
enhancing observability with specific health metrics.
- Added constants for health monitoring, including `HEALTH`,
`MONGO_HEALTH`, and `REDIS_HEALTH`.

- **Bug Fixes**
- Enhanced error handling for health checks, ensuring consistent logging
and mapping of timeout exceptions.

- **Refactor**
- Updated constructor signatures to accommodate new dependencies for
better service initialization.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Nidhi 2024-12-05 17:33:00 +05:30 committed by GitHub
parent 80f200058d
commit 86d22e561c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 34 additions and 5 deletions

View File

@ -0,0 +1,5 @@
package com.appsmith.external.constants.spans;
import com.appsmith.external.constants.spans.ce.HealthSpanCE;
public class HealthSpan extends HealthSpanCE {}

View File

@ -0,0 +1,10 @@
package com.appsmith.external.constants.spans.ce;
import com.appsmith.external.constants.spans.BaseSpan;
public class HealthSpanCE {
public static final String HEALTH = "health.";
public static final String MONGO_HEALTH = BaseSpan.APPSMITH_SPAN_PREFIX + HEALTH + "mongo";
public static final String REDIS_HEALTH = BaseSpan.APPSMITH_SPAN_PREFIX + HEALTH + "redis";
}

View File

@ -1,6 +1,7 @@
package com.appsmith.server.services;
import com.appsmith.server.services.ce.HealthCheckServiceCEImpl;
import io.micrometer.observation.ObservationRegistry;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.stereotype.Component;
@ -9,7 +10,8 @@ import org.springframework.stereotype.Component;
public class HealthCheckServiceImpl extends HealthCheckServiceCEImpl implements HealthCheckService {
public HealthCheckServiceImpl(
ReactiveRedisConnectionFactory reactiveRedisConnectionFactory,
ReactiveMongoTemplate reactiveMongoTemplate) {
super(reactiveRedisConnectionFactory, reactiveMongoTemplate);
ReactiveMongoTemplate reactiveMongoTemplate,
ObservationRegistry observationRegistry) {
super(reactiveRedisConnectionFactory, reactiveMongoTemplate, observationRegistry);
}
}

View File

@ -2,29 +2,37 @@ package com.appsmith.server.services.ce;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import io.micrometer.observation.ObservationRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.data.mongo.MongoReactiveHealthIndicator;
import org.springframework.boot.actuate.data.redis.RedisReactiveHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import reactor.core.observability.micrometer.Micrometer;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;
import static com.appsmith.external.constants.spans.ce.HealthSpanCE.MONGO_HEALTH;
import static com.appsmith.external.constants.spans.ce.HealthSpanCE.REDIS_HEALTH;
@Slf4j
public class HealthCheckServiceCEImpl implements HealthCheckServiceCE {
private final ReactiveRedisConnectionFactory reactiveRedisConnectionFactory;
private final ReactiveMongoTemplate reactiveMongoTemplate;
private final ObservationRegistry observationRegistry;
public HealthCheckServiceCEImpl(
ReactiveRedisConnectionFactory reactiveRedisConnectionFactory,
ReactiveMongoTemplate reactiveMongoTemplate) {
ReactiveMongoTemplate reactiveMongoTemplate,
ObservationRegistry observationRegistry) {
this.reactiveRedisConnectionFactory = reactiveRedisConnectionFactory;
this.reactiveMongoTemplate = reactiveMongoTemplate;
this.observationRegistry = observationRegistry;
}
@Override
@ -42,7 +50,9 @@ public class HealthCheckServiceCEImpl implements HealthCheckServiceCE {
return redisReactiveHealthIndicator
.health()
.timeout(Duration.ofSeconds(3))
.onErrorMap(TimeoutException.class, healthTimeout);
.onErrorMap(TimeoutException.class, healthTimeout)
.name(REDIS_HEALTH)
.tap(Micrometer.observation(observationRegistry));
}
private Mono<Health> getMongoHealth() {
@ -55,6 +65,8 @@ public class HealthCheckServiceCEImpl implements HealthCheckServiceCE {
return mongoReactiveHealthIndicator
.health()
.timeout(Duration.ofSeconds(1))
.onErrorMap(TimeoutException.class, healthTimeout);
.onErrorMap(TimeoutException.class, healthTimeout)
.name(MONGO_HEALTH)
.tap(Micrometer.observation(observationRegistry));
}
}