Skip to content

Add db entry metrics#1412

Open
zealsham wants to merge 1 commit intopayjoin:masterfrom
zealsham:db-metrics
Open

Add db entry metrics#1412
zealsham wants to merge 1 commit intopayjoin:masterfrom
zealsham:db-metrics

Conversation

@zealsham
Copy link
Collaborator

@zealsham zealsham commented Mar 12, 2026

this pr addresses the db entries part of #941 , it adds db entries metrics seperated by version (v1/v2). For now it's just simple incremental counter seperated by v1/v2.

Pull Request Checklist

Please confirm the following before requesting review:

@coveralls
Copy link
Collaborator

coveralls commented Mar 12, 2026

Pull Request Test Coverage Report for Build 23269894458

Details

  • 90 of 96 (93.75%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.09%) to 84.215%

Changes Missing Coverage Covered Lines Changed/Added Lines %
payjoin-mailroom/src/lib.rs 9 15 60.0%
Totals Coverage Status
Change from base Build 23193941891: 0.09%
Covered Lines: 10729
Relevant Lines: 12740

💛 - Coveralls

@zealsham zealsham requested a review from spacebear21 March 13, 2026 12:08
Copy link
Collaborator

@spacebear21 spacebear21 left a comment

Choose a reason for hiding this comment

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

cACK, but I have a suggestion for a different approach.

Now that the directory lives in payjoin-mailroom, we should be able to wrap a Metrics wrapper around the DB following a middleware pattern. Something like this:

// db/mod.rs

  /// Db decorator that records write metrics.
  #[derive(Clone)]
  pub struct MetricsDb<D: Db> {
      inner: D,
      metrics: MetricsService,
  }

  impl<D: Db> Db for MetricsDb<D> {
      type OperationalError = D::OperationalError;

      async fn post_v2_payload(
          &self,
          mailbox_id: &ShortId,
          data: Vec<u8>,
      ) -> Result<Option<()>, Error<Self::OperationalError>> {
          let result = self.inner.post_v2_payload(mailbox_id, data).await?;
          self.metrics.record_db_entry("2");
          Ok(result)
      }
// ...
}

// lib.rs
struct Services {
      directory: directory::Service<MetricsDb<DbServiceAdapter>>,
      relay: ohttp_relay::Service,
      metrics: MetricsService,
      // ...
  }

async fn init_directory(
      config: &Config,
      sentinel_tag: SentinelTag,
      metrics: MetricsService,
  ) -> anyhow::Result<directory::Service<MetricsDb<DbServiceAdapter>>> {
      let files_db = FilesDb::init(config.timeout, config.storage_dir.clone()).await?;
      files_db.spawn_background_prune().await;
      let db = MetricsDb::new(DbServiceAdapter::new(files_db), metrics);
      // ...
  }

This keeps metrics out of the directory and follows a more modular approach in line with tower patterns.

@zealsham zealsham force-pushed the db-metrics branch 2 times, most recently from f21c0df to ad71757 Compare March 17, 2026 10:05
let geoip = init_geoip(&config).await?;

let directory = init_directory(&config, sentinel_tag).await?;
let directory = init_directory(&config, sentinel_tag, metrics.clone()).await?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This clones the metrics service, that would result in two separate instances of MetricsService tracking different things independently right? We should probably pass some type of reference to ensure there is only one source of truth.

this pr addresses payjoin#941 , it adds db entries metrics seperated
by version (v1/v2)
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