Files
grafana/docs/sources/developers/plugins/add-support-for-explore-queries.md
T
Torkel Ödegaard 8011ff4f79 Docs: Backport 32916 to v7.5x (#33008)
* fixing things

* Update add-authentication-for-data-source-plugins.md

* Docs: Sync release branch with latest docs (#32986)

* Docs: Sync release branch with latest docs

* Fixed what branch to update

* Elasticsearch: Force re-rendering of each editor row type change (#32993) (#32996)

(cherry picked from commit 136460d369)

Co-authored-by: Giordano Ricci <me@giordanoricci.com>

* ReleaseNotes: Updated changelog and release notes for 7.5.4 (#32973) (#32998)

* ReleaseNotes: Updated changelog and release notes for 7.5.4 (#32973)

* ReleaseNotes: Updated changelog and release notes for 7.5.4

* Update index link

* Fix boldness

* Update CHANGELOG.md

Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com>

* Update CHANGELOG.md

Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com>

* Update codespell words

* Update docs/sources/release-notes/release-notes-7-5-4.md

* Update CHANGELOG.md

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>

* Update docs/sources/release-notes/release-notes-7-5-4.md

* Change bold markers

* Fix escaping

* Update drone yaml

Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com>
Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
(cherry picked from commit a516ff00db)

* Add old release notes

Co-authored-by: Grot (@grafanabot) <43478413+grafanabot@users.noreply.github.com>

* fixing things

* Merge branch 'v7.5.x' of https://github.com/grafana/grafana into backport-32916-to-v7.5.x

* Update add-authentication-for-data-source-plugins.md

* Update license-restrictions.md

Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com>
Co-authored-by: Grot (@grafanabot) <43478413+grafanabot@users.noreply.github.com>
Co-authored-by: Giordano Ricci <me@giordanoricci.com>
Co-authored-by: Andrej Ocenas <mr.ocenas@gmail.com>
2021-04-14 15:40:43 -07:00

3.7 KiB

+++ title = "Add support for Explore queries" +++

Add support for Explore queries

This guide explains how to improve support for [Explore]({{< relref "../../explore/_index.md" >}}) to an existing data source plugin.

This guide assumes that you're already familiar with how to [Build a data source plugin]({{< relref "/tutorials/build-a-data-source-plugin.md" >}}).

With Explore, users can make ad-hoc queries without the use of a dashboard. This is useful when users want to troubleshoot or to learn more about the data.

Your data source already supports Explore by default, and will use the existing query editor for the data source. If you want to offer extended Explore functionality for your data source however, you can define a Explore-specific query editor.

Add a query editor for Explore

The query editor for Explore is similar to the query editor for the data source itself. In fact, you'll probably reuse the same components for both query editors.

  1. Create a file ExploreQueryEditor.tsx in the src directory of your plugin, with the following content:

    import React from 'react';
    
    import { ExploreQueryFieldProps } from '@grafana/data';
    import { QueryField } from '@grafana/ui';
    import { DataSource } from './DataSource';
    import { MyQuery, MyDataSourceOptions } from './types';
    
    export type Props = ExploreQueryFieldProps<DataSource, MyQuery, MyDataSourceOptions>;
    
    export default (props: Props) => {
      return (
        <h2>My query editor</h2>
      );
    };
    
  2. Configure the plugin to use the ExploreQueryEditor.

    import ExploreQueryEditor from './ExploreQueryEditor';
    
    export const plugin = new DataSourcePlugin<DataSource, MyQuery, MyDataSourceOptions>(DataSource)
      .setConfigEditor(ConfigEditor)
      .setQueryEditor(QueryEditor)
      .setExploreQueryField(ExploreQueryEditor);
    
  3. Add a [QueryField]({{< relref "../../packages_api/ui/queryfield.md" >}}) to ExploreQueryEditor.

    import { QueryField } from '@grafana/ui';
    
    export default (props: Props) => {
      const { query } = props;
    
      const onQueryChange = (value: string, override?: boolean) => {
        const { query, onChange, onRunQuery } = props;
    
        if (onChange) {
          // Update the query whenever the query field changes.
          onChange({ ...query, queryText: value });
    
          // Run the query on Enter.
          if (override && onRunQuery) {
            onRunQuery();
          }
        }
      };
    
      return (
        <QueryField
          portalOrigin="mock-origin"
          onChange={onQueryChange}
          onRunQuery={props.onRunQuery}
          onBlur={props.onBlur}
          query={query.queryText || ''}
          placeholder="Enter a query"
        />
      );
    };
    

Support multiple Explore modes

Explore lets you query any data source, regardless of whether it returns metrics or logs. You can change which type of query you want to make, by setting the Explore mode.

The query modes that the plugin supports are defined in the [plugin.json]({{< relref "metadata.md" >}}) file.

The query mode is available on the props object for both the query editor and the start page. For example, here's how you can change the query editor based on the currently selected mode:

export default (props: Props) => {
  const { query, exploreMode } = props;

  switch (exploreMode) {
    case ExploreMode.Metrics:
      return <MetricsQueryField query={query} />;
    case ExploreMode.Logs:
      return <LogsQueryField query={query} />;
    default:
      return <p>Unsupported mode</p>;
  }
}

For possible options, refer to PreferredVisualisationType.