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

[red-knot] Add metrics collection #16005

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Start recording some metrics
  • Loading branch information
dcreager committed Feb 6, 2025
commit 1c71f9b8c41445401daae6c038a629f4c5674ff7
25 changes: 18 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ lsp-types = { git = "https://github.com/astral-sh/lsp-types.git", rev = "3512a9f
] }
matchit = { version = "0.8.1" }
memchr = { version = "2.7.1" }
metrics = { version = "0.24.1" }
mimalloc = { version = "0.1.39" }
natord = { version = "1.0.9" }
notify = { version = "8.0.0" }
Expand Down
1 change: 1 addition & 0 deletions crates/red_knot_python_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ countme = { workspace = true }
drop_bomb = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
metrics = { workspace = true }
ordermap = { workspace = true }
salsa = { workspace = true }
thiserror = { workspace = true }
Expand Down
11 changes: 11 additions & 0 deletions crates/red_knot_python_semantic/src/semantic_index/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ pub(super) struct SemanticIndexBuilder<'db> {
// Builder state
db: &'db dyn Db,
file: File,
// A shared clone of the path of the file being analyzed. We use this as a label for all of the
// metrics that we export, and this avoids cloning the path into a new string each time.
file_path: Arc<str>,
module: &'db ParsedModule,
scope_stack: Vec<ScopeInfo>,
/// The assignments we're currently visiting, with
Expand Down Expand Up @@ -95,9 +98,11 @@ pub(super) struct SemanticIndexBuilder<'db> {

impl<'db> SemanticIndexBuilder<'db> {
pub(super) fn new(db: &'db dyn Db, file: File, parsed: &'db ParsedModule) -> Self {
let file_path = Arc::from(file.path(db).as_str());
let mut builder = Self {
db,
file,
file_path,
module: parsed,
scope_stack: Vec::new(),
current_assignments: vec![],
Expand Down Expand Up @@ -175,6 +180,12 @@ impl<'db> SemanticIndexBuilder<'db> {
}

fn push_scope_with_parent(&mut self, node: NodeWithScopeRef, parent: Option<FileScopeId>) {
metrics::counter!(
"semantic_index.scope_count",
"file" => self.file_path.clone(),
)
.increment(1);

let children_start = self.scopes.next_index() + 1;

#[allow(unsafe_code)]
Expand Down