Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the use of Templating #110

Open
erkolson opened this issue Feb 16, 2018 · 8 comments · Fixed by #390
Open

Document the use of Templating #110

erkolson opened this issue Feb 16, 2018 · 8 comments · Fixed by #390

Comments

@erkolson
Copy link

erkolson commented Feb 16, 2018

What you expected to happen?

I attempted to use add templating to a dashboard, it is not documented how to use it so I provided these parameters:
https://github.com/aknuds1/grafanalib/blob/master/grafanalib/core.py#L431-L439

        :param default: the default value for the variable
        :param dataSource: where to fetch the values for the variable from
        :param label: the variable's human label
        :param name: the variable's name
        :param query: the query users to fetch the valid values of the variable
        :param allValue: specify a custom all value with regex,
            globs or lucene syntax.
        :param includeAll: Add a special All option whose value includes
            all options.

What happened?

Js error in grafana when trying to import the resulting json dashboard. After some effort I determined that the js error is caued by the absence of some of these values for each template:
https://github.com/aknuds1/grafanalib/blob/master/grafanalib/core.py#L456-L470

    def to_json_data(self):
        return {**_as_dict(self), **{
            'current': {
                'text': self.default,
                'value': self.default,
                'tags': [],
            },
            'hide': 0,
            'multi': False,
            'options': [],
            'refresh': 1,
            'regex': '',
            'sort': 1,
            'tagValuesQuery': None,
            'tagsQuery': None,
            'type': 'query',
        }}

I'm not very familiar with python but that bit of code looks like it should be used as defaults? In any case, when I include those keys/values for each templated variable in my dashboard.py, the resulting json generated does work!

How to reproduce it?

Create any dashboard that uses the templating, i.e.:

  templating=Templating(list=[
    { # namespace
      "default": "",
      "datasource": "prometheus",
      "label": "Namespace",
      "name": "namespace",
      "query": "label_values(kube_pod_info, namespace)",
      "allValue": ".+",
      "includeAll": True,
    },
  ]),

Is this a bug with how templating gets rendered? If not, can you add the proper incantation to use templating in the example dashboard.py?

@erkolson erkolson changed the title Document the use ot Templating Document the use of Templating Feb 16, 2018
@jml
Copy link
Contributor

jml commented Feb 20, 2018

Thanks! This looks like a bug. I hope to try to fix it soon, but am stretched pretty thin.

@taion809
Copy link

taion809 commented Apr 3, 2018

@jml do you have any thoughts as to where this bug might be? I looked through the code a bit and it's not really clear why this would work as expected.

also as a workaround you can explicitly pass the template object.

dashboard = G.Dashboard(
    title="Summary",
    templating=G.Templating(list=[
        G.Template(
            default="east-platform",
            dataSource="prometheus",
            name="data_source",
            label="Data source",
            query="prometheus"
        )
    ])
)

@jml
Copy link
Contributor

jml commented Apr 17, 2018

I think it's just missing values in one of the objects. Isolating it is made more complicated by the fact that the bug is only revealed when the generated JSON is loaded by Grafana itself.

I won't have time to get into this for a little while.

@matthewmrichter
Copy link
Collaborator

Bump - may take a look at fixing this myself.

@matthewmrichter
Copy link
Collaborator

On the Grafana side, the defaults appear to be here: https://github.com/grafana/grafana/blob/85dad73e9de458ed8610ce7acdb326ff72808970/public/app/features/templating/query_variable.ts#L47-L68

defaults: QueryVariableModel = {
    type: 'query',
    name: '',
    label: null,
    hide: VariableHide.dontHide,
    skipUrlSync: false,
    datasource: null,
    query: '',
    regex: '',
    sort: VariableSort.disabled,
    refresh: VariableRefresh.never,
    multi: false,
    includeAll: false,
    allValue: null,
    options: [],
    current: {} as VariableOption,
    tags: [],
    useTags: false,
    tagsQuery: '',
    tagValuesQuery: '',
    definition: '',
  };

@chrisfleming
Copy link
Contributor

I'm still checking this out - but I think I've found the problem, in my experimentation, it looks like 'selected' has to be set to either True or False to get things working chrisfleming@0589cb5

I've a few more checks but should have a pull request next week.

@JamesGibo
Copy link
Collaborator

Thanks Chis, look forward to the PR.

@JamesGibo
Copy link
Collaborator

Could templating still be improved with some better docs? Or do we think this issue can be closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants