Skip to content

Commit

Permalink
fix: metric to control email sent
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Apr 9, 2024
1 parent 2bbb5df commit f98dac2
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions backend/src/main/java/ca/bc/gov/app/service/ches/ChesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.observation.annotation.Observed;
import java.io.IOException;
import java.io.StringWriter;
Expand Down Expand Up @@ -53,25 +55,33 @@ public class ChesService {
public static final String FAILED_TO_SEND_EMAIL = "Failed to send email: {}";
private final ForestClientConfiguration configuration;
private final WebClient chesApi;

private final WebClient authApi;
private final Configuration freeMarkerConfiguration;

private final EmailLogRepository emailLogRepository;
private final MeterRegistry meterRegistry;
private final Counter emailCounterSuccess;
private final Counter emailCounterFailure;


public ChesService(
ForestClientConfiguration configuration,
@Qualifier("chesApi") WebClient chesApi,
@Qualifier("authApi") WebClient authApi,
EmailLogRepository emailLogRepository
EmailLogRepository emailLogRepository,
MeterRegistry meterRegistry
) {
this.configuration = configuration;
this.chesApi = chesApi;
this.authApi = authApi;
this.meterRegistry = meterRegistry;
this.freeMarkerConfiguration = new Configuration(Configuration.VERSION_2_3_31);
this.emailLogRepository = emailLogRepository;
freeMarkerConfiguration.setClassForTemplateLoading(this.getClass(), "/templates");
freeMarkerConfiguration.setDefaultEncoding("UTF-8");

this.emailCounterSuccess = meterRegistry.counter("service.ches.email.sent", "status", "success");
this.emailCounterFailure = meterRegistry.counter("service.ches.email.sent", "status", "failure");

}

public Mono<String> sendEmail(String templateName,
Expand Down Expand Up @@ -247,8 +257,14 @@ public Mono<String> sendEmail(ChesRequestDto requestContent, String subject) {
get422ErrorMessage())
.onStatus(HttpStatusCode::isError, get500ErrorMessage())
.bodyToMono(ChesMailResponse.class)
.doOnNext(response -> log.info("Email sent successfully"))
.doOnError(error -> log.error("Failed to send email", error))
.doOnNext(response -> {
log.info("Email sent successfully");
emailCounterSuccess.increment();
})
.doOnError(error -> {
log.error("Failed to send email", error);
emailCounterFailure.increment();
})
)
)
.map(response -> response.txId().toString())
Expand Down

0 comments on commit f98dac2

Please sign in to comment.