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

Initial metrics support #545

Merged
merged 18 commits into from
Sep 1, 2022
Merged

Initial metrics support #545

merged 18 commits into from
Sep 1, 2022

Conversation

CHr15F0x
Copy link
Member

@CHr15F0x CHr15F0x commented Aug 29, 2022

This PR is the second attempt at adding metrics to pathfinder (feature request issue, first attempt),

  • It's based on the metrics and metrics-exporter-prometheus crates.
  • The default prometheus exporter is not used, instead the payload is rendered in the /metrics route.
  • Only the basic total call counters were added for RPC methods to keep the PR small.
  • RecorderGuard should be used when installing a per-test recorder, which takes care of recorder removal and avoids races between tests (metrics forces a global recorder instance per binary).
  • At the moment I'm using format!() to build the counter labels but this can be sped up ~5x if using a BTreeMap<&'static str, &'static str> for method name vs counter name lookup instead (benchmark is disposable so not included in the PR). I haven't added this yet as I assumed that this extra heap allocation is negligible in terms of performance when taking the DB operations into account.

@CHr15F0x CHr15F0x requested a review from a team as a code owner August 29, 2022 13:59
@koivunej
Copy link
Contributor

At least Cc: #258

Copy link
Contributor

@Mirko-von-Leipzig Mirko-von-Leipzig left a comment

Choose a reason for hiding this comment

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

No major complaints :)

Is there anything we can use to see what this produces externally?

Copy link
Contributor

@koivunej koivunej left a comment

Choose a reason for hiding this comment

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

I would hope that the RPC changes could be split off, done in a follow-up. I failed to see those were just really strangely diffed whitespace changes. Otherwise this looks good, would:

  • consider changing adding in a metrics prefix right away since this will become part of the "stable api" and we don't want to go back and forth with it often
  • would consider not initializing any recorders from main when monitoring addr is not specified, or at least commenting why it needs to be always added

I don't really know what would be a good way to handle the format! away. Cheapest at runtime might be https://docs.rs/phf/latest/phf/ but hope you kept that BTreeMap bench :) But yeah, it's followup stuff.

@CHr15F0x CHr15F0x force-pushed the CHr15F0x/metrics branch 3 times, most recently from f7b0834 to 3dc9ef7 Compare August 30, 2022 12:04
@CHr15F0x
Copy link
Member Author

CHr15F0x commented Aug 30, 2022

Once labels are used to differentiate between counters for different method calls there is no need for format!. I did a quick benchmark and method_name_as_non_static_str.to_owned() in on_call() turns out to be on par with the fastest of lookup alternatives (these include phf::[Ordered_]Set which surprisingly is not the fastest).

@CHr15F0x
Copy link
Member Author

@Mirko-von-Leipzig

Assuming monitoring is on port 9000 you can just:
curl localhost:9000/metrics
And you should see

# TYPE rpc_method_calls_total counter
rpc_method_calls_total{method="starknet_getTransactionByBlockIdAndIndex"} 0
rpc_method_calls_total{method="starknet_getBlockTransactionCount"} 0
rpc_method_calls_total{method="starknet_getTransactionByHash"} 0
rpc_method_calls_total{method="starknet_getEvents"} 0
rpc_method_calls_total{method="starknet_getClassHashAt"} 0
rpc_method_calls_total{method="starknet_addDeployTransaction"} 0
rpc_method_calls_total{method="starknet_getStateUpdate"} 1
rpc_method_calls_total{method="starknet_blockNumber"} 0
rpc_method_calls_total{method="starknet_chainId"} 0
rpc_method_calls_total{method="starknet_blockHashAndNumber"} 0
rpc_method_calls_total{method="starknet_addInvokeTransaction"} 0
rpc_method_calls_total{method="starknet_getClassAt"} 0
rpc_method_calls_total{method="starknet_getBlockWithTxHashes"} 0
rpc_method_calls_total{method="starknet_syncing"} 0
rpc_method_calls_total{method="starknet_pendingTransactions"} 0
rpc_method_calls_total{method="starknet_call"} 0
rpc_method_calls_total{method="starknet_getStorageAt"} 0
rpc_method_calls_total{method="starknet_getClass"} 0
rpc_method_calls_total{method="starknet_getBlockWithTxs"} 0
rpc_method_calls_total{method="starknet_addDeclareTransaction"} 0
rpc_method_calls_total{method="starknet_getNonce"} 0
rpc_method_calls_total{method="starknet_estimateFee"} 0
rpc_method_calls_total{method="starknet_getTransactionReceipt"} 0

Or better download and run prometheus with a small addition to the config file (prometheus.yml) in scrape_configs, for example:

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name:       'pathfinder'

    # Override the global default and scrape targets from this job every 5 seconds.
    scrape_interval: 5s

    static_configs:
      - targets: ['localhost:9000']

@CHr15F0x CHr15F0x force-pushed the CHr15F0x/metrics branch 2 times, most recently from b5cf45d to 4fa1c15 Compare August 31, 2022 08:24
@CHr15F0x CHr15F0x merged commit 35dfc06 into main Sep 1, 2022
@CHr15F0x CHr15F0x deleted the CHr15F0x/metrics branch September 1, 2022 16:10
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.

4 participants