voxpupuli / puppet-grafana

Puppet module to manage Grafana
Apache License 2.0
15 stars 106 forks source link

Fix performance of `grafana_user` #285

Closed alexjfisher closed 2 years ago

alexjfisher commented 2 years ago

Previously, the number of API calls to look up the details of a single user was equal to 1 + the number of existing users. This made managing multiple users exponentially slow.

The way the types in this module are written, (with the Grafana API user credentials being parameters of each resource), we can't implement prefetch to fetch all users just once; but we can change the implementation to directly lookup the user we're trying to manage.

This is much faster!

alexjfisher commented 2 years ago

For reference, using the following puppet code and puppet apply to talk to a remote grafana server (in the same datacenter, but not as quick as localhost with no ssl!).

grafana_user { range("testuser00","testuser99"):
  ensure           => present,                                                                                                                                                                                               
  grafana_url      => 'https://remotegrafana.example.com',                                                                                                                                                               
  grafana_user     => 'admin',                                                                                                                                                                                                            
  grafana_password => 'admin',                                                                                                                                                                                                            }

Before change...

Creating 100 users Notice: Applied catalog in 126.45 seconds

Running Puppet again (ensuring those 100 users are still present) Notice: Applied catalog in 224.31 seconds

Second run is slower as the average number of grafana users is > 100 for the entire duration, and not ~ 50.

After change... 1st puppet run Notice: Applied catalog in 5.58 seconds 2nd puppet run Notice: Applied catalog in 2.11 seconds

ie 100 times faster for this use case of just 100 users.