| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | package plugins | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-31 20:05:52 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2017-12-16 00:17:58 +08:00
										 |  |  | 	"path" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/backendplugin" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-29 00:25:35 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util/errutil" | 
					
						
							| 
									
										
										
										
											2019-10-24 23:15:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-13 14:45:54 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2017-12-16 00:17:58 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2018-01-17 18:37:37 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/datasource/wrapper" | 
					
						
							| 
									
										
										
										
											2017-12-16 00:17:58 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/tsdb" | 
					
						
							| 
									
										
										
										
											2017-08-31 20:05:52 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-20 23:07:17 +08:00
										 |  |  | // DataSourcePlugin contains all metadata about a datasource plugin
 | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | type DataSourcePlugin struct { | 
					
						
							|  |  |  | 	FrontendPluginBase | 
					
						
							| 
									
										
										
										
											2019-10-31 00:51:13 +08:00
										 |  |  | 	Annotations  bool              `json:"annotations"` | 
					
						
							|  |  |  | 	Metrics      bool              `json:"metrics"` | 
					
						
							|  |  |  | 	Alerting     bool              `json:"alerting"` | 
					
						
							|  |  |  | 	Explore      bool              `json:"explore"` | 
					
						
							|  |  |  | 	Table        bool              `json:"tables"` | 
					
						
							|  |  |  | 	Logs         bool              `json:"logs"` | 
					
						
							| 
									
										
										
										
											2020-03-25 19:25:39 +08:00
										 |  |  | 	Tracing      bool              `json:"tracing"` | 
					
						
							| 
									
										
										
										
											2019-10-31 00:51:13 +08:00
										 |  |  | 	QueryOptions map[string]bool   `json:"queryOptions,omitempty"` | 
					
						
							|  |  |  | 	BuiltIn      bool              `json:"builtIn,omitempty"` | 
					
						
							|  |  |  | 	Mixed        bool              `json:"mixed,omitempty"` | 
					
						
							|  |  |  | 	Routes       []*AppPluginRoute `json:"routes"` | 
					
						
							|  |  |  | 	Streaming    bool              `json:"streaming"` | 
					
						
							| 
									
										
										
										
											2017-12-16 00:17:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Backend    bool   `json:"backend,omitempty"` | 
					
						
							|  |  |  | 	Executable string `json:"executable,omitempty"` | 
					
						
							| 
									
										
										
										
											2019-10-24 23:15:27 +08:00
										 |  |  | 	SDK        bool   `json:"sdk,omitempty"` | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-14 03:18:45 +08:00
										 |  |  | func (p *DataSourcePlugin) Load(decoder *json.Decoder, pluginDir string, backendPluginManager backendplugin.Manager) error { | 
					
						
							| 
									
										
										
										
											2019-10-29 00:25:35 +08:00
										 |  |  | 	if err := decoder.Decode(p); err != nil { | 
					
						
							|  |  |  | 		return errutil.Wrapf(err, "Failed to decode datasource plugin") | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 18:03:12 +08:00
										 |  |  | 	if err := p.registerPlugin(pluginDir); err != nil { | 
					
						
							| 
									
										
										
										
											2019-10-29 00:25:35 +08:00
										 |  |  | 		return errutil.Wrapf(err, "Failed to register plugin") | 
					
						
							| 
									
										
										
										
											2016-02-10 18:03:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | 	if p.Backend { | 
					
						
							|  |  |  | 		cmd := ComposePluginStartCommmand(p.Executable) | 
					
						
							|  |  |  | 		fullpath := path.Join(p.PluginDir, cmd) | 
					
						
							| 
									
										
										
										
											2020-01-14 00:13:17 +08:00
										 |  |  | 		descriptor := backendplugin.NewBackendPluginDescriptor(p.Id, fullpath, backendplugin.PluginStartFuncs{ | 
					
						
							|  |  |  | 			OnLegacyStart: p.onLegacyPluginStart, | 
					
						
							|  |  |  | 			OnStart:       p.onPluginStart, | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-01-14 03:18:45 +08:00
										 |  |  | 		if err := backendPluginManager.Register(descriptor); err != nil { | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | 			return errutil.Wrapf(err, "Failed to register backend plugin") | 
					
						
							| 
									
										
										
										
											2019-10-12 03:02:15 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-12 03:02:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | 	DataSources[p.Id] = p | 
					
						
							| 
									
										
										
										
											2019-10-12 03:02:15 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2017-12-22 22:40:45 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-30 00:22:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-14 00:13:17 +08:00
										 |  |  | func (p *DataSourcePlugin) onLegacyPluginStart(pluginID string, client *backendplugin.LegacyClient, logger log.Logger) error { | 
					
						
							|  |  |  | 	tsdb.RegisterTsdbQueryEndpoint(pluginID, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) { | 
					
						
							|  |  |  | 		return wrapper.NewDatasourcePluginWrapper(logger, client.DatasourcePlugin), nil | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2017-12-16 00:17:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-14 00:13:17 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-10-24 23:15:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-14 00:13:17 +08:00
										 |  |  | func (p *DataSourcePlugin) onPluginStart(pluginID string, client *backendplugin.Client, logger log.Logger) error { | 
					
						
							| 
									
										
										
										
											2020-03-10 19:59:03 +08:00
										 |  |  | 	if client.DataPlugin != nil { | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | 		tsdb.RegisterTsdbQueryEndpoint(pluginID, func(dsInfo *models.DataSource) (tsdb.TsdbQueryEndpoint, error) { | 
					
						
							| 
									
										
										
										
											2020-03-10 19:59:03 +08:00
										 |  |  | 			return wrapper.NewDatasourcePluginWrapperV2(logger, p.Id, p.Type, client.DataPlugin), nil | 
					
						
							| 
									
										
										
										
											2019-10-24 23:15:27 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-01-09 00:43:28 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-12-16 00:17:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |