Files
grafana/docs/sources/developers/plugins/add-support-for-explore-queries.md
Joseph Perez 49a18bc8e2 Plugins doc review chunk 2 (#67691)
* Doc style edit for 7 topics

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Proofread topics

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Prettier

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Doc fix

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Update docs/sources/developers/plugins/add-query-editor-help.md

Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>

* Doc fixes

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Changes from doc review

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* Incorporate review feedback

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* More fixes

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

* More doc fixes

Signed-off-by: Joe Perez <joseph.perez@grafana.com>

---------

Signed-off-by: Joe Perez <joseph.perez@grafana.com>
Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
2023-05-12 13:33:49 -05:00

2.4 KiB

title
Add features to Explore queries

Add features for Explore queries

[Explore]({{< relref "../../explore/" >}}) allows users can make ad-hoc queries without the use of a dashboard. This is useful when they want to troubleshoot or learn more about the data.

Your data source supports Explore by default and uses the existing query editor for the data source. This guide explains how to extend functionality for Explore queries in a data source plugin.

Add an Explore-specific query editor

To extend Explore functionality for your data source, define an Explore-specific query editor.

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

    import React from 'react';
    
    import { QueryEditorProps } from '@grafana/data';
    import { QueryField } from '@grafana/ui';
    import { DataSource } from './DataSource';
    import { MyQuery, MyDataSourceOptions } from './types';
    
    type Props = QueryEditorProps<DataSource, MyQuery, MyDataSourceOptions>;
    
    export default (props: Props) => {
      return <h2>My Explore-specific query editor</h2>;
    };
    
  2. Modify your base query editor in QueryEditor.tsx to render the Explore-specific query editor. For example:

    // [...]
    import { CoreApp } from '@grafana/data';
    import ExploreQueryEditor from './ExploreQueryEditor';
    
    type Props = QueryEditorProps<DataSource, MyQuery, MyDataSourceOptions>;
    
    export default (props: Props) => {
      const { app } = props;
    
      switch (app) {
        case CoreApp.Explore:
          return <ExploreQueryEditor {...props} />;
        default:
          return <div>My base query editor</div>;
      }
    };
    

Select a preferred visualization type

By default, Explore should select an appropriate and useful visualization for your data. It can figure out whether the returned data is time series data or logs or something else, and creates the right type of visualization.

However, if you want a custom visualization, you can add a hint to your returned data frame by setting the meta' attribute to preferredVisualisationType`.

Construct a data frame with specific metadata like this:

const firstResult = new MutableDataFrame({
    fields: [...],
    meta: {
        preferredVisualisationType: 'logs',
    },
});

For possible options, refer to PreferredVisualisationType.