Skip to content

Commit

Permalink
add missing schema for prometheus timeseries query
Browse files Browse the repository at this point in the history
Signed-off-by: Augustin Husson <[email protected]>
  • Loading branch information
Nexucis committed Jan 6, 2025
1 parent 9b2a33f commit ebbc8cd
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2025 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package migrate

#target: _

// NB we would need `if` to support short-circuit in order to avoid code duplication here.
// See https://github.com/cue-lang/cue/issues/2232
if (*#target.datasource.type | null) == "prometheus" && #target.expr != _|_ {
kind: "PrometheusTimeSeriesQuery"
spec: {
datasource: {
kind: "PrometheusDatasource"
name: #target.datasource.uid
}
query: #target.expr
#legendFormat: *#target.legendFormat | "__auto"
if #legendFormat != "__auto" {
seriesNameFormat: #legendFormat
}
if #target.interval != _|_ {
minStep: #target.interval
}
}
},

// The datasource.type may not be present while we are dealing with a prometheus query.
// In such case, rely on the "expr" field, whose presence likely indicates that this is a prometheus query.
// /!\ This is a best-effort conversion logic and may wrongly convert not-prometheus queries to PrometheusTimeSeriesQuery
if #target.expr != _|_ {
kind: "PrometheusTimeSeriesQuery"
spec: {
if #target.datasource != _|_ {
datasource: {
kind: "PrometheusDatasource"
name: #target.datasource.uid
}
}
query: #target.expr
#legendFormat: *#target.legendFormat | "__auto"
if #legendFormat != "__auto" {
seriesNameFormat: #legendFormat
}
if #target.interval != _|_ {
minStep: #target.interval
}
}
},
30 changes: 30 additions & 0 deletions Prometheus/schemas/prometheus-time-series-query/query.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2025 The Perses Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package model

import (
"strings"
prometheus "github.com/perses/perses/cue/schemas/datasources/prometheus:model"
)

kind: "PrometheusTimeSeriesQuery"
spec: close({
datasource?: {
kind: prometheus.kind
}
query: strings.MinRunes(1)
seriesNameFormat?: string
minStep?: =~"^(?:(\\d+)y)?(?:(\\d+)w)?(?:(\\d+)d)?(?:(\\d+)h)?(?:(\\d+)m)?(?:(\\d+)s)?(?:(\\d+)ms)?(?:\\$\\w+)?$"
resolution?: number
})

0 comments on commit ebbc8cd

Please sign in to comment.