Skip to content

Commit

Permalink
Merge pull request #4 from pagopa/PRDP-133-fix-log
Browse files Browse the repository at this point in the history
[PRDP-133] fix: Show log on container, application insight and elastic
  • Loading branch information
pasqualespica authored Sep 8, 2023
2 parents 3e56625 + 0375ea6 commit eda8137
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 114 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN echo $(ls -1 /src/java-function-app)
RUN chmod 777 /src/java-function-app/agent/config.yaml
RUN cd /src/java-function-app && \
wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.19.0/jmx_prometheus_javaagent-0.19.0.jar && \
curl -o 'elastic-apm-agent.jar' -L 'https://oss.sonatype.org/service/local/artifact/maven/redirect?r=releases&g=co.elastic.apm&a=elastic-apm-agent&v=LATEST' && \
curl -o 'opentelemetry-javaagent.jar' -L 'https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v1.25.1/opentelemetry-javaagent.jar' && \
mkdir -p /home/site/wwwroot && \
mvn clean package -Dmaven.test.skip=true && \
cd ./target/azure-functions/ && \
Expand All @@ -17,8 +17,8 @@ RUN cd /src/java-function-app && \
RUN chmod 777 /src/java-function-app/jmx_prometheus_javaagent-0.19.0.jar && \
cp /src/java-function-app/jmx_prometheus_javaagent-0.19.0.jar /home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar

RUN chmod 777 /src/java-function-app/elastic-apm-agent.jar && \
cp /src/java-function-app/elastic-apm-agent.jar /home/site/wwwroot/elastic-apm-agent.jar
RUN chmod 777 /src/java-function-app/opentelemetry-javaagent.jar && \
cp /src/java-function-app/opentelemetry-javaagent.jar /home/site/wwwroot/opentelemetry-javaagent.jar

# This image is ssh enabled
#FROM mcr.microsoft.com/azure-functions/java:3.0-java$JAVA_VERSION-appservice
Expand Down
16 changes: 10 additions & 6 deletions helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ microservice-chart:
COSMOS_RECEIPT_CONTAINER_NAME: "receipts"
BLOB_STORAGE_ACCOUNT_ENDPOINT: "https://pagopadweureceiptsfnsa.blob.core.windows.net/"
BLOB_STORAGE_CONTAINER_NAME: "pagopa-d-weu-receipts-azure-blob-receipt-st-attach"
ELASTIC_APM_SERVICE_NAME: "pagopareceiptpdfgeneratorapm"
ELASTIC_APM_APPLICATION_PACKAGES: "it.gov.pagopa.receipt.pdf.generator"
ELASTIC_APM_SERVER_URL: "http://quickstart-apm-http.elastic-system.svc:8200"
ELASTIC_APM_LOG_SENDING: 'false'
ENABLE_ECS_CONSOLE: "true"
JAVA_TOOL_OPTIONS: "-javaagent:/home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar=12345:/home/site/wwwroot/config.yaml -javaagent:/home/site/wwwroot/elastic-apm-agent.jar -Xmx1024m -XX:+UseG1GC"
CONSOLE_LOG_THRESHOLD: "DEBUG"
CONSOLE_LOG_PATTERN: "%d{HH:mm:ss.SSS}[%thread]%-5level%logger{36}-%msg%n"
CONSOLE_LOG_CHARSET: "UTF-8"
ECS_SERVICE_NAME: "pagopareceiptpdfgeneratorotl"
OTEL_RESOURCE_ATTRIBUTES: "service.name=pagopareceiptpdfgeneratorotl,deployment.environment=dev"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector.elastic-system.svc:4317"
OTEL_LOGS_EXPORTER: none
OTEL_TRACES_SAMPLER: "always_on"
JAVA_TOOL_OPTIONS: "-javaagent:/home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar=12345:/home/site/wwwroot/config.yaml -javaagent:/home/site/wwwroot/opentelemetry-javaagent.jar -Xmx1024m -XX:+UseG1GC"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
Expand All @@ -106,7 +110,7 @@ microservice-chart:
RECEIPT_QUEUE_CONN_STRING: "receipts-storage-account-connection-string"
OCP_APIM_SUBSCRIPTION_KEY: "shared-apim-d-subscription-key"
COSMOS_RECEIPT_KEY: "cosmos-receipt-pkey"
ELASTIC_APM_SECRET_TOKEN: 'elastic-apm-secret-token'
OTEL_EXPORTER_OTLP_HEADERS: 'elastic-otl-secret-token'
keyvault:
name: "pagopa-d-receipts-kv"
tenantId: "7788edaf-0346-4068-9d79-c868aed15b3d"
Expand Down
16 changes: 10 additions & 6 deletions helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ microservice-chart:
COSMOS_RECEIPT_CONTAINER_NAME: 'receipts'
BLOB_STORAGE_ACCOUNT_ENDPOINT: 'https://pagopapweureceiptsfnsa.blob.core.windows.net'
BLOB_STORAGE_CONTAINER_NAME: 'pagopa-p-weu-receipts-azure-blob-receipt-st-attach'
ELASTIC_APM_SERVICE_NAME: "pagopareceiptpdfgeneratorapm"
ELASTIC_APM_APPLICATION_PACKAGES: "it.gov.pagopa.receipt.pdf.generator"
ELASTIC_APM_SERVER_URL: "http://quickstart-apm-http.elastic-system.svc:8200"
ELASTIC_APM_LOG_SENDING: "false"
ENABLE_ECS_CONSOLE: "true"
JAVA_TOOL_OPTIONS: "-javaagent:/home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar=12345:/home/site/wwwroot/config.yaml -javaagent:/home/site/wwwroot/elastic-apm-agent.jar"
CONSOLE_LOG_THRESHOLD: "DEBUG"
CONSOLE_LOG_PATTERN: "%d{HH:mm:ss.SSS}[%thread]%-5level%logger{36}-%msg%n"
CONSOLE_LOG_CHARSET: "UTF-8"
ECS_SERVICE_NAME: "pagopareceiptpdfgeneratorotl"
OTEL_RESOURCE_ATTRIBUTES: "service.name=pagopareceiptpdfgeneratorotl,deployment.environment=prod"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector.elastic-system.svc:4317"
OTEL_LOGS_EXPORTER: none
OTEL_TRACES_SAMPLER: "always_on"
JAVA_TOOL_OPTIONS: "-javaagent:/home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar=12345:/home/site/wwwroot/config.yaml -javaagent:/home/site/wwwroot/opentelemetry-javaagent.jar -Xmx1024m -XX:+UseG1GC"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
Expand All @@ -105,7 +109,7 @@ microservice-chart:
RECEIPT_QUEUE_CONN_STRING: "receipts-storage-account-connection-string"
OCP_APIM_SUBSCRIPTION_KEY: 'shared-apim-p-subscription-key'
COSMOS_RECEIPT_KEY: "cosmos-receipt-pkey"
ELASTIC_APM_SECRET_TOKEN: 'elastic-apm-secret-token'
OTEL_EXPORTER_OTLP_HEADERS: 'elastic-otl-secret-token'
keyvault:
name: "pagopa-p-receipts-kv"
tenantId: "7788edaf-0346-4068-9d79-c868aed15b3d"
Expand Down
16 changes: 10 additions & 6 deletions helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ microservice-chart:
COSMOS_RECEIPT_CONTAINER_NAME: 'receipts'
BLOB_STORAGE_ACCOUNT_ENDPOINT: 'https://pagopauweureceiptsfnsa.blob.core.windows.net'
BLOB_STORAGE_CONTAINER_NAME: 'pagopa-u-weu-receipts-azure-blob-receipt-st-attach'
ELASTIC_APM_SERVICE_NAME: "pagopareceiptpdfgeneratorapm"
ELASTIC_APM_APPLICATION_PACKAGES: "it.gov.pagopa.receipt.pdf.generator"
ELASTIC_APM_SERVER_URL: "http://quickstart-apm-http.elastic-system.svc:8200"
ELASTIC_APM_LOG_SENDING: "false"
ENABLE_ECS_CONSOLE: "true"
JAVA_TOOL_OPTIONS: "-javaagent:/home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar=12345:/home/site/wwwroot/config.yaml -javaagent:/home/site/wwwroot/elastic-apm-agent.jar -Xmx1512m -XX:+UseG1GC"
CONSOLE_LOG_THRESHOLD: "DEBUG"
CONSOLE_LOG_PATTERN: "%d{HH:mm:ss.SSS}[%thread]%-5level%logger{36}-%msg%n"
CONSOLE_LOG_CHARSET: "UTF-8"
ECS_SERVICE_NAME: "pagopareceiptpdfgeneratorotl"
OTEL_RESOURCE_ATTRIBUTES: "service.name=pagopareceiptpdfgeneratorotl,deployment.environment=uat"
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector.elastic-system.svc:4317"
OTEL_LOGS_EXPORTER: none
OTEL_TRACES_SAMPLER: "always_on"
JAVA_TOOL_OPTIONS: "-javaagent:/home/site/wwwroot/jmx_prometheus_javaagent-0.19.0.jar=12345:/home/site/wwwroot/config.yaml -javaagent:/home/site/wwwroot/opentelemetry-javaagent.jar -Xmx1024m -XX:+UseG1GC"
envFieldRef:
APP_NAME: "metadata.labels['app.kubernetes.io/instance']"
APP_VERSION: "metadata.labels['app.kubernetes.io/version']"
Expand All @@ -106,7 +110,7 @@ microservice-chart:
RECEIPT_QUEUE_CONN_STRING: "receipts-storage-account-connection-string"
OCP_APIM_SUBSCRIPTION_KEY: 'shared-apim-u-subscription-key'
COSMOS_RECEIPT_KEY: "cosmos-receipt-pkey"
ELASTIC_APM_SECRET_TOKEN: 'elastic-apm-secret-token'
OTEL_EXPORTER_OTLP_HEADERS: 'elastic-otl-secret-token'
keyvault:
name: "pagopa-u-receipts-kv"
tenantId: "7788edaf-0346-4068-9d79-c868aed15b3d"
Expand Down
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,26 @@
<version>1.4.7</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>

<dependency>
<groupId>co.elastic.logging</groupId>
<artifactId>logback-ecs-encoder</artifactId>
<version>1.5.0</version>
</dependency>

<!-- The org.codehaus.janino:commons-compiler:2.6.1 dependency -->
<!-- will be automatically pulled in by Maven's transitivity rules -->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>2.6.1</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
public class GenerateReceiptPdf {

private final Logger logger = LoggerFactory.getLogger(GenerateReceiptPdf.class);

/**
* This function will be invoked when a Queue trigger occurs
* #
Expand Down Expand Up @@ -93,11 +95,8 @@ public void processGenerateReceipt(
}

List<Receipt> itemsToNotify = new ArrayList<>();
Logger logger = LoggerFactory.getLogger(getClass());

String logMsg = String.format("[%s] function called at %s for bizEvent with id %s",
logger.info("[{}] function called at {} for bizEvent with id {}",
context.getFunctionName(), LocalDateTime.now(), bizEvent.getId());
logger.info(logMsg);

//Retrieve receipt's data from CosmosDB
Receipt receipt;
Expand Down Expand Up @@ -130,32 +129,21 @@ public void processGenerateReceipt(

if (debtorCF != null || payerCF != null) {
boolean generateOnlyDebtor = payerCF == null || payerCF.equals(debtorCF);

String log = String.format(
"[%s] Generating pdf for Receipt with id %s",
logger.info("[{}] Generating pdf for Receipt with id {}",
context.getFunctionName(),
receipt.getEventId()
);
logger.info(log);
receipt.getEventId());

//Generate and save PDF
PdfGeneration pdfGeneration = service.handlePdfsGeneration(generateOnlyDebtor, receipt, bizEvent, debtorCF, payerCF, logger);


log = String.format(
"[%s] Saving pdf for Receipt with id %s to the blob storage",
PdfGeneration pdfGeneration = service.handlePdfsGeneration(generateOnlyDebtor, receipt, bizEvent, debtorCF, payerCF);
logger.info("[{}] Saving pdf for Receipt with id {} to the blob storage",
context.getFunctionName(),
receipt.getEventId()
);
logger.info(log);
receipt.getEventId());

//Write PDF blob storage metadata on receipt
numberOfSavedPdfs = service.addPdfsMetadataToReceipt(receipt, pdfGeneration);

//Verify PDF generation success
service.verifyPdfGeneration(bizEventMessage, requeueMessage, logger, receipt, generateOnlyDebtor, pdfGeneration);


} else {
String errorMessage = String.format(
"[%s] Error processing receipt with id %s : both debtor's and payer's fiscal code are null",
Expand All @@ -169,34 +157,24 @@ public void processGenerateReceipt(
errorMessage,
bizEventMessage,
receipt,
requeueMessage,
logger
requeueMessage
);
}

//Add receipt to items to be saved to CosmosDB
itemsToNotify.add(receipt);

} else if (receipt != null) {
String errorMessage = String.format(
"[%s] Receipt with id %s not in INSERTED or RETRY",
logger.info("[{}] Receipt with id {} not in INSERTED or RETRY",
context.getFunctionName(),
receipt.getEventId()
);
logger.info(errorMessage);
receipt.getEventId());
}



if (!itemsToNotify.isEmpty()) {
String log = String.format(
"[%s] Receipt with id %s being saved with status %s and with %s pdfs",
logger.info("[{}] Receipt with id {} being saved with status {} and with {} pdfs",
context.getFunctionName(),
receipt.getEventId(),
receipt.getStatus(),
numberOfSavedPdfs
);
logger.info(log);
numberOfSavedPdfs);
documentdb.setValue(itemsToNotify);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/it/gov/pagopa/receipt/pdf/generator/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*/
public class Info {

private final Logger logger = LoggerFactory.getLogger(Info.class);

/**
* This function will be invoked when a Http Trigger occurs
*
Expand All @@ -35,13 +37,11 @@ public HttpResponseMessage run (
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {

org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

return request.createResponseBuilder(HttpStatus.OK)
.body(getInfo(logger))
.body(getInfo())
.build();
}
public synchronized AppInfo getInfo(Logger logger) {
public synchronized AppInfo getInfo() {
String version = null;
String name = null;
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
public class ManageReceiptPoisonQueue {

private final Logger logger = LoggerFactory.getLogger(ManageReceiptPoisonQueue.class);

/**
* This function will be invoked when a Queue trigger occurs
*
Expand All @@ -53,33 +55,25 @@ public void processManageReceiptPoisonQueue(
OutputBinding<ReceiptError> documentdb,
final ExecutionContext context) {

Logger logger = LoggerFactory.getLogger(getClass());
BizEvent bizEvent = null;

String logMsg = String.format("[%s] function called at %s for payload %s",
context.getFunctionName(), LocalDateTime.now(), errorMessage);
logger.info(logMsg);
logger.info("[{}] function called at {} for payload {}", context.getFunctionName(), LocalDateTime.now(), errorMessage);
boolean retriableContent = false;

try {
//attempt to Map queue bizEventMessage to BizEvent
bizEvent = ObjectMapperUtils.mapString(errorMessage, BizEvent.class);
logMsg = String.format("[%s] function called at %s recognized as valid BizEvent" +
"with id %s",
logger.info("[{}] function called at {} recognized as valid BizEvent with id {}",
context.getFunctionName(), LocalDateTime.now(), bizEvent.getId());
logger.info(logMsg);
if (bizEvent.getAttemptedPoisonRetry()) {
logMsg = String.format("[%s] function called at %s for event with id %s" +
" has ingestion already retried, sending to review",
logger.info("[{}] function called at {} for event with id {} has ingestion already retried, sending to review",
context.getFunctionName(), LocalDateTime.now(), bizEvent.getId());
logger.info(logMsg);
} else {
retriableContent = true;
}
} catch (JsonProcessingException e) {
logMsg = String.format("[%s] received parsing error in the" +
" function called at %s for payload %s", context.getFunctionName(), LocalDateTime.now(), errorMessage);
logger.info(logMsg);
logger.error("[{}] received parsing error in the function called at {} for payload {}",
context.getFunctionName(), LocalDateTime.now(), errorMessage, e);
}

if (retriableContent) {
Expand All @@ -95,22 +89,21 @@ public void processManageReceiptPoisonQueue(
sendMessageResult.getStatusCode());
}
} catch (Exception e) {
logMsg = String.format("[%s] error for the function called at %s when attempting" +
"to requeue BizEvent wit id %s, saving to cosmos for review. Call Error %s",
context.getFunctionName(), LocalDateTime.now(), bizEvent.getId(), e.getMessage());
logger.info(logMsg);
saveToDocument(context, errorMessage, documentdb, logger);
logger.error("[{}] error for the function called at {} when attempting" +
"to requeue BizEvent wit id {}, saving to cosmos for review",
context.getFunctionName(), LocalDateTime.now(), bizEvent.getId(), e);
saveToDocument(context, errorMessage, documentdb);
}
} else {
saveToDocument(context, errorMessage, documentdb, logger);
saveToDocument(context, errorMessage, documentdb);
}

}

private void saveToDocument(ExecutionContext context, String errorMessage,
OutputBinding<ReceiptError> documentdb, Logger logger) {
logger.info(String.format("[%s] saving new entry to the retry error to review with payload %s",
context.getFunctionName(), errorMessage));
OutputBinding<ReceiptError> documentdb) {
logger.info("[{}] saving new entry to the retry error to review with payload {}",
context.getFunctionName(), errorMessage);
documentdb.setValue(ReceiptError.builder().messagePayload(errorMessage)
.status(ReceiptErrorStatusType.TO_REVIEW).build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
public class RetryReviewedPoisonMessages {

private final Logger logger = LoggerFactory.getLogger(RetryReviewedPoisonMessages.class);

/**
* This function will be invoked when an CosmosDB trigger occurs
*
Expand Down Expand Up @@ -58,11 +60,9 @@ public void processRetryReviewedPoisonMessages(
final ExecutionContext context) {

List<ReceiptError> itemsDone = new ArrayList<>();
Logger logger = LoggerFactory.getLogger(getClass());

String msg = String.format("[%s] documentCaptorValue stat %s function - num errors reviewed triggered %d",
logger.debug("[{}] documentCaptorValue stat {} function - num errors reviewed triggered {}",
context.getFunctionName(), context.getInvocationId(), items.size());
logger.debug(msg);

ReceiptQueueClientImpl queueService = ReceiptQueueClientImpl.getInstance();

Expand All @@ -86,9 +86,8 @@ public void processRetryReviewedPoisonMessages(

} catch (Exception e) {
//Error info
msg = String.format("[%s] Error to process receiptError with id %s",
context.getFunctionName(), receiptError.getId());
logger.error(msg, e);
logger.error("[{}] Error to process receiptError with id {}",
context.getFunctionName(), receiptError.getId(), e);
receiptError.setMessageError(e.getMessage());
receiptError.setStatus(ReceiptErrorStatusType.TO_REVIEW);
}
Expand Down
Loading

0 comments on commit eda8137

Please sign in to comment.