Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds prom client and initial set of metrics #15

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"ioredis": "^5.3.2",
"pino": "^8.15.3",
"pino-pretty": "^10.2.0",
"prom-client": "^15.1.0",
"stellar-sdk": "11.1.0",
"yargs": "^17.7.2"
},
Expand Down
8 changes: 6 additions & 2 deletions src/helper/test-helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Client, fetchExchange } from "@urql/core";
import pino from "pino";
import { nativeToScVal } from "stellar-sdk";
import Prometheus from "prom-client";

import { mutation, query } from "../service/mercury/queries";
import { MercuryClient } from "../service/mercury";
Expand Down Expand Up @@ -254,12 +255,15 @@ jest.spyOn(client, "mutation").mockImplementation((_mutation: any): any => {
}
});

const register = new Prometheus.Registry();

const mockMercuryClient = new MercuryClient(
"http://example.com/graphql",
mercurySession,
client,
renewClient,
testLogger
testLogger,
register
);

jest
Expand All @@ -274,7 +278,7 @@ jest
}
);
async function getDevServer() {
const server = initApiServer(mockMercuryClient, testLogger);
const server = initApiServer(mockMercuryClient, testLogger, register);
await server.listen();
return server;
}
Expand Down
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expand } from "dotenv-expand";
import yargs from "yargs";
import { Client, fetchExchange } from "@urql/core";
import Redis from "ioredis";
import Prometheus from "prom-client";

import { logger } from "./logger";
import { buildConfig } from "./config";
Expand Down Expand Up @@ -37,6 +38,12 @@ async function main() {
const env = argv.env || "development";
const port = argv.port || 3002;

const register = new Prometheus.Registry();
register.setDefaultLabels({
app: "freighter-backend",
});
Prometheus.collectDefaultMetrics({ register });

const client = new Client({
url: conf.mercuryGraphQL,
exchanges: [fetchExchange],
Expand Down Expand Up @@ -82,9 +89,10 @@ async function main() {
client,
renewClient,
logger,
register,
redis
);
const server = initApiServer(mercuryClient, logger, redis);
const server = initApiServer(mercuryClient, logger, register, redis);

try {
await server.listen({ port, host: "0.0.0.0" });
Expand Down
11 changes: 11 additions & 0 deletions src/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import helmet from "@fastify/helmet";
import rateLimiter from "@fastify/rate-limit";
import { Logger } from "pino";
import { Redis } from "ioredis";
import Prometheus from "prom-client";

import { MercuryClient } from "../service/mercury";
import { ajv } from "./validators";
Expand Down Expand Up @@ -30,6 +31,7 @@ const API_VERSION = "v1";
export function initApiServer(
mercuryClient: MercuryClient,
logger: Logger,
register: Prometheus.Registry,
redis?: Redis
) {
const server = Fastify({
Expand All @@ -55,6 +57,15 @@ export function initApiServer(
},
});

instance.route({
method: "GET",
url: "/metrics",
handler: async (_request, reply) => {
reply.header("Content-Type", register.contentType);
register.metrics().then((data) => reply.code(200).send(data));
},
});
Comment on lines +60 to +67
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which port will metrics be exposed on?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 8080 right now

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I this separate from the main application port? If you use the same port it means that metrics might get exposed on the internet.
It's often useful expose metrics on a separate port

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah good point, it is on the same port as the app so I'll update that to a metrics specific port. Thank you!


instance.route({
method: "GET",
url: "/account-history/:pubKey",
Expand Down
46 changes: 46 additions & 0 deletions src/service/mercury/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Logger } from "pino";
import { Address, Horizon, xdr } from "stellar-sdk";
import { Redis } from "ioredis";
import BigNumber from "bignumber.js";
import Prometheus from "prom-client";

import { mutation, query } from "./queries";
import {
Expand Down Expand Up @@ -76,13 +77,16 @@ export class MercuryClient {
accountSubUrl: string;
redisClient?: Redis;
logger: Logger;
mercuryErrorCounter: Prometheus.Counter<"endpoint">;
rpcErrorCounter: Prometheus.Counter<"rpc">;

constructor(
mercuryUrl: string,
mercurySession: MercurySession,
urqlClient: Client,
renewClient: Client,
logger: Logger,
register: Prometheus.Registry,
redisClient?: Redis
) {
this.mercuryUrl = mercuryUrl;
Expand All @@ -94,6 +98,19 @@ export class MercuryClient {
this.renewClient = renewClient;
this.logger = logger;
this.redisClient = redisClient;

this.mercuryErrorCounter = new Prometheus.Counter({
name: "freighter_backend_mercury_error_count",
help: "Count of errors returned from Mercury",
labelNames: ["endpoint"],
});

this.rpcErrorCounter = new Prometheus.Counter({
name: "freighter_backend_rpc_error_count",
help: "Count of errors returned from Horizon or Soroban RPCs",
labelNames: ["rpc"],
});
register.registerMetric(this.mercuryErrorCounter);
}

tokenBalanceKey = (pubKey: string) => {
Expand Down Expand Up @@ -406,6 +423,11 @@ export class MercuryClient {
} catch (error) {
this.logger.error(error);
const _error = JSON.stringify(error);
this.rpcErrorCounter
.labels({
rpc: "Horizon",
})
.inc();
return {
data: null,
error: _error,
Expand Down Expand Up @@ -453,6 +475,13 @@ export class MercuryClient {

if (!response.error) {
return response;
} else {
this.logger.error(response.error);
this.mercuryErrorCounter
.labels({
endpoint: "getAccountHistory",
})
.inc();
}
}

Expand Down Expand Up @@ -616,6 +645,13 @@ export class MercuryClient {
// if Mercury returns an error, fallback to the RPCs
if (!response.error) {
return response;
} else {
this.logger.error(response.error);
this.mercuryErrorCounter
.labels({
endpoint: "getAccountBalance",
})
.inc();
}
}

Expand All @@ -636,6 +672,11 @@ export class MercuryClient {
this.logger.error(
`failed to fetch token classic balances from Horizon: ${pubKey}, ${network}`
);
this.rpcErrorCounter
.labels({
rpc: "Horizon",
})
.inc();
return {
data: null,
error,
Expand All @@ -654,6 +695,11 @@ export class MercuryClient {
this.logger.error(
`failed to fetch token token balances from Soroban RPC: ${pubKey}, ${network}`
);
this.rpcErrorCounter
.labels({
rpc: "Soroban",
})
.inc();
return {
data: null,
error,
Expand Down
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,11 @@
"@jridgewell/resolve-uri" "^3.1.0"
"@jridgewell/sourcemap-codec" "^1.4.14"

"@opentelemetry/api@^1.4.0":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.7.0.tgz#b139c81999c23e3c8d3c0a7234480e945920fc40"
integrity sha512-AdY5wvN0P2vXBi3b29hxZgSFvdhdxPB9+f0B6s//P9Q8nibRWeA3cHm8UmLpio9ABigkVHJ5NMPk+Mz8VCCyrw==

"@sinclair/typebox@^0.27.8":
version "0.27.8"
resolved "https://registry.yarnpkg.com/@sinclair/typebox/-/typebox-0.27.8.tgz#6667fac16c436b5434a387a34dedb013198f6e6e"
Expand Down Expand Up @@ -1258,6 +1263,11 @@ bignumber.js@^9.1.2:
resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c"
integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==

[email protected]:
version "1.0.2"
resolved "https://registry.yarnpkg.com/bintrees/-/bintrees-1.0.2.tgz#49f896d6e858a4a499df85c38fb399b9aff840f8"
integrity sha512-VOMgTMwjAaUG580SXn3LacVgjurrbMme7ZZNYGSSV7mmtY6QQRh0Eg3pwIcntQ77DErK1L0NxkbetjcoXzVwKw==

brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
Expand Down Expand Up @@ -3090,6 +3100,14 @@ process@^0.11.10:
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
integrity sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==

prom-client@^15.1.0:
version "15.1.0"
resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-15.1.0.tgz#816a4a2128da169d0471093baeccc6d2f17a4613"
integrity sha512-cCD7jLTqyPdjEPBo/Xk4Iu8jxjuZgZJ3e/oET3L+ZwOuap/7Cw3dH/TJSsZKs1TQLZ2IHpIlRAKw82ef06kmMw==
dependencies:
"@opentelemetry/api" "^1.4.0"
tdigest "^0.1.1"

prompts@^2.0.1:
version "2.4.2"
resolved "https://registry.yarnpkg.com/prompts/-/prompts-2.4.2.tgz#7b57e73b3a48029ad10ebd44f74b01722a4cb069"
Expand Down Expand Up @@ -3537,6 +3555,13 @@ tapable@^2.1.1, tapable@^2.2.0:
resolved "https://registry.yarnpkg.com/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0"
integrity sha512-GNzQvQTOIP6RyTfE2Qxb8ZVlNmw0n88vp1szwWRimP02mnTsx3Wtn5qRdqY9w2XduFNUgvOwhNnQsjwCp+kqaQ==

tdigest@^0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/tdigest/-/tdigest-0.1.2.tgz#96c64bac4ff10746b910b0e23b515794e12faced"
integrity sha512-+G0LLgjjo9BZX2MfdvPfH+MKLCrxlXSYec5DaPYP1fe6Iyhf0/fSmJ0bFiZ1F8BT6cGXl2LpltQptzjXKWEkKA==
dependencies:
bintrees "1.0.2"

terser-webpack-plugin@^5.3.7:
version "5.3.9"
resolved "https://registry.yarnpkg.com/terser-webpack-plugin/-/terser-webpack-plugin-5.3.9.tgz#832536999c51b46d468067f9e37662a3b96adfe1"
Expand Down
Loading