Skip to content

Commit

Permalink
Merge pull request #35 from drift-labs/master
Browse files Browse the repository at this point in the history
Bump mainnet
  • Loading branch information
jackwaller authored Oct 14, 2024
2 parents fe3b692 + c0dcc96 commit 8036c0a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 180 deletions.
142 changes: 23 additions & 119 deletions src/core/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
import { ObservableResult } from '@opentelemetry/api';
import { PrometheusExporter } from '@opentelemetry/exporter-prometheus';
import { logger } from '../utils/logger';
import {
ExplicitBucketHistogramAggregation,
InstrumentType,
MeterProvider,
View,
} from '@opentelemetry/sdk-metrics-base';
import { MeterProvider } from '@opentelemetry/sdk-metrics-base';
import { NextFunction, Request, Response } from 'express';

/**
* Creates {count} buckets of size {increment} starting from {start}. Each bucket stores the count of values within its "size".
* @param start
* @param increment
* @param count
* @returns
*/
const createHistogramBuckets = (
start: number,
increment: number,
count: number
) => {
return new ExplicitBucketHistogramAggregation(
Array.from(new Array(count), (_, i) => start + i * increment)
);
};

enum METRIC_TYPES {
runtime_specs = 'runtime_specs',
endpoint_response_times_histogram = 'endpoint_response_times_histogram',
endpoint_response_status = 'endpoint_response_status',
gpa_fetch_duration = 'gpa_fetch_duration',
last_ws_message_received_ts = 'last_ws_message_received_ts',
account_updates_count = 'account_updates_count',
cache_hit_count = 'cache_hit_count',
cache_miss_count = 'cache_miss_count',
current_system_ts = 'current_system_ts',
health_status = 'health_status',
user_pubkey_list_length = 'user_pubkey_list_length',
}

export enum HEALTH_STATUS {
Expand All @@ -61,101 +31,44 @@ const exporter = new PrometheusExporter(
);
}
);
const meterName = 'dlob-meter';
const meterProvider = new MeterProvider({
views: [
new View({
instrumentName: METRIC_TYPES.endpoint_response_times_histogram,
instrumentType: InstrumentType.HISTOGRAM,
meterName,
aggregation: createHistogramBuckets(0, 20, 30),
}),
new View({
instrumentName: METRIC_TYPES.gpa_fetch_duration,
instrumentType: InstrumentType.HISTOGRAM,
meterName,
aggregation: createHistogramBuckets(0, 500, 20),
}),
],
});

const meterName = 'usermap-server-meter';
const meterProvider = new MeterProvider();

meterProvider.addMetricReader(exporter);

const meter = meterProvider.getMeter(meterName);

const runtimeSpecsGauge = meter.createObservableGauge(
METRIC_TYPES.runtime_specs,
{
description: 'Runtime sepcification of this program',
}
);
let currentUserPubkeyListLength = 0;

let healthStatus: HEALTH_STATUS = HEALTH_STATUS.Ok;
const healthStatusGauge = meter.createObservableGauge(
METRIC_TYPES.health_status,
const userPubkeyListLengthGauge = meter.createObservableGauge(
METRIC_TYPES.user_pubkey_list_length,
{
description: 'Health status of this program',
description: 'Number of user public keys in the list',
}
);
healthStatusGauge.addCallback((obs: ObservableResult) => {
obs.observe(healthStatus, {});

userPubkeyListLengthGauge.addCallback((obs: ObservableResult) => {
obs.observe(currentUserPubkeyListLength);
});

let lastWsMsgReceivedTs = 0;
const setLastReceivedWsMsgTs = (ts: number) => {
lastWsMsgReceivedTs = ts;
const updateUserPubkeyListLength = (length: number) => {
currentUserPubkeyListLength = length;
};
const lastWsReceivedTsGauge = meter.createObservableGauge(
METRIC_TYPES.last_ws_message_received_ts,
{
description: 'Timestamp of last received websocket message',
}
);
lastWsReceivedTsGauge.addCallback((obs: ObservableResult) => {
obs.observe(lastWsMsgReceivedTs, {});
});

const cacheHitCounter = meter.createCounter(METRIC_TYPES.cache_hit_count, {
description: 'Total redis cache hits',
});
let healthStatus: HEALTH_STATUS = HEALTH_STATUS.Ok;

const accountUpdatesCounter = meter.createCounter(
METRIC_TYPES.account_updates_count,
const healthStatusGauge = meter.createObservableGauge(
METRIC_TYPES.health_status,
{
description: 'Total accounts update',
description: 'Current health status of the server',
}
);

const currentSystemTsGauge = meter.createObservableGauge(
METRIC_TYPES.current_system_ts,
{
description: 'Timestamp of system at time of metric collection',
}
);
currentSystemTsGauge.addCallback((obs: ObservableResult) => {
obs.observe(Date.now(), {});
healthStatusGauge.addCallback((obs: ObservableResult) => {
obs.observe(healthStatus);
});

const endpointResponseTimeHistogram = meter.createHistogram(
METRIC_TYPES.endpoint_response_times_histogram,
{
description: 'Duration of endpoint responses',
unit: 'ms',
}
);
const gpaFetchDurationHistogram = meter.createHistogram(
METRIC_TYPES.gpa_fetch_duration,
{
description: 'Duration of GPA fetches',
unit: 'ms',
}
);

const responseStatusCounter = meter.createCounter(
METRIC_TYPES.endpoint_response_status,
{
description: 'Count of endpoint responses by status code',
}
);

const healthCheckInterval = 2000;
let lastHealthCheckSlot = -1;
let lastHealthCheckState = true; // true = healthy, false = unhealthy
Expand Down Expand Up @@ -240,13 +153,4 @@ const handleHealthCheck = (core: Core) => {
};
};

export {
endpointResponseTimeHistogram,
gpaFetchDurationHistogram,
responseStatusCounter,
handleHealthCheck,
setLastReceivedWsMsgTs,
accountUpdatesCounter,
cacheHitCounter,
runtimeSpecsGauge,
};
export { handleHealthCheck, updateUserPubkeyListLength };
26 changes: 0 additions & 26 deletions src/core/middleware.ts

This file was deleted.

Loading

0 comments on commit 8036c0a

Please sign in to comment.