Dashboard Migrations: Add observability logging to dashboard conversion process (#109368)

This commit is contained in:
Ivan Ortega Alba
2025-08-11 13:27:54 +02:00
committed by GitHub
parent 6ca3d8a27a
commit f3182a3838
4 changed files with 88 additions and 1 deletions
+1 -1
View File
@@ -7,6 +7,7 @@ require (
github.com/grafana/grafana-app-sdk v0.40.3
github.com/grafana/grafana-plugin-sdk-go v0.278.0
github.com/grafana/grafana/pkg/apimachinery v0.0.0-20250514132646-acbc7b54ed9e
github.com/prometheus/client_golang v1.23.0
github.com/stretchr/testify v1.10.0
k8s.io/apimachinery v0.33.3
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff
@@ -80,7 +81,6 @@ require (
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.23.0 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.65.0 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
@@ -8,8 +8,11 @@ import (
dashv1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v1beta1"
dashv2alpha1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2alpha1"
dashv2beta1 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v2beta1"
"github.com/grafana/grafana/pkg/infra/log"
)
var logger = log.New("dashboard.conversion")
func RegisterConversions(s *runtime.Scheme) error {
// v0 conversions
if err := s.AddConversionFunc((*dashv0.Dashboard)(nil), (*dashv1.Dashboard)(nil), func(a, b interface{}, scope conversion.Scope) error {
@@ -1,6 +1,9 @@
package conversion
import (
"errors"
"fmt"
"k8s.io/apimachinery/pkg/conversion"
dashv0 "github.com/grafana/grafana/apps/dashboard/pkg/apis/dashboard/v0alpha1"
@@ -25,8 +28,45 @@ func Convert_V0_to_V1(in *dashv0.Dashboard, out *dashv1.Dashboard, scope convers
if err := migration.Migrate(out.Spec.Object, schemaversion.LATEST_VERSION); err != nil {
out.Status.Conversion.Failed = true
out.Status.Conversion.Error = err.Error()
// Classify error type for metrics
errorType := "conversion_error"
var migrationErr *schemaversion.MigrationError
var minVersionErr *schemaversion.MinimumVersionError
if errors.As(err, &migrationErr) {
errorType = "schema_version_migration_error"
} else if errors.As(err, &minVersionErr) {
errorType = "schema_minimum_version_error"
}
// Record failure metrics
migration.MDashboardConversionFailureTotal.WithLabelValues(
dashv0.APIVERSION,
dashv1.APIVERSION,
fmt.Sprintf("%v", in.Spec.Object["schemaVersion"]),
fmt.Sprintf("%d", schemaversion.LATEST_VERSION),
errorType,
).Inc()
logger.Error("Dashboard conversion failed",
"sourceVersionAPI", dashv0.APIVERSION,
"targetVersionAPI", dashv1.APIVERSION,
"dashboardUID", in.UID,
"sourceSchemaVersion", in.Spec.Object["schemaVersion"],
"targetSchemaVersion", schemaversion.LATEST_VERSION,
"errorType", errorType,
"error", err)
return nil
}
migration.MDashboardConversionSuccessTotal.WithLabelValues(
dashv0.APIVERSION,
dashv1.APIVERSION,
fmt.Sprintf("%v", in.Spec.Object["schemaVersion"]),
fmt.Sprintf("%d", schemaversion.LATEST_VERSION),
).Inc()
return nil
}
+44
View File
@@ -0,0 +1,44 @@
package migration
import (
"github.com/prometheus/client_golang/prometheus"
)
const (
metricsNamespace = "grafana"
metricsSubSystem = "dashboard_migration"
)
var (
// MDashboardConversionSuccessTotal is a metric counter for successful dashboard conversions
MDashboardConversionSuccessTotal *prometheus.CounterVec
// MDashboardConversionFailureTotal is a metric counter for failed dashboard conversions
MDashboardConversionFailureTotal *prometheus.CounterVec
)
func init() {
MDashboardConversionSuccessTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Subsystem: metricsSubSystem,
Name: "conversion_success_total",
Help: "Total number of successful dashboard conversions",
}, []string{"source_version_api", "target_version_api", "source_schema_version", "target_schema_version"})
MDashboardConversionFailureTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Subsystem: metricsSubSystem,
Name: "conversion_failure_total",
Help: "Total number of failed dashboard conversions",
}, []string{"source_version_api", "target_version_api", "source_schema_version", "target_schema_version", "error_type"})
}
// RegisterMetrics registers all migration metrics with the provided Prometheus registerer
func RegisterMetrics(reg prometheus.Registerer) {
if reg != nil {
reg.MustRegister(
MDashboardConversionSuccessTotal,
MDashboardConversionFailureTotal,
)
}
}