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

implement mint time metrics #223

Merged
merged 10 commits into from
Sep 14, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2022-12-11
toolchain: 1.71.0
override: true
components: rustfmt, clippy

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target/
.env.local
keypair.json
.vscode
.vscode
133 changes: 127 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["api", "migration"]
resolver = "2"
resolver = "2"
5 changes: 2 additions & 3 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ strum = { version = "0.24.1", features = ["derive"] }

[dependencies.hub-core]
package = "holaplex-hub-core"
version = "0.5.4"
version = "0.5.5"
git = "https://github.com/holaplex/hub-core"
branch = "stable"
features = ["kafka", "credits", "asset_proxy", "sea-orm"]
features = ["kafka", "credits", "asset_proxy", "sea-orm", "metrics"]

[build-dependencies.hub-core-build]
package = "holaplex-hub-core-build"
Expand Down
20 changes: 18 additions & 2 deletions api/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use hub_core::{
chrono::{DateTime, NaiveDateTime, Offset, Utc},
credits::{CreditsClient, TransactionId},
metrics::KeyValue,
prelude::*,
producer::Producer,
thiserror,
Expand All @@ -26,6 +27,7 @@ use crate::{
sea_orm_active_enums::{Blockchain, CreationStatus},
switch_collection_histories, transfer_charges, update_histories,
},
metrics::Metrics,
proto::{
nft_events::Event as NftEvent,
polygon_nft_events::Event as PolygonNftEvents,
Expand Down Expand Up @@ -113,6 +115,7 @@ pub struct Processor {
pub db: Connection,
pub credits: CreditsClient<Actions>,
pub producer: Producer<NftEvents>,
pub metrics: Metrics,
}

#[derive(Clone)]
Expand Down Expand Up @@ -151,16 +154,17 @@ impl Processor {
db: Connection,
credits: CreditsClient<Actions>,
producer: Producer<NftEvents>,
metrics: Metrics,
) -> Self {
Self {
db,
credits,
producer,
metrics,
}
}

#[allow(clippy::too_many_lines)]

/// Processes incoming messages related to different services like Treasury and Solana.
/// Routes each message to the corresponding handler based on the type of service and the specific event.

Expand Down Expand Up @@ -817,6 +821,17 @@ impl Processor {
creation_status = NftCreationStatus::Failed;
}

let now = Utc::now();
let elapsed = now
.signed_duration_since(collection_mint.created_at)
.num_milliseconds();
self.metrics
.mint_duration_ms_bucket
.record(elapsed, &[KeyValue::new(
"status",
creation_status.as_str_name(),
)]);

self.producer
.send(
Some(&NftEvents {
Expand Down Expand Up @@ -863,7 +878,8 @@ impl Processor {
}

async fn mint_updated(&self, id: String, payload: UpdateResult) -> ProcessResult<()> {
let update_history = UpdateHistories::find_by_id(Uuid::from_str(&id)?)
let id: Uuid = id.parse()?;
let update_history = UpdateHistories::find_by_id(id)
.one(self.db.get())
.await?
.ok_or(ProcessorErrorKind::DbMissingUpdateHistory)?;
Expand Down
20 changes: 17 additions & 3 deletions api/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use async_graphql::http::{playground_source, GraphQLPlaygroundConfig};
use async_graphql_poem::{GraphQLRequest, GraphQLResponse};
use hub_core::anyhow::Result;
use hub_core::{
anyhow::Result,
metrics::{Encoder, TextEncoder},
};
use poem::{
handler,
http::StatusCode,
web::{Data, Html},
IntoResponse,
};

use crate::{AppContext, AppState, Balance, OrganizationId, UserID};
use crate::{AppContext, AppState, Balance, Metrics, OrganizationId, UserID};

#[handler]
pub fn health() {}
pub fn health() -> StatusCode {
StatusCode::OK
}

#[handler]
pub fn playground() -> impl IntoResponse {
Expand Down Expand Up @@ -42,3 +48,11 @@ pub async fn graphql_handler(
.await
.into())
}

#[handler]
pub fn metrics_handler(Data(metrics): Data<&Metrics>) -> Result<String> {
let mut buffer = vec![];
let encoder = TextEncoder::new();
encoder.encode(&metrics.registry.gather(), &mut buffer)?;
Ok(String::from_utf8_lossy(&buffer).into_owned())
}
5 changes: 5 additions & 0 deletions api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod entities;
pub mod events;
pub mod handlers;
pub mod metadata_json;
pub mod metrics;
pub mod mutations;
pub mod nft_storage;
pub mod objects;
Expand Down Expand Up @@ -41,6 +42,7 @@ use hub_core::{
tokio,
uuid::Uuid,
};
use metrics::Metrics;
use mutations::Mutation;
use nft_storage::NftStorageClient;
use poem::{async_trait, FromRequest, Request, RequestBody};
Expand Down Expand Up @@ -239,6 +241,7 @@ pub struct AppState {

impl AppState {
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn new(
schema: AppSchema,
connection: Connection,
Expand Down Expand Up @@ -292,6 +295,7 @@ pub struct AppContext {

impl AppContext {
#[must_use]
#[allow(clippy::similar_names)]
pub fn new(
db: Connection,
user_id: UserID,
Expand Down Expand Up @@ -338,6 +342,7 @@ impl AppContext {
DataLoader::new(CollectionMintTransfersLoader::new(db.clone()), tokio::spawn);
let switch_collection_history_loader =
DataLoader::new(SwitchCollectionHistoryLoader::new(db.clone()), tokio::spawn);

Self {
db,
user_id,
Expand Down
Loading
Loading