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

Provisioning: Fix unmarshaling nested jsonData values #20399

Merged
merged 1 commit into from
Nov 18, 2019

Conversation

aocenas
Copy link
Member

@aocenas aocenas commented Nov 14, 2019

Fixes: #11537
Problem was that yaml un-marshal returned nested maps as map[interface{}]interface{} which are then not marshal-able to json because of that interface{} key type. This adds explicit casting of the keys in the yaml value types to string which then makes the values marshal-able to JSON in DB.

func tranformInterface(i interface{}) interface{} {
// map or slice value instead of modifying them in place and also return value without interpolation but with converted
// type as a second value.
func transformInterface(i interface{}) (interface{}, interface{}) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning 2 values so that both raw value and value which was interpolated has the same types and there is no discrepancy. Also non string keys will be dropped (should not happen but yaml technically allows non string keys afaik) which would also create a difference in raw vs interpolated value.

@aocenas
Copy link
Member Author

aocenas commented Nov 14, 2019

@marefr Although the bug itself is old, I added this to current beta because with the derived fields feature in Loki we need a nested jsonData to make that provision-able.

@wardbekker
Copy link
Member

wardbekker commented Nov 18, 2019

With a docker image that contains this fix, I had issues with template variables in the url field of this YAML below. ${__value.raw} seems to get stripped and becomes http://localhost:16686/trace/

The docker image SHA used is: sha256:37e71eaf4076d43a30e44c3c7b9a048c7a8086675f1c2d5b53962a97613b38b3

apiVersion: 1
datasources:
- name: Loki
  type: loki
  access: proxy
  url: http://loki:3100
  basicAuth: false
  jsonData: 
    maxLines: 1000
    derivedFields: 
    - matcherRegex: ".*trace_id=(.*?)\\s.*"
      name: "TraceId"
      url: "http://localhost:16686/trace/${__value.raw}"    

@marefr
Copy link
Contributor

marefr commented Nov 18, 2019

@wardbekker solution is here: https://grafana.com/docs/administration/provisioning/#using-environment-variables

So you want to use $$ instead of $:

apiVersion: 1
datasources:
- name: Loki
  type: loki
  access: proxy
  url: http://loki:3100
  basicAuth: false
  jsonData: 
    maxLines: 1000
    derivedFields: 
    - matcherRegex: ".*trace_id=(.*?)\\s.*"
      name: "TraceId"
      url: "http://localhost:16686/trace/$${__value.raw}"    

Copy link
Contributor

@marefr marefr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great 👍 LGTM

@marefr marefr merged commit 3f144bd into master Nov 18, 2019
@marefr marefr deleted the aocenas/fix-provisioning-nested-jsondata branch November 18, 2019 10:06
marefr pushed a commit that referenced this pull request Nov 24, 2019
Problem was that yaml unmarshal returned nested maps as
 map[interface{}]interface{} which are then not marshal-able
to json because of that interface{} key type. This adds explicit
casting of the keys in the yaml value types to string which
then makes the values marshal-able to JSON in DB.

Fixes: #11537
(cherry picked from commit 3f144bd)
@marefr marefr mentioned this pull request Nov 24, 2019
2 tasks
marefr pushed a commit that referenced this pull request Nov 25, 2019
Problem was that yaml unmarshal returned nested maps as
 map[interface{}]interface{} which are then not marshal-able
to json because of that interface{} key type. This adds explicit
casting of the keys in the yaml value types to string which
then makes the values marshal-able to JSON in DB.

Fixes: #11537
(cherry picked from commit 3f144bd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

datasource provisioning not working if datasource has nested jsonData options
4 participants