WriteTargetConfig.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { css } from '@emotion/css';
  2. import React, { useEffect, useState } from 'react';
  3. import { UnpackNestedValue } from 'react-hook-form';
  4. import { connect, ConnectedProps } from 'react-redux';
  5. import { AppEvents, DataSourceInstanceSettings, GrafanaTheme2 } from '@grafana/data';
  6. import { DataSourcePicker, getDataSourceSrv } from '@grafana/runtime';
  7. import { Button, Field, Form, Icon, Input, useStyles2 } from '@grafana/ui';
  8. import appEvents from 'app/core/app_events';
  9. import Page from 'app/core/components/Page/Page';
  10. import { getNavModel } from 'app/core/selectors/navModel';
  11. import { EnterpriseStoreState, PrometheusWriteTarget } from '../types';
  12. import { getPrometheusWriteTarget, savePrometheusWriteTarget } from './state/actions';
  13. import { getRecordedQueryWriter } from './state/selectors';
  14. export type Props = ConnectedProps<typeof connector>;
  15. function mapStateToProps(state: EnterpriseStoreState) {
  16. return {
  17. navModel: getNavModel(state.navIndex, 'recordedQueries'),
  18. prometheusWriteTarget: getRecordedQueryWriter(state.recordedQueries),
  19. };
  20. }
  21. const mapDispatchToProps = {
  22. getPrometheusWriteTarget,
  23. };
  24. const connector = connect(mapStateToProps, mapDispatchToProps);
  25. export const WriteTargetConfigUnconnected = ({ navModel, prometheusWriteTarget, getPrometheusWriteTarget }: Props) => {
  26. const [selectedDataSource, setSelectedDataSource] = useState<DataSourceInstanceSettings | undefined>();
  27. const [writePath, setWritePath] = useState<string | undefined>(prometheusWriteTarget?.remote_write_path);
  28. const styles = useStyles2(getStyles);
  29. useEffect(getPrometheusWriteTarget, [getPrometheusWriteTarget]);
  30. useEffect(() => {
  31. const getWriteTarget = async () => {
  32. if (prometheusWriteTarget) {
  33. const selectedDataSource = await getDataSourceSrv().getInstanceSettings(prometheusWriteTarget.data_source_uid);
  34. setSelectedDataSource(selectedDataSource);
  35. }
  36. };
  37. getWriteTarget();
  38. }, [prometheusWriteTarget]);
  39. const onSubmit = (data: UnpackNestedValue<PrometheusWriteTarget>) => {
  40. updateWriteTarget(selectedDataSource?.uid!, data.remote_write_path);
  41. };
  42. const label = (
  43. <span>
  44. {'Select the data source where metrics will be written'}
  45. <a
  46. href="https://grafana.com/docs/grafana/latest/enterprise/recorded-queries/#remote-write-target"
  47. className={styles.docsLink}
  48. target="_blank"
  49. rel="noreferrer"
  50. >
  51. <Icon name="info-circle" />
  52. </a>
  53. </span>
  54. );
  55. return (
  56. <Page navModel={navModel}>
  57. <Page.Contents>
  58. <Form key={prometheusWriteTarget?.data_source_uid} defaultValues={prometheusWriteTarget} onSubmit={onSubmit}>
  59. {({ register }) => (
  60. <>
  61. <Field label={label}>
  62. <DataSourcePicker
  63. onChange={setSelectedDataSource}
  64. current={selectedDataSource?.name}
  65. placeholder={'Select a data source'}
  66. type={'prometheus'}
  67. noDefault={true}
  68. alerting={true}
  69. aria-label="Write target config select data source"
  70. />
  71. </Field>
  72. <Field label="Remote write path">
  73. <Input
  74. {...register('remote_write_path')}
  75. onChange={(e) => setWritePath(e.currentTarget.value)}
  76. placeholder="/api/v1/write"
  77. aria-label="Write target config remote write path"
  78. />
  79. </Field>
  80. <Button
  81. disabled={Boolean(!writePath || !selectedDataSource)}
  82. type="submit"
  83. aria-label="Write target config save"
  84. >
  85. Save
  86. </Button>
  87. </>
  88. )}
  89. </Form>
  90. </Page.Contents>
  91. </Page>
  92. );
  93. };
  94. function updateWriteTarget(data_source_uid: string, remote_write_path?: string) {
  95. savePrometheusWriteTarget({
  96. data_source_uid,
  97. remote_write_path,
  98. } as PrometheusWriteTarget)
  99. .then(() => {
  100. appEvents.emit(AppEvents.alertSuccess, [`Prometheus remote write target created`]);
  101. })
  102. .catch((error) => {
  103. appEvents.emit(AppEvents.alertError, [error.data.message]);
  104. });
  105. }
  106. const getStyles = (theme: GrafanaTheme2) => {
  107. return {
  108. docsLink: css`
  109. margin-left: ${theme.spacing(1)};
  110. `,
  111. };
  112. };
  113. export const WriteTargetConfig = connector(WriteTargetConfigUnconnected);