grafana / sqlds

A package that assists writing SQL-driven datasources
Apache License 2.0
20 stars 12 forks source link

Cache connections based on the datasource updated timestamp #111

Closed njvrzm closed 9 months ago

njvrzm commented 9 months ago

An issue was created for athena-datasource (https://github.com/grafana/athena-datasource/issues/264) where if you rotate AWS credentials and then edit them in the datasource config in grafana, the new credentails aren't used until the datasource is recreated (or the server restarted). This is because database connections there are cached based only on the datasource UID and (if present) the connection arguments.

The grafana-aws-sdk uses a cache mechanism copied from sqlds, and the same issue is present in all sqlds-based (and awsds-based) datasources as well. To address this I've updated sqlds to include the Updated timestamp from the datasource settings in its cache key, and to expose the key mechanism so athena can use it instead of duplicated code.

This does, unfortunately, slightly change the sqlds API - the GetDBFromQuery method now takes backend.DataSourceInstanceSettings instead of a string for the datasource UID. However, as far as I can tell (searching github and google both) this method is only used in grafana-aws-sdk, in one place. Personally I think it's probably okay to keep this at v3 despite the API change, but please let me know if you think we need to bump to v4 instead.

njvrzm commented 9 months ago

As it turns out, while this works, it's unnecessarily deep - the problem is actually with how some aws datasources respond to the NewDatasource call from the manager (thanks @gabor for the help figuring this out!). Closing this one.