Skip to content

Instantly share code, notes, and snippets.

@jjo
Last active November 23, 2023 18:38
Show Gist options
  • Save jjo/080ae9f49175279f52d744325b0eb482 to your computer and use it in GitHub Desktop.
Save jjo/080ae9f49175279f52d744325b0eb482 to your computer and use it in GitHub Desktop.
PromQL fun abusing `vector()` and `label_replace()` to create ~arbitrary timeseries, note these are used from grafana's /explore (for those ${__to:...} expressions)

Building a sinusoidal, back from now, using vector and label_replace.

round(
    8  # <- number of nodes
    *
    (1-sin(vector((time()-(${__to:date:seconds}))/(3600*10) * pi()))) / 2
)
* on() group_right() (
    label_replace(vector(0.0445000) , "spend", "cpu", "", "") * 8  # <- vCPU per node
    or
    label_replace(vector(0.0049225) , "spend", "mem", "", "") * 16 # <- GB per node
)
* on() group_left(unit) (
    label_replace(vector(1) , "unit", "usd_per_hour", "", "")
)

image

Building a sinusoidal, back from now, using vector and absent.

round(
    8  # <- number of nodes
    *
    (1-sin(vector((time()-(${__to:date:seconds}))/(3600*10) * pi()))) / 2
)
* on() group_right() (
    absent(_x_{spend="cpu"}) * 0.0445000 * 8  # <- vCPU per node
    or
    absent(_x_{spend="memory"}) * 0.0049225 * 16  # <- GB per node
)
* on() group_left(unit) (
    absent(_x_{unit="usd_per_hour"})
)

image

label_replace(
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-12*3600))), 1), 0) * (0.25)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-11*3600))), 1), 0) * (0.5)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-10*3600))), 1), 0) * (1.0)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-9*3600))), 1), 0) * (1.0)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-8*3600))), 1), 0) * (1.0)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-7*3600))), 1), 0) * (1.0)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-6*3600))), 1), 0) * (2.0)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-5*3600))), 1), 0) * (-1.0)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-4*3600))), 1), 0) * (-2.0)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-3*3600))), 1), 0) * (-1.0)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-2*3600))), 1), 0) * (-0.5)
+ clamp_min(clamp_max(vector((time()-(${__to:date:seconds}-1*3600))), 1), 0) * (-0.25)
, "spend", "compute", "", "") * 10

image

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