2020-05-05 18:02:55 +02:00
import {
DataSourcePluginOptionsEditorProps ,
2021-01-15 16:20:20 +01:00
onUpdateDatasourceJsonDataOptionChecked ,
SelectableValue ,
updateDatasourcePluginJsonDataOption ,
2020-05-05 18:02:55 +02:00
} from '@grafana/data' ;
2021-01-15 16:20:20 +01:00
import { EventsWithValidation , InlineFormLabel , LegacyForms , regexValidation } from '@grafana/ui' ;
import React , { SyntheticEvent } from 'react' ;
2019-11-08 10:02:51 +01:00
import { PromOptions } from '../types' ;
2021-01-15 16:20:20 +01:00
import { ExemplarsSettings } from './ExemplarsSettings' ;
const { Select , Input , FormField , Switch } = LegacyForms ;
2019-11-08 10:02:51 +01:00
2019-11-19 13:59:39 +00:00
const httpOptions = [
{ value : 'GET' , label : 'GET' } ,
{ value : 'POST' , label : 'POST' } ,
] ;
2019-11-08 10:02:51 +01:00
2020-05-05 18:02:55 +02:00
type Props = Pick < DataSourcePluginOptionsEditorProps < PromOptions > , 'options' | 'onOptionsChange' > ;
2019-11-08 10:02:51 +01:00
export const PromSettings = ( props : Props ) = > {
2020-05-05 18:02:55 +02:00
const { options , onOptionsChange } = props ;
2019-11-08 10:02:51 +01:00
return (
< >
< div className = "gf-form-group" >
< div className = "gf-form-inline" >
< div className = "gf-form" >
< FormField
label = "Scrape interval"
labelWidth = { 13 }
inputEl = {
< Input
className = "width-6"
2020-05-05 18:02:55 +02:00
value = { options . jsonData . timeInterval }
2019-11-08 10:02:51 +01:00
spellCheck = { false }
2020-04-27 18:29:41 +02:00
placeholder = "15s"
2020-05-05 18:02:55 +02:00
onChange = { onChangeHandler ( 'timeInterval' , options , onOptionsChange ) }
2019-12-12 13:26:12 +01:00
validationEvents = { promSettingsValidationEvents }
2019-11-08 10:02:51 +01:00
/ >
}
2019-12-27 09:49:42 +01:00
tooltip = "Set this to the typical scrape and evaluation interval configured in Prometheus. Defaults to 15s."
2019-11-08 10:02:51 +01:00
/ >
< / div >
< / div >
< div className = "gf-form-inline" >
< div className = "gf-form" >
< FormField
label = "Query timeout"
labelWidth = { 13 }
inputEl = {
< Input
className = "width-6"
2020-05-05 18:02:55 +02:00
value = { options . jsonData . queryTimeout }
onChange = { onChangeHandler ( 'queryTimeout' , options , onOptionsChange ) }
2019-11-08 10:02:51 +01:00
spellCheck = { false }
placeholder = "60s"
2019-12-12 13:26:12 +01:00
validationEvents = { promSettingsValidationEvents }
2019-11-08 10:02:51 +01:00
/ >
}
tooltip = "Set the Prometheus query timeout."
/ >
< / div >
< / div >
< div className = "gf-form" >
2020-04-21 11:42:21 +02:00
< InlineFormLabel
2019-11-08 10:02:51 +01:00
width = { 13 }
tooltip = "Specify the HTTP Method to query Prometheus. (POST is only available in Prometheus >= v2.1.0)"
>
HTTP Method
2020-04-21 11:42:21 +02:00
< / InlineFormLabel >
2019-11-08 10:02:51 +01:00
< Select
options = { httpOptions }
2020-05-05 18:02:55 +02:00
value = { httpOptions . find ( o = > o . value === options . jsonData . httpMethod ) }
onChange = { onChangeHandler ( 'httpMethod' , options , onOptionsChange ) }
2019-11-08 10:02:51 +01:00
width = { 7 }
/ >
< / div >
< / div >
< h3 className = "page-heading" > Misc < / h3 >
< div className = "gf-form-group" >
2020-05-05 18:02:55 +02:00
< div className = "gf-form" >
< Switch
2020-07-06 21:16:27 +02:00
checked = { options . jsonData . disableMetricsLookup ? ? false }
2020-05-05 18:02:55 +02:00
label = "Disable metrics lookup"
labelClass = "width-14"
onChange = { onUpdateDatasourceJsonDataOptionChecked ( props , 'disableMetricsLookup' ) }
tooltip = "Checking this option will disable the metrics chooser and metric/label support in the query field's autocomplete. This helps if you have performance issues with bigger Prometheus instances."
/ >
< / div >
2019-11-08 10:02:51 +01:00
< div className = "gf-form-inline" >
< div className = "gf-form max-width-30" >
< FormField
label = "Custom query parameters"
labelWidth = { 14 }
2020-11-26 11:56:14 +02:00
tooltip = "Add Custom parameters to all Prometheus or Thanos queries."
2019-11-08 10:02:51 +01:00
inputEl = {
< Input
className = "width-25"
2020-05-05 18:02:55 +02:00
value = { options . jsonData . customQueryParameters }
onChange = { onChangeHandler ( 'customQueryParameters' , options , onOptionsChange ) }
2019-11-08 10:02:51 +01:00
spellCheck = { false }
placeholder = "Example: max_source_resolution=5m&timeout=10"
/ >
}
/ >
< / div >
< / div >
< / div >
2021-01-15 16:20:20 +01:00
< ExemplarsSettings
options = { options . jsonData . exemplarTraceIdDestinations }
onChange = { exemplarOptions = >
updateDatasourcePluginJsonDataOption (
{ onOptionsChange , options } ,
'exemplarTraceIdDestinations' ,
exemplarOptions
)
}
/ >
2019-11-08 10:02:51 +01:00
< / >
) ;
} ;
2019-12-12 13:26:12 +01:00
export const promSettingsValidationEvents = {
[ EventsWithValidation . onBlur ] : [
regexValidation (
/^$|^\d+(ms|[Mwdhmsy])$/ ,
'Value is not valid, you can use number with time unit specifier: y, M, w, d, h, m, s'
) ,
] ,
} ;
2019-12-12 11:52:03 +01:00
export const getValueFromEventItem = ( eventItem : SyntheticEvent < HTMLInputElement > | SelectableValue < string > ) = > {
if ( ! eventItem ) {
return '' ;
}
if ( eventItem . hasOwnProperty ( 'currentTarget' ) ) {
return eventItem . currentTarget . value ;
}
return ( eventItem as SelectableValue < string > ) . value ;
} ;
2020-05-05 18:02:55 +02:00
const onChangeHandler = (
key : keyof PromOptions ,
options : Props [ 'options' ] ,
onOptionsChange : Props [ 'onOptionsChange' ]
) = > ( eventItem : SyntheticEvent < HTMLInputElement > | SelectableValue < string > ) = > {
onOptionsChange ( {
. . . options ,
2019-11-08 10:02:51 +01:00
jsonData : {
2020-05-05 18:02:55 +02:00
. . . options . jsonData ,
2019-12-12 11:52:03 +01:00
[ key ] : getValueFromEventItem ( eventItem ) ,
2019-11-08 10:02:51 +01:00
} ,
} ) ;
} ;