Skip to content

Commit

Permalink
feat(metrics): send to coralogix (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
yossi-eynav authored Feb 23, 2024
1 parent 3886b20 commit 3d370e1
Show file tree
Hide file tree
Showing 5 changed files with 456 additions and 62 deletions.
4 changes: 4 additions & 0 deletions server/api/controller/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ const PlanAreaChangesController = require('../controller/plan_area_changes');
const getPlanTagger = require('../lib/tags');
const PlanStatusChange = require('../model/plan_status_change');
const { meirimStatuses } = require('../constants');
const { report } = require('../../metrics')

const iplan = (limit = -1) =>
iplanApi
.getBlueLines()
.then(iPlans => {
report({metricName: "iplane.bluelines.count", value: iPlans.length})

// limit blue lines found so we output only *limit* plans
if (limit > -1) {
iPlans.splice(limit);
Expand Down Expand Up @@ -116,6 +119,7 @@ const sendPlanningAlerts = () => {
limit: 1
})
.then(unsentPlans => {
report({ metricName: "planning_alerts.unsent", value: unsentPlans.models.length })
Log.debug('Got', unsentPlans.models.length, 'Plans');
return unsentPlans.models;
})
Expand Down
3 changes: 2 additions & 1 deletion server/config/default.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"coralogix": {
"host": "ingress.cx498.coralogix.com"
"host": "ingress.cx498.coralogix.com",
"metricsEndpoint": "https://ingress.cx498-aws-us-west-2.coralogix.com:443"
},
"geocoder": {
"provider": "google",
Expand Down
96 changes: 48 additions & 48 deletions server/metrics.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@

const { CloudWatchClient, PutMetricDataCommand } = require("@aws-sdk/client-cloudwatch"); // CommonJS import
const client = new CloudWatchClient();
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-grpc');
const { MeterProvider, PeriodicExportingMetricReader } = require('@opentelemetry/sdk-metrics');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { Metadata } = require('@grpc/grpc-js');
const Log = require('./api/lib/log');
const Config = require("config")

const env = process.env.NODE_ENV
const apikey = Config.get('coralogix.apikey');
const url = Config.get('coralogix.metricsEndpoint');
const serviceName = Config.get('coralogix.serviceName');


const metadata = new Metadata();
metadata.add('Authorization', 'Bearer ' + apikey);

const collectorOptions = {
url,
metadata: metadata
};

const metricExporter = new OTLPMetricExporter(collectorOptions);

// [
// {
// Name: "UNIQUE_PAGES",
// Value: "URLS",
// },
// ]
const report = async ({ metricName, unit = "None", value = 1.0, dims = [] }) => {
// See https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html#API_PutMetricData_RequestParameters
// and https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/publishingMetrics.html
// for more information about the parameters in this command.
const command = new PutMetricDataCommand({
MetricData: [
{
MetricName: metricName,
Dimensions: [...dims, { Name: "environment", Value: env }],
Unit: unit,
Value: value,
},
],
Namespace: "meirim",
});
const meterProvider = new MeterProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
}),
});

meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 100,
}));

['SIGINT', 'SIGTERM'].forEach(signal => {
process.on(signal, () => meterProvider.shutdown().catch(console.error));
});

const meter = meterProvider.getMeter('meirim');

const report = async ({ metricName, value = 1, attributes = {} }) => {
try {
await client.send(command);
const h = meter.createHistogram(metricName)
h.record(value, {
...attributes,
env,
})
} catch (err) {
console.error("failed to report metric", err);}
};
console.error("failed to report metric", err);
}
};


async function runAndReport({name, func}) {
try {
await func()
report({
metricName: "job",
dims: [
{
Name: "result",
Value: "success",
},
{
Name: "component",
Value: name,
},
]
attributes: {result: "success", "job-id": name}
})
process.exit();
} catch (e) {
report({
metricName: "job",
dims: [
{
Name: "result",
Value: "failed",
},
{
Name: "component",
Value: name,
},
]
attributes: {result: "failed", "job-id": name}
})
Log.error("failed to run - ", name, e)
process.exit();
process.exit(1);
}
}

Expand Down
Loading

0 comments on commit 3d370e1

Please sign in to comment.