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

feat(util): dedupe query targets into shared queries #205

Merged
merged 1 commit into from
May 31, 2024

Conversation

Duologic
Copy link
Member

@Duologic Duologic commented May 25, 2024

This PR adds a util function to dedupe query targets into 'shared queries', this could boost performance of dashboards that run the same query multiple times in different panels.

Rudimentary example:

local g = import 'github.com/grafana/grafonnet/gen/grafonnet-latest/main.libsonnet';

local sharedQuery1 =
  g.query.prometheus.new(
    'mimir',
    'query1',
  );

local sharedQuery2 =
  g.query.prometheus.new(
    'mimir',
    'query2',
  );

local panels =
  [
    g.panel.timeSeries.new('Panel 1')
    + g.panel.timeSeries.queryOptions.withTargets([
      sharedQuery1,
    ]),

    g.panel.timeSeries.new('Panel 2')
    + g.panel.timeSeries.queryOptions.withTargets([
      sharedQuery2,
      sharedQuery1, # This will get replaced.
    ]),
    g.panel.timeSeries.new('Panel 3')
    + g.panel.timeSeries.queryOptions.withTargets([
      sharedQuery2, # This will get replaced.
    ]),
  ];

g.dashboard.new('Dashboard')
+ g.dashboard.withPanels(
  std.foldl(
    function(panels, f) f(panels),
    // Apply these functions to the panels
    [
      g.util.panel.setPanelIDs,
      g.util.panel.setRefIDsOnPanels,
      g.util.panel.dedupeQueryTargets,  // Requires `refId` on targets and `id` on panels
    ],
    panels
  ),
  setPanelIDs=false,  // IDs set on inner function
)

@Duologic Duologic requested a review from a team as a code owner May 25, 2024 19:26
@Duologic Duologic changed the title feat(util): dedupe query targets in to shared queries feat(util): dedupe query targets into shared queries May 26, 2024
d.arg('panels', d.T.array),
]
),
setRefIDsOnPanels(panels):
Copy link
Member

Choose a reason for hiding this comment

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

Should we keep the index, having multiple refID A could get confusing if we're referencing between panels

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if I understand what you mean.

refID is unique per panel, so a reference to a target is a combination of the panel ID and the refID of the target

@Duologic Duologic merged commit 7d72871 into main May 31, 2024
2 checks passed
@Duologic Duologic deleted the duologic/shared_queries branch May 31, 2024 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants