@@ -42,7 +42,8 @@ class Agent(BaseAgent):
42
42
"pplEnabled" : True ,
43
43
"timeField" : "pscheduler.start_time" ,
44
44
"tlsAuth" : False ,
45
- "tlsSkipVerify" : True ,
45
+ "tlsSkipVerify" : True ,
46
+ "version" : "AUTO"
46
47
},
47
48
"readOnly" : False
48
49
}
@@ -413,8 +414,14 @@ def _gf_http(self, url_path, action, method="get", data={}):
413
414
General function for sending HTTP requests to Grafana and handling errors
414
415
'''
415
416
url = self ._gf_build_url (url_path )
417
+ return self ._http (url , action , method , data , headers = self .grafana_header , auth = self .grafana_auth , log_prefix = "grafana_" )
418
+
419
+ def _http (self , url , action , method = "get" , data = {}, headers = {}, auth = None , log_prefix = "" ):
420
+ '''
421
+ General function for sending HTTP requests and handling errors
422
+ '''
416
423
local_context = {}
417
- local_context ["grafana_url" ] = url
424
+ local_context ["{}url" . format ( log_prefix ) ] = url
418
425
local_context ["action" ] = "{}.start" .format (action )
419
426
if data :
420
427
local_context ["data" ] = data
@@ -424,13 +431,13 @@ def _gf_http(self, url_path, action, method="get", data={}):
424
431
try :
425
432
r = None
426
433
if method == "get" :
427
- r = requests .get (url , headers = self . grafana_header , auth = self . grafana_auth , verify = False )
434
+ r = requests .get (url , headers = headers , auth = auth , verify = False )
428
435
elif method == "post" :
429
- r = requests .post (url , json = data , headers = self . grafana_header , auth = self . grafana_auth , verify = False )
436
+ r = requests .post (url , json = data , headers = headers , auth = auth , verify = False )
430
437
elif method == "put" :
431
- r = requests .put (url , json = data , headers = self . grafana_header , auth = self . grafana_auth , verify = False )
438
+ r = requests .put (url , json = data , headers = headers , auth = auth , verify = False )
432
439
elif method == "delete" :
433
- r = requests .delete (url , headers = self . grafana_header , auth = self . grafana_auth , verify = False )
440
+ r = requests .delete (url , headers = headers , auth = auth , verify = False )
434
441
else :
435
442
return None , "Invalid method specified."
436
443
r .raise_for_status ()
@@ -529,13 +536,41 @@ def _build_ds_name(self, ds_url):
529
536
'''
530
537
return self .grafana_datasource_name_format .format (ds_url )
531
538
539
+ def _ds_version (self , ds_body ):
540
+ '''
541
+ Determine version of database for datasource. Required for Opensearch.
542
+ '''
543
+ #Detect database version if set to AUTO
544
+ if ds_body .get ("jsonData" , {}).get ("version" ,"" ).upper () != "AUTO" :
545
+ #nothing to do
546
+ return
547
+
548
+ #clear out AUTO in case something fails
549
+ ds_body ["jsonData" ]["version" ] = None
550
+ if ds_body ["type" ] != "grafana-opensearch-datasource" :
551
+ #only know how to get version for opensearch
552
+ self .logger .error (self .logf .format ("Datasource version set to AUTO but setting version not supported for datasource type {}. Manually change jsonData.version in config with {}" .format (ds_body ["type" ], self .PSCONFIG_KEY_GF_DS_SETTINGS )))
553
+ return
554
+
555
+ #fetch version
556
+ r , msg = self ._http (ds_body ["url" ], "ds_version" , log_prefix = "ds_" )
557
+ if msg :
558
+ #return if error making request
559
+ return
560
+
561
+ #parse version
562
+ ds_body ["jsonData" ]["version" ] = r .json ().get ("version" , {}).get ("number" , None )
563
+ if ds_body ["jsonData" ]["version" ] is None :
564
+ self .logger .error (self .logf .format ("Unable to detect version for {} of type {}. Manually set version in config with {}" .format (ds_body ["url" ], ds_body ["type" ], self .PSCONFIG_KEY_GF_DS_SETTINGS )))
565
+
532
566
def _gf_create_datasource (self , ds_url , ds_settings ):
533
567
'''
534
568
Creates a datasource using the Grafana API
535
569
'''
536
570
ds_body = ds_settings .copy ()
537
571
ds_body ["url" ] = ds_url
538
572
ds_body ["name" ] = self ._build_ds_name (ds_url )
573
+ self ._ds_version (ds_body )
539
574
r , msg = self ._gf_http ("/api/datasources" , "create_datasource" , method = "post" , data = ds_body )
540
575
if msg :
541
576
self .logger .error (self .logf .format ("Unable to create datasource for {}: {}" .format (ds_url , msg )))
@@ -562,6 +597,7 @@ def _gf_update_datasource(self, ds_url, ds_settings, ds_name):
562
597
ds_body ["name" ] = ds_name
563
598
ds_body ["uid" ] = self .grafana_datasource_by_name .get (ds_name , {}).get ("uid" , None )
564
599
ds_body ["type" ] = self .grafana_datasource_by_name .get (ds_name , {}).get ("type" , None )
600
+ self ._ds_version (ds_body )
565
601
r , msg = self ._gf_http (f'/api/datasources/uid/{ ds_body ["uid" ]} ' , "update_datasource" , method = "put" , data = ds_body )
566
602
if msg :
567
603
self .logger .error (self .logf .format ("Unable to update datasource for {}: {}" .format (ds_url , msg )))
0 commit comments