grafana/pkg
Erik Sundell 934a8f08ae
Stackdriver: Project selector (#22447)
* clean PR #17366

* udpate vendor

* [WIP] Implement projects management for stackdriver

* [WIP] Implement projects management for stackdriver

* [WIP] Implement projects management for stackdriver

* Implement projects management for stackdriver

* [WIP][Tests] Fix errors

* clean anonymous struct

* remove await

* don't store project list

* Add default project on query editor

* gofmt

* Fix tests

* Move test data source to backend

* Use segment instead of dropdown. remove ensure default project since it's not being used anymore.

* Fix broken annotation editor

* Load gceDefaultAccount only once when in the config page

* Reset error message on auth type change

* Add metric find query for projects

* Remove debug code

* Fix broken tests

* Fix typings

* Fix lint error

* Slightly different approach - now having a distiction between config page default project, and project that is selectable from the dropdown in the query editor.

* Fix broken tests

* Attempt to fix strict ts errors

* Prevent state from being set multiple times

* Remove noOptionsMessage since it seems to be obosolete in react select

* One more attempt to solve ts strict error

* Interpolate project template variable. Make sure its loaded correctly when opening variable query editor first time

* Implicit any fix

* fix: typescript strict null check fixes

* Return empty array in case project endpoint fails

* Rename project to projectName to prevent clashing with legacy query prop

* Fix broken test

* fix: Stackdriver - template replace on filter label

should have a regex format as that escapes the dots
in the label name which is not valid.

Co-authored-by: Labesse Kévin <kevin@labesse.me>
Co-authored-by: Elias Cédric Laouiti <elias@abtasty.com>
Co-authored-by: Daniel Lee <dan.limerick@gmail.com>
2020-03-02 09:31:09 -05:00
..
api API: Include IP address when logging request error (#21596) 2020-03-02 09:43:16 +01:00
bus Bus: Remove unused wildcard handlers and clean up tests (#20327) 2019-11-13 04:02:44 -08:00
cmd grafana-cli: Upgrade to urfave/cli v2 (#22402) 2020-02-26 12:27:31 +01:00
components Alerting: Fix image rendering and uploading timeout preventing to send alert notifications (#21536) 2020-01-17 12:07:16 +01:00
events
extensions Admin: Add promotional page for Grafana Enterprise (#21422) 2020-01-15 14:50:44 +01:00
infra Auth: Azure AD OAuth (#20030) 2020-02-13 12:12:25 +03:00
login Azure OAuth: enable teamsync (#22160) 2020-02-14 14:03:00 +03:00
middleware chore: avoid aliasing models in middleware (#22484) 2020-02-28 12:50:58 +01:00
models Dashboard: Adds support for a global minimum dashboard refresh interval (#19416) 2020-02-28 14:32:01 +01:00
plugins chore: avoid alias for models in plugins (#22483) 2020-02-28 12:51:21 +01:00
registry add functionality to override service in registry 2018-10-30 13:37:30 +01:00
services chore: avoid aliasing imports in services (#22499) 2020-02-29 13:35:15 +01:00
setting Dashboard: Adds support for a global minimum dashboard refresh interval (#19416) 2020-02-28 14:32:01 +01:00
tsdb Stackdriver: Project selector (#22447) 2020-03-02 09:31:09 -05:00
util Util: Modify SplitHostPortDefault not to return an error for empty input (#20351) 2019-11-18 10:42:51 +02:00
README.md Docs: Consolidate backend guidelines (#19823) 2019-10-30 13:59:27 -07:00

Backend

This directory contains the code for the Grafana backend. This document gives an overview of the directory structure, and ongoing refactorings.

For more information on developing for the backend:

Central folders of Grafana's backend

folder description
/pkg/api HTTP handlers and routing. Almost all handler funcs are global which is something we would like to improve in the future. Handlers should be associated with a struct that refers to all dependencies.
/pkg/cmd The binaries that we build: grafana-server and grafana-cli.
/pkg/components A mix of third-party packages and packages we have implemented ourselves. Includes our packages that have out-grown the util package and don't naturally belong somewhere else.
/pkg/infra Packages in infra should be packages that are used in multiple places in Grafana without knowing anything about the Grafana domain.
/pkg/services Packages in services are responsible for peristing domain objects and manage the relationship between domain objects. Services should communicate with each other using DI when possible. Most of Grafana's codebase still relies on global state for this. Any new features going forward should use DI.
/pkg/tsdb All backend implementations of the data sources in Grafana. Used by both Grafana's frontend and alerting.
/pkg/util Small helper functions that are used in multiple parts of the codebase. Many functions are placed directly in the util folders which is something we want to avoid. Its better to give the util function a more descriptive package name. Ex errutil.

Central components of Grafana's backend

package description
/pkg/bus The bus is described in more details under Communication
/pkg/models This is where we keep our domain model. This package should not depend on any package outside standard library. It does contain some references within Grafana but that is something we should avoid going forward.
/pkg/registry Package for managing services.
/pkg/services/alerting Grafana's alerting services. The alerting engine runs in a separate goroutine and shouldn't depend on anything else within Grafana.
/pkg/services/sqlstore Currently where the database logic resides.
/pkg/setting Anything related to Grafana global configuration should be dealt with in this package.

Dependency management

Refer to UPGRADING_DEPENDENCIES.md.

Ongoing refactoring

These issues are not something we want to address all at once but something we will improve incrementally. Since Grafana is released at a regular schedule the preferred approach is to do this in batches. Not only is it easier to review, but it also reduces the risk of conflicts when cherry-picking fixes from master to release branches. Please try to submit changes that span multiple locations at the end of the release cycle. We prefer to wait until the end because we make fewer patch releases at the end of the release cycle, so there are fewer opportunities for complications.

Global state

Global state makes testing and debugging software harder and it's something we want to avoid when possible. Unfortunately, there is quite a lot of global state in Grafana.

We want to migrate away from this by using the inject package to wire up all dependencies either in pkg/cmd/grafana-server/main.go or self-registering using registry.RegisterService ex https://github.com/grafana/grafana/blob/master/pkg/services/cleanup/cleanup.go#L25.

Limit the use of the init() function

Only use the init() function to register services/implementations.

Settings refactoring

The plan is to move all settings to from package level vars in settings package to the setting.Cfg struct. To access the settings, services and components can inject this setting.Cfg struct:

Cfg struct Injection example

Reduce the use of GoConvey

We want to migrate away from using GoConvey. Instead, we want to use stdlib testing, because it's the most common approach in the Go community and we think it will be easier for new contributors. Read more about how we want to write tests in the style guide.

Refactor SqlStore

The sqlstore handlers all use a global xorm engine variable. Refactor them to use the SqlStore instance.

Avoid global HTTP handler functions

Refactor HTTP handlers so that the handler methods are on the HttpServer instance or a more detailed handler struct. E.g (AuthHandler). This ensures they get access to HttpServer service dependencies (and Cfg object) and can avoid global state.

Date comparison

Store newly introduced date columns in the database as epochs if they require date comparison. This permits a unified approach for comparing dates against all the supported databases instead of handling dates differently for each database. Also, by comparing epochs, we no longer need error pruning transformations to and from other time zones.

Provisionable*

All new features that require state should be possible to configure using config files. For example:

Today its only possible to provision data sources and dashboards but this is something we want to support all over Grafana.