From 7efbc60e1ebc8852db8c25a1b08a84ac3dac6beb Mon Sep 17 00:00:00 2001 From: Chris Raible Date: Wed, 6 Nov 2024 15:03:10 -0800 Subject: [PATCH] Improved error log output for prometheus pushgateway requests (#21556) ref https://linear.app/ghost/issue/ENG-1746/enable-ghost-to-push-metrics-to-a-pushgateway - Trying to get Ghost working with the prometheus pushgateway in staging, but it's logging an error each time it tries to push the metrics. The error output is pretty useless for debugging, so this commit improves the error messages to make it easier to debug. --- ghost/prometheus-metrics/package.json | 1 + ghost/prometheus-metrics/src/PrometheusClient.ts | 11 +++++++++-- yarn.lock | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ghost/prometheus-metrics/package.json b/ghost/prometheus-metrics/package.json index 7512a325de..ac054443a2 100644 --- a/ghost/prometheus-metrics/package.json +++ b/ghost/prometheus-metrics/package.json @@ -32,6 +32,7 @@ "typescript": "5.6.2" }, "dependencies": { + "@tryghost/errors": "1.3.6", "@tryghost/logging": "2.4.19", "express": "4.21.1", "prom-client": "15.1.3", diff --git a/ghost/prometheus-metrics/src/PrometheusClient.ts b/ghost/prometheus-metrics/src/PrometheusClient.ts index c09889f600..df760aee47 100644 --- a/ghost/prometheus-metrics/src/PrometheusClient.ts +++ b/ghost/prometheus-metrics/src/PrometheusClient.ts @@ -1,6 +1,7 @@ import {Request, Response} from 'express'; import client from 'prom-client'; import logging from '@tryghost/logging'; +import errors from '@tryghost/errors'; type PrometheusClientConfig = { register?: client.Registry; @@ -56,8 +57,14 @@ export class PrometheusClient { try { await this.gateway.pushAdd({jobName}); logging.debug('Metrics pushed to pushgateway - jobName: ', jobName); - } catch (error) { - logging.error('Error pushing metrics to pushgateway - jobName: ', jobName); + } catch (err) { + let error; + if (typeof err === 'object' && err !== null && 'code' in err) { + error = new errors.InternalServerError({message: 'Error pushing metrics to pushgateway: ' + err.code, code: err.code as string}); + } else { + error = new errors.InternalServerError({message: 'Error pushing metrics to pushgateway: Unknown error'}); + } + logging.error(error); } } } diff --git a/yarn.lock b/yarn.lock index 3b4361ed77..fd48b03cc2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7430,7 +7430,7 @@ focus-trap "^6.7.2" postcss-preset-env "^7.3.1" -"@tryghost/errors@1.3.1", "@tryghost/errors@1.3.5", "@tryghost/errors@^1.2.26", "@tryghost/errors@^1.2.3", "@tryghost/errors@^1.3.5", "@tryghost/errors@^1.3.6": +"@tryghost/errors@1.3.1", "@tryghost/errors@1.3.5", "@tryghost/errors@1.3.6", "@tryghost/errors@^1.2.26", "@tryghost/errors@^1.2.3", "@tryghost/errors@^1.3.5", "@tryghost/errors@^1.3.6": version "1.3.5" resolved "https://registry.yarnpkg.com/@tryghost/errors/-/errors-1.3.5.tgz#f4ef8e5c41a8a37456f2285271124180685827ae" integrity sha512-iOkiHGnYFqSdFM9AVlgiL56Qcx6V9iQ3kbDKxyOAxrhMKq1OnOmOm7tr1CgGK1YDte9XYEZmR9hUZEg+ujn/jQ==