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

feat: add db metrics #445

Merged
merged 9 commits into from
Mar 24, 2025
Merged

feat: add db metrics #445

merged 9 commits into from
Mar 24, 2025

Conversation

anihamde
Copy link
Contributor

This PR adds two sets of metrics for substantive db queries to the bid, opportunity, and auction tables:

  • a counter metric for the total number of queries
  • a histogram metric for the query latency

Copy link

vercel bot commented Mar 21, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
swap-staging ⬜️ Ignored (Inspect) Visit Preview Mar 24, 2025 8:39am

fields(
category = "db_queries",
result = "success",
name = "add_auction",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe we should name this something else, to distinguish it from the instrument name. This name field influences the name of the metrics, but the instrument/span name is what determines the naming of the traces.

@@ -86,7 +86,7 @@ async fn main() -> Result<()> {
.compact()
.with_filter(LevelFilter::INFO)
.with_filter(filter::filter_fn(|metadata| {
!is_metrics(metadata) && is_internal(metadata)
is_internal(metadata) || is_metrics(metadata, true)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

later we can add more flags to distinguish btwn logging and telemetry

@@ -678,37 +697,59 @@ impl<T: ChainTrait> Database<T> for DB {
bid.profile_id,
serde_json::to_value(bid.metadata.clone()).expect("Failed to serialize metadata"),
).execute(self)
.instrument(info_span!("db_add_bid"))
.await.map_err(|e| {
.await {
Copy link
Contributor

Choose a reason for hiding this comment

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

why not map_err with ? instead of using the if let?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes that makes sense, changed back to this format

@@ -648,8 +645,18 @@ pub trait Database<T: ChainTrait>: Debug + Send + Sync + 'static {

#[async_trait]
impl<T: ChainTrait> Database<T> for DB {
#[instrument(
Copy link
Contributor

Choose a reason for hiding this comment

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

RN in the traces, the name is add_auction. I think it's a good idea to set the name as db_add_auction in the traces. In the metrics I think it's good to keep the name as it is RN, because all of these queries are in the same category (db_queries).

If you set the name in the outer scope (same level as the target), it'll be used for tracing. the name in the fields variable is being used only by metrics as the traces is not using this field name for the trace name :D

So you can update the code like below and this issue should be fixed:

#[instrument(
        target = "metrics",
        name = "db_add_auction",
        fields(
            category = "db_queries",
            result = "success",
            name = "add_auction",
            tracing_enabled
        ),
        skip_all
    )]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup sgtm

@@ -229,12 +247,12 @@ impl<T: InMemoryStore> Database<T> for DB {
}
query.push(" ORDER BY creation_time ASC LIMIT ");
query.push_bind(super::OPPORTUNITY_PAGE_SIZE_CAP as i64);
let opps: Vec<models::Opportunity<<T::Opportunity as entities::Opportunity>::ModelMetadata>> = query
let opps: Vec<models::Opportunity<<T::Opportunity as entities::Opportunity>::ModelMetadata>,> = query
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need that comma?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, addressing

@anihamde anihamde merged commit 358eedc into main Mar 24, 2025
3 checks passed
@anihamde anihamde deleted the feat/db-metrics branch March 24, 2025 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants