From 05bf8ff97f715dd899380eec093cbca06455ec2a Mon Sep 17 00:00:00 2001 From: Saiful Khan Date: Mon, 15 Apr 2024 18:16:54 +0530 Subject: [PATCH] Add safety checks for reporting metrics --- .../main/nextflow/polly/PollyConfig.groovy | 2 +- .../main/nextflow/polly/PollyExtension.groovy | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/plugins/nf-polly/src/main/nextflow/polly/PollyConfig.groovy b/plugins/nf-polly/src/main/nextflow/polly/PollyConfig.groovy index 3d3d778..1d07966 100644 --- a/plugins/nf-polly/src/main/nextflow/polly/PollyConfig.groovy +++ b/plugins/nf-polly/src/main/nextflow/polly/PollyConfig.groovy @@ -16,7 +16,7 @@ class PollyConfig { PollyConfig(Map map) { def config = map ?: Collections.emptyMap() - metricsStreamName = config.metricsStreamName ?: 'pravaah-dev-user-defined-metrics-events-stream-v1' + metricsStreamName = config.metricsStreamName ?: "NA" } String getMetricsStreamName() { metricsStreamName } diff --git a/plugins/nf-polly/src/main/nextflow/polly/PollyExtension.groovy b/plugins/nf-polly/src/main/nextflow/polly/PollyExtension.groovy index e61ad49..db2fd7f 100644 --- a/plugins/nf-polly/src/main/nextflow/polly/PollyExtension.groovy +++ b/plugins/nf-polly/src/main/nextflow/polly/PollyExtension.groovy @@ -44,6 +44,11 @@ class PollyExtension extends PluginExtensionPoint { */ private PollyConfig config + /** + * A map of 'env' variables set in the Nextflow config file + */ + private Map env + /* * nf-core initializes the plugin once loaded and session is ready * @param session @@ -53,6 +58,7 @@ class PollyExtension extends PluginExtensionPoint { protected void init(Session session) { this.session = session this.config = new PollyConfig(session.config.navigate('polly') as Map) + this.env = session.config.navigate('env') as Map } /** @@ -65,8 +71,19 @@ class PollyExtension extends PluginExtensionPoint { @Function void reportMetric(var key, var value) { logger.info(String.format("Putting record with key='%s' & value='%s'", key, value)) + String streamName = this.config.getMetricsStreamName() - String jobId = System.getenv("job_id") ?: "NA" + if (streamName == "NA") { + logger.error("No stream set for process to send metrics to. Unable to report metric.") + return + } + + String jobId = this.env.get("JOB_ID") ?: "NA" + if (jobId == "NA") { + logger.error("No JOB_ID set for process. Unable to report metric.") + return + } + String partitionKey = key.toString() try { Map map = [job_id: jobId, key: key, value: value, type: getValueType(value)]