Skip to content

Commit

Permalink
Requisito Logging (#148)
Browse files Browse the repository at this point in the history
* harvester will stop for fatal errors

* adds accepts header for url check

* implementazione iniziale logger

* logging requirement

* fixing lombok's warning
  • Loading branch information
ndc-dxc authored Nov 13, 2024
1 parent ad03a4f commit f904d05
Show file tree
Hide file tree
Showing 32 changed files with 903 additions and 46 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '3.3.4'
id 'org.springframework.boot' version '3.3.5'
id 'io.spring.dependency-management' version '1.1.6'
id 'java'
id 'checkstyle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.gov.innovazione.ndc.alerter.data.EventService;
import it.gov.innovazione.ndc.alerter.dto.EventDto;
import it.gov.innovazione.ndc.alerter.event.AlertableEvent;
import it.gov.innovazione.ndc.service.logging.LoggingContext;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
Expand All @@ -12,6 +13,7 @@
import java.time.Instant;
import java.util.Optional;

import static it.gov.innovazione.ndc.service.logging.NDCHarvesterLogger.logApplicationInfo;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;

@Service
Expand All @@ -30,6 +32,12 @@ public void alert(AlertableEvent alertableEvent) {
.createdBy(getUser())
.occurredAt(defaultIfNull(alertableEvent.getOccurredAt(), Instant.now()))
.build());
logApplicationInfo(LoggingContext.builder()
.component("alerter")
.message("Alerted event")
.details(alertableEvent.getDescription())
.eventCategory(alertableEvent.getCategory())
.build());
}

public static String getUser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import it.gov.innovazione.ndc.alerter.data.EntityService;
import it.gov.innovazione.ndc.alerter.dto.SlimPager;
import it.gov.innovazione.ndc.alerter.entities.Nameable;
import it.gov.innovazione.ndc.service.logging.LoggingContext;
import it.gov.innovazione.ndc.service.logging.NDCHarvesterLogger;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -82,6 +84,14 @@ protected void handlePreCreate(D dto) {

protected void handlePostCreate(D createdEntity) {
log.info("Created entity: {}", createdEntity);
NDCHarvesterLogger.logApplicationInfo(
LoggingContext.builder()
.component("EntityCreation")
.message("Created entity")
.additionalInfo("entityName", createdEntity.getName())
.additionalInfo("entityId", createdEntity.getId())
.additionalInfo("entityType", createdEntity.getClass().getSimpleName())
.build());
}

@PatchMapping
Expand All @@ -103,6 +113,14 @@ protected void handlePreUpdate(D dto) {

protected void handlePostUpdate(D updatedDto) {
log.info("Updated entity: {}", updatedDto);
NDCHarvesterLogger.logApplicationInfo(
LoggingContext.builder()
.component("EntityUpdate")
.message("Updated entity")
.additionalInfo("entityName", updatedDto.getName())
.additionalInfo("entityId", updatedDto.getId())
.additionalInfo("entityType", updatedDto.getClass().getSimpleName())
.build());
}

@DeleteMapping("{id}")
Expand All @@ -123,5 +141,13 @@ protected void handlePreDelete(String id) {

protected void handlePostDelete(D deletedDto) {
log.info("Deleted entity: {}", deletedDto);
NDCHarvesterLogger.logApplicationInfo(
LoggingContext.builder()
.component("EntityDeletion")
.message("Deleted entity")
.additionalInfo("entityName", deletedDto.getName())
.additionalInfo("entityId", deletedDto.getId())
.additionalInfo("entityType", deletedDto.getClass().getSimpleName())
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ProfileInitializer implements Initializer {
public void init() {
List<String> existingProfileNames = repository.findAll().stream()
.map(Profile::getName)
.collect(Collectors.toList());
.toList();
DEFAULT_PROFILES.stream()
.filter(not(p -> existingProfileNames.contains(p.getLeft())))
.map(pair -> Profile.builder()
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/it/gov/innovazione/ndc/config/GitHubConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import it.gov.innovazione.ndc.alerter.event.DefaultAlertableEvent;
import it.gov.innovazione.ndc.eventhandler.NdcEventPublisher;
import it.gov.innovazione.ndc.service.NdcGitHubClient;
import it.gov.innovazione.ndc.service.logging.LoggingContext;
import it.gov.innovazione.ndc.service.logging.NDCHarvesterLogger;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -16,6 +18,8 @@

import java.time.Instant;

import static it.gov.innovazione.ndc.service.logging.NDCHarvesterLogger.logApplicationInfo;

@Slf4j
@Configuration
@RequiredArgsConstructor
Expand All @@ -39,11 +43,24 @@ NdcGitHubClient gitHub(@Value("${github.personal-access-token}") String token) {
.severity(Severity.WARNING)
.build());

NDCHarvesterLogger.logApplicationWarn(LoggingContext.builder()
.component("GitHubConfig")
.message("GitHubConfig not provided")
.details("GitHubConfig personal access token not provided. The GitHub issuer capability will be disabled")
.eventCategory(EventCategory.APPLICATION)
.build());

return NdcGitHubClient.builder()
.enabled(false)
.build();
}
log.info("GitHub personal access token provided. The GitHub issuer capability will be enabled");
logApplicationInfo(LoggingContext.builder()
.component("GitHubConfig")
.message("GitHubConfig provided")
.details("GitHub personal access token provided. The GitHub issuer capability will be enabled")
.eventCategory(EventCategory.APPLICATION)
.build());
return NdcGitHubClient.builder()
.gitHub(new GitHubBuilder().withOAuthToken(token).build())
.enabled(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import it.gov.innovazione.ndc.model.harvester.Repository;
import it.gov.innovazione.ndc.service.GithubService;
import it.gov.innovazione.ndc.service.InstanceManager;
import it.gov.innovazione.ndc.service.logging.HarvesterStage;
import it.gov.innovazione.ndc.service.logging.LoggingContext;
import it.gov.innovazione.ndc.service.logging.NDCHarvesterLoggerUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ThreadUtils;
Expand All @@ -33,6 +36,10 @@
import static it.gov.innovazione.ndc.config.AsyncConfiguration.THREAD_PREFIX;
import static it.gov.innovazione.ndc.model.harvester.HarvesterRun.Status.ALREADY_RUNNING;
import static it.gov.innovazione.ndc.model.harvester.HarvesterRun.Status.FAILURE;
import static it.gov.innovazione.ndc.model.harvester.HarvesterRun.Status.RUNNING;
import static it.gov.innovazione.ndc.service.logging.NDCHarvesterLogger.logSemanticError;
import static it.gov.innovazione.ndc.service.logging.NDCHarvesterLogger.logSemanticInfo;
import static it.gov.innovazione.ndc.service.logging.NDCHarvesterLogger.logSemanticWarn;
import static java.lang.String.format;
import static org.apache.commons.lang3.StringUtils.endsWith;
import static org.apache.commons.lang3.StringUtils.startsWith;
Expand Down Expand Up @@ -70,8 +77,36 @@ public void execute(String runId, Repository repository, String correlationId, S

Instance instanceToHarvest = instanceManager.getNextOnlineInstance(repository);

HarvestExecutionContextUtils.setContext(
HarvestExecutionContext.builder()
.repository(repository)
.revision(revision)
.correlationId(correlationId)
.runId(runId)
.currentUserId(currentUserLogin)
.instance(instanceToHarvest)
.build());

NDCHarvesterLoggerUtils.setInitialContext(
LoggingContext.builder()
.jobId(runId)
.repoUrl(repository.getUrl())
.stage(HarvesterStage.START)
.harvesterStatus(RUNNING)
.component("HARVESTER")
.additionalInfo("revision", revision)
.additionalInfo("correlationId", correlationId)
.additionalInfo("currentUserLogin", currentUserLogin)
.additionalInfo("instance", instanceToHarvest)
.additionalInfo("force", force)
.build());

publishHarvesterStartedEvent(repository, correlationId, revision, runId, currentUserLogin, instanceToHarvest);

logSemanticInfo(LoggingContext.builder()
.message("Starting harvester on repo " + repository.getUrl())
.build());

synchronized (locks) {
if (locks.contains(repository.getId() + revision)) {
log.info("Harvesting for repo '{}' is already in progress", repository.getUrl());
Expand All @@ -85,6 +120,13 @@ public void execute(String runId, Repository repository, String correlationId, S
format("Harvesting for repo %s is already running",
repository.getUrl())),
currentUserLogin);

logSemanticError(LoggingContext.builder()
.harvesterStatus(ALREADY_RUNNING)
.message("Failed harvester on repo " + repository.getUrl())
.details("Harvester on repo " + repository.getUrl() + " is already running")
.build());

setThreadName(runId, repository.getId(), revision, "IDLE");
return;
}
Expand All @@ -97,16 +139,6 @@ public void execute(String runId, Repository repository, String correlationId, S
verifySameRunWasNotExecuted(repository, revision);
}

HarvestExecutionContextUtils.setContext(
HarvestExecutionContext.builder()
.repository(repository)
.revision(revision)
.correlationId(correlationId)
.runId(runId)
.currentUserId(currentUserLogin)
.instance(instanceToHarvest)
.build());

verifyNoNdcIssuesInRepoIfNecessary(repository);

harvesterService.harvest(repository, revision, instanceToHarvest);
Expand All @@ -126,7 +158,9 @@ public void execute(String runId, Repository repository, String correlationId, S
} finally {
log.info("Cleaning up after processing {}", repository.getUrl());
harvesterService.clearTempGraphIfExists(repository.getUrl());
NDCHarvesterLoggerUtils.clearContext();
log.info("Cleaned up after processing {}", repository.getUrl());

}

// cleanup
Expand All @@ -142,6 +176,12 @@ private void verifyNoNdcIssuesInRepoIfNecessary(Repository repository) {
boolean hasIssues = ndcIssue.isPresent();
if (hasIssues) {
URL issueUrl = ndcIssue.get().getUrl();
logSemanticError(LoggingContext.builder()
.harvesterStatus(FAILURE)
.repoUrl(repository.getUrl())
.message("Repository has at least one open NDC issues")
.additionalInfo("issueUrl", issueUrl)
.build());
throw new RepoContainsNdcIssueException(format("Repository %s has NDC issues [%s]",
repository.getUrl(),
issueUrl.toString()));
Expand All @@ -155,14 +195,26 @@ private void removeLock(Repository repository, String revision) {
}

private synchronized void verifySameRunWasNotExecuted(Repository repository, String revision) {
if (harvesterRunService.isHarvestingAlreadyExecuted(repository.getId(), revision)) {
Optional<HarvesterRun> harvesterRun = harvesterRunService.isHarvestingAlreadyExecuted(repository.getId(), revision);
if (harvesterRun.isPresent()) {
logSemanticWarn(LoggingContext.builder()
.harvesterStatus(HarvesterRun.Status.UNCHANGED)
.message("Harvesting for repo '" + repository.getUrl() + "' was already executed")
.additionalInfo("otherJobId", harvesterRun.get().getId())
.build());
throw new HarvesterAlreadyExecutedException(format("Harvesting for repo '%s' with revision '%s' was already executed and no force param was passed",
repository.getUrl(), revision));
}
}

private synchronized void verifyHarvestingIsNotInProgress(String runId, Repository repository) {
if (harvesterRunService.isHarvestingInProgress(runId, repository)) {
Optional<HarvesterRun> harvestingInProgress = harvesterRunService.isHarvestingInProgress(runId, repository);
if (harvestingInProgress.isPresent()) {
logSemanticError(LoggingContext.builder()
.message("Harvesting for repo '" + repository.getUrl() + "' is already in progress")
.harvesterStatus(ALREADY_RUNNING)
.additionalInfo("otherJobId", harvestingInProgress.get().getId())
.build());
throw new HarvesterAlreadyInProgressException(format("Harvesting for repo '%s' is already in progress", repository.getUrl()));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package it.gov.innovazione.ndc.controller;

import io.swagger.v3.oas.annotations.Operation;
import it.gov.innovazione.ndc.eventhandler.NdcEventPublisher;
import it.gov.innovazione.ndc.eventhandler.event.ConfigService;
import it.gov.innovazione.ndc.harvester.service.ActualConfigService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -30,7 +29,6 @@
public class ConfigurationController {

private final ActualConfigService configService;
private final NdcEventPublisher eventPublisher;

@GetMapping
@Operation(
Expand Down
Loading

0 comments on commit f904d05

Please sign in to comment.