yaml

package
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 9, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DetectKind

func DetectKind(data []byte) (string, error)

DetectKind extracts the "kind" field from raw YAML/JSON bytes. When the document has no explicit "kind" (e.g., a check rule exported via `check-rules get -o yaml`), the kind is inferred from the document structure: the "expression" field is required for check rules and absent in all other asset types. An empty kind is returned when the input is valid YAML but has no recognizable kind. An error is returned when the input cannot be parsed as YAML.

Example
package main

import (
	"fmt"
	"log"

	dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
)

func main() {
	kind, err := dash0yaml.DetectKind([]byte("kind: PrometheusRule\nspec: {}"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(kind)
}
Output:
PrometheusRule
Example (InferredCheckRule)
package main

import (
	"fmt"
	"log"

	dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
)

func main() {
	// Documents without an explicit "kind" but with "name" and "expression"
	// are inferred as check rules.
	kind, err := dash0yaml.DetectKind([]byte("name: HighErrors\nexpression: up == 0"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(kind)
}
Output:
CheckRule

func MarshalPrometheusRule

func MarshalPrometheusRule(rule *dash0.PrometheusAlertRule) ([]byte, error)

MarshalPrometheusRule converts a PrometheusAlertRule (Dash0 API format) back to a Prometheus rule YAML document.

Example
package main

import (
	"fmt"
	"log"

	dash0 "github.com/dash0hq/dash0-api-client-go"
	dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
)

func main() {
	forDur := dash0.Duration("5m")
	summary := "High error rate detected"
	rule := &dash0.PrometheusAlertRule{
		Name:       "my-group - HighErrors",
		Expression: "sum(rate(errors[5m])) > 0.1",
		For:        &forDur,
		Annotations: &dash0.PrometheusAlertRule_Annotations{
			Summary: &summary,
		},
	}

	// Marshal to YAML and unmarshal back to verify the round-trip.
	data, err := dash0yaml.MarshalPrometheusRule(rule)
	if err != nil {
		log.Fatal(err)
	}
	roundTripped, err := dash0yaml.UnmarshalPrometheusRule(data)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(roundTripped.Name)
	fmt.Println(roundTripped.Expression)
	fmt.Println(*roundTripped.For)
	fmt.Println(*roundTripped.Annotations.Summary)
}
Output:
my-group - HighErrors
sum(rate(errors[5m])) > 0.1
5m
High error rate detected

func ParseAsDashboard

func ParseAsDashboard(data []byte) (*dash0.DashboardDefinition, error)

ParseAsDashboard detects whether data is a Dashboard or PersesDashboard CRD, unmarshals it, and returns a normalized DashboardDefinition ready for the API. PersesDashboard CRDs are converted via dash0.ConvertPersesDashboardToDashboard.

Example
package main

import (
	"fmt"
	"log"

	dash0 "github.com/dash0hq/dash0-api-client-go"
	dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
)

func main() {
	data := []byte(`kind: Dashboard
metadata:
  name: My Dashboard
  dash0Extensions:
    id: dash-123
    dataset: production
spec:
  display:
    name: My Dashboard
`)
	dashboard, err := dash0yaml.ParseAsDashboard(data)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(dashboard.Metadata.Name)
	fmt.Println(dash0.GetDashboardID(dashboard))
	fmt.Println(string(*dashboard.Metadata.Dash0Extensions.Dataset))
}
Output:
My Dashboard
dash-123
production
Example (PersesDashboard)
package main

import (
	"fmt"
	"log"

	dash0 "github.com/dash0hq/dash0-api-client-go"
	dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
)

func main() {
	// PersesDashboard CRDs are automatically detected and converted to
	// the Dash0 DashboardDefinition format.
	// The dash0.com/id and dash0.com/dataset labels are extracted into
	// dash0Extensions.
	data := []byte(`apiVersion: perses.dev/v1alpha1
kind: PersesDashboard
metadata:
  name: my-perses-dashboard
  labels:
    dash0.com/id: perses-123
    dash0.com/dataset: production
  annotations:
    dash0.com/folder-path: /team/sre
spec:
  display:
    name: SRE Overview
  panels: {}
`)
	dashboard, err := dash0yaml.ParseAsDashboard(data)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(dashboard.Metadata.Name)
	fmt.Println(dash0.GetDashboardID(dashboard))
	fmt.Println(string(*dashboard.Metadata.Dash0Extensions.Dataset))
	fmt.Println(*dashboard.Metadata.Annotations.Dash0ComfolderPath)
}
Output:
SRE Overview
perses-123
production
/team/sre

func ParseAsPrometheusAlertRules

func ParseAsPrometheusAlertRules(data []byte) ([]*dash0.PrometheusAlertRule, error)

ParseAsPrometheusAlertRules detects whether data is a CheckRule or PrometheusRule CRD, unmarshals it, and returns one or more normalized check rules ready for the API. A plain CheckRule returns a slice of length 1. A PrometheusRule CRD returns one entry per alerting rule (recording rules are skipped).

Example
package main

import (
	"fmt"
	"log"

	dash0 "github.com/dash0hq/dash0-api-client-go"
	dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
)

func main() {
	// The dash0.com/id and dash0.com/dataset labels from CRD metadata are
	// propagated to every returned rule.
	data := []byte(`apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: my-rules
  labels:
    dash0.com/id: rule-42
    dash0.com/dataset: production
spec:
  groups:
    - name: availability
      rules:
        - alert: HighErrorRate
          expr: "sum(rate(errors[5m])) > 0.1"
          for: 5m
        - alert: ServiceDown
          expr: up == 0
`)
	rules, err := dash0yaml.ParseAsPrometheusAlertRules(data)
	if err != nil {
		log.Fatal(err)
	}
	for _, r := range rules {
		fmt.Printf("%s (id=%s, dataset=%s)\n", r.Name, dash0.StringValue(r.Id), dash0.StringValue(r.Dataset))
	}
}
Output:
HighErrorRate (id=rule-42, dataset=production)
ServiceDown (id=rule-42, dataset=production)
Example (NativeCheckRule)
package main

import (
	"fmt"
	"log"

	dash0 "github.com/dash0hq/dash0-api-client-go"
	dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
)

func main() {
	// A native check rule (no "kind" field, detected by the presence of
	// "name" and "expression") returns a single-element slice.
	// The "dataset" field is preserved from the input.
	data := []byte(`
name: HighErrorRate
expression: "sum(rate(errors[5m])) > 0.1"
dataset: production
`)
	rules, err := dash0yaml.ParseAsPrometheusAlertRules(data)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(len(rules))
	fmt.Println(rules[0].Name)
	fmt.Println(rules[0].Expression)
	fmt.Println(dash0.StringValue(rules[0].Dataset))
}
Output:
1
HighErrorRate
sum(rate(errors[5m])) > 0.1
production

func UnmarshalPrometheusRule

func UnmarshalPrometheusRule(data []byte) (*dash0.PrometheusAlertRule, error)

UnmarshalPrometheusRule converts a Prometheus rule YAML document to a PrometheusAlertRule (Dash0 API format). The YAML must contain exactly one group with one rule. The check rule name is composed as "groupName - alertName".

Example
package main

import (
	"fmt"
	"log"

	dash0 "github.com/dash0hq/dash0-api-client-go"
	dash0yaml "github.com/dash0hq/dash0-api-client-go/yaml"
)

func main() {
	// The dash0.com/id and dash0.com/dataset labels from CRD metadata are
	// extracted and set on the returned PrometheusAlertRule.
	data := []byte(`apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  labels:
    dash0.com/id: rule-42
    dash0.com/dataset: production
spec:
  groups:
    - name: my-group
      interval: 1m
      rules:
        - alert: HighErrors
          expr: "sum(rate(errors[5m])) > 0.1"
          for: 5m
          annotations:
            summary: High error rate detected
`)
	rule, err := dash0yaml.UnmarshalPrometheusRule(data)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(rule.Name)
	fmt.Println(rule.Expression)
	fmt.Println(dash0.StringValue(rule.Id))
	fmt.Println(dash0.StringValue(rule.Dataset))
	fmt.Println(*rule.For)
	fmt.Println(*rule.Interval)
	fmt.Println(*rule.Annotations.Summary)
}
Output:
my-group - HighErrors
sum(rate(errors[5m])) > 0.1
rule-42
production
5m
1m
High error rate detected

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL