Skip to content

Commit

Permalink
Merge pull request #17 from K-Phoen/converter-stackdriver
Browse files Browse the repository at this point in the history
Converter stackdriver
  • Loading branch information
K-Phoen authored May 26, 2020
2 parents 79cb84d + 0471686 commit c349b3c
Show file tree
Hide file tree
Showing 21 changed files with 885 additions and 44 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# DARK

[![codecov](https://codecov.io/gh/K-Phoen/dark/branch/master/graph/badge.svg)](https://codecov.io/gh/K-Phoen/dark)

**D**ashboards **A**s **R**esources in **K**ubernetes.

DARK provides a way to define and deploy Grafana dashboards via Kubernetes, next to the services they monitor.
Expand All @@ -16,7 +18,7 @@ with your services, then this project is meant for you.

## Setup

**Note:** review these manifests to ensure that they fit your cluster's configuration.
**Note:** review these manifests to ensure that they fit your cluster's configuration.

Setup the [CRD](https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/):

Expand Down Expand Up @@ -57,6 +59,13 @@ spec:
tags: [generated, yaml]
auto_refresh: 10s

variables:
- interval:
name: interval
label: interval
default: 1m
values: [30s, 1m, 5m, 10m, 30m, 1h, 6h, 12h]

rows:
- name: Prometheus
panels:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/K-Phoen/dark
go 1.13

require (
github.com/K-Phoen/grabana v0.10.0
github.com/K-Phoen/grabana v0.11.1
github.com/caarlos0/env v3.5.0+incompatible
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/grafana-tools/sdk v0.0.0-20200326200416-f0431e44c1c3
github.com/grafana-tools/sdk v0.0.0-20200424161218-784ae0c7d6cc
github.com/leodido/go-urn v1.2.0 // indirect
github.com/stretchr/testify v1.4.0
go.uber.org/zap v1.14.1
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6L
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/K-Phoen/grabana v0.10.0 h1:GQcpDAeHS1Tc4ZV2Ws+Dq4AqnIEaPaWRpuWkfsOYhO0=
github.com/K-Phoen/grabana v0.10.0/go.mod h1:dvei2nA99XDfr7odmewPk9+TMtB4VTdMbAxCFXejyKE=
github.com/K-Phoen/grabana v0.11.1 h1:z8ZcNNSgo8svhSC+vmumg026O1O0T1zRAIQNF3oOaXQ=
github.com/K-Phoen/grabana v0.11.1/go.mod h1:d6laJN347jVc/UvHXP94IIh+IInrzD46JsxbsF64oU4=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
Expand Down Expand Up @@ -93,8 +93,10 @@ github.com/gosimple/slug v1.1.1 h1:fRu/digW+NMwBIP+RmviTK97Ho/bEj/C9swrCspN3D4=
github.com/gosimple/slug v1.1.1/go.mod h1:ER78kgg1Mv0NQGlXiDe57DpCyfbNywXXZ9mIorhxAf0=
github.com/gosimple/slug v1.9.0 h1:r5vDcYrFz9BmfIAMC829un9hq7hKM4cHUrsv36LbEqs=
github.com/gosimple/slug v1.9.0/go.mod h1:AMZ+sOVe65uByN3kgEyf9WEBKBCSS+dJjMX9x4vDJbg=
github.com/grafana-tools/sdk v0.0.0-20200326200416-f0431e44c1c3 h1:VTVvPDSLqUYbiAl3VxiVWELHhMzChi+mo32+aPhQ6AM=
github.com/grafana-tools/sdk v0.0.0-20200326200416-f0431e44c1c3/go.mod h1:aqBqJVTJmj0MTX9cP8wuReJPte6HyttMDzSS2u8nJwo=
github.com/grafana-tools/sdk v0.0.0-20200421141239-edb3b044281c h1:T1ckSwJLGPeKLa5eTUFQ8Ho3DpThl/tlPYz4IWjevpA=
github.com/grafana-tools/sdk v0.0.0-20200421141239-edb3b044281c/go.mod h1:aqBqJVTJmj0MTX9cP8wuReJPte6HyttMDzSS2u8nJwo=
github.com/grafana-tools/sdk v0.0.0-20200424161218-784ae0c7d6cc h1:vbciiLT0YD3wsBITfwdQF/UNVYjMxD5xh0kJQesrypk=
github.com/grafana-tools/sdk v0.0.0-20200424161218-784ae0c7d6cc/go.mod h1:aqBqJVTJmj0MTX9cP8wuReJPte6HyttMDzSS2u8nJwo=
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
Expand Down
156 changes: 152 additions & 4 deletions internal/pkg/converter/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
grabanaDashboard "github.com/K-Phoen/grabana/dashboard"
grabana "github.com/K-Phoen/grabana/decoder"
grabanaTable "github.com/K-Phoen/grabana/table"
"github.com/K-Phoen/grabana/target/stackdriver"
"github.com/grafana-tools/sdk"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -60,8 +61,10 @@ func (converter *JSON) convertGeneralSettings(board *sdk.Board, dashboard *graba
dashboard.SharedCrosshair = board.SharedCrosshair
dashboard.Tags = board.Tags
dashboard.Editable = board.Editable
dashboard.Time = [2]string{board.Time.From, board.Time.To}
dashboard.Timezone = board.Timezone

if board.Refresh != nil && board.Refresh.Flag {
if board.Refresh != nil {
dashboard.AutoRefresh = board.Refresh.Value
}
}
Expand Down Expand Up @@ -420,16 +423,161 @@ func (converter *JSON) convertTable(panel sdk.Panel) grabana.DashboardPanel {
}

func (converter *JSON) convertTarget(target sdk.Target) *grabana.Target {
// FIXME
prometheusTarget := &grabana.Target{
// looks like a prometheus target
if target.Expr != "" {
return converter.convertPrometheusTarget(target)
}

if target.MetricType != "" {
return converter.convertStackdriverTarget(target)
}

converter.logger.Warn("unhandled target type: skipped", zap.Any("target", target))

return nil
}

func (converter *JSON) convertPrometheusTarget(target sdk.Target) *grabana.Target {
return &grabana.Target{
Prometheus: &grabana.PrometheusTarget{
Query: target.Expr,
Legend: target.LegendFormat,
Ref: target.RefID,
},
}
}

func (converter *JSON) convertStackdriverTarget(target sdk.Target) *grabana.Target {
switch strings.ToLower(target.MetricKind) {
case "cumulative":
case "gauge":
case "delta":
default:
converter.logger.Warn("unhandled stackdriver metric kind: target skipped", zap.Any("metricKind", target.MetricKind))
return nil
}

var aggregation string
if target.CrossSeriesReducer != "" {
aggregationMap := map[string]string{
string(stackdriver.ReduceNone): "none",
string(stackdriver.ReduceMean): "mean",
string(stackdriver.ReduceMin): "min",
string(stackdriver.ReduceMax): "max",
string(stackdriver.ReduceSum): "sum",
string(stackdriver.ReduceStdDev): "stddev",
string(stackdriver.ReduceCount): "count",
string(stackdriver.ReduceCountTrue): "count_true",
string(stackdriver.ReduceCountFalse): "count_false",
string(stackdriver.ReduceCountFractionTrue): "fraction_true",
string(stackdriver.ReducePercentile99): "percentile_99",
string(stackdriver.ReducePercentile95): "percentile_95",
string(stackdriver.ReducePercentile50): "percentile_50",
string(stackdriver.ReducePercentile05): "percentile_05",
}

if agg, ok := aggregationMap[target.CrossSeriesReducer]; ok {
aggregation = agg
} else {
converter.logger.Warn("unhandled stackdriver crossSeriesReducer: target skipped", zap.Any("crossSeriesReducer", target.CrossSeriesReducer))
}
}

var alignment *grabana.StackdriverAlignment
if target.PerSeriesAligner != "" {
alignmentMethodMap := map[string]string{
string(stackdriver.AlignNone): "none",
string(stackdriver.AlignDelta): "delta",
string(stackdriver.AlignRate): "rate",
string(stackdriver.AlignInterpolate): "interpolate",
string(stackdriver.AlignNextOlder): "next_older",
string(stackdriver.AlignMin): "min",
string(stackdriver.AlignMax): "max",
string(stackdriver.AlignMean): "mean",
string(stackdriver.AlignCount): "count",
string(stackdriver.AlignSum): "sum",
string(stackdriver.AlignStdDev): "stddev",
string(stackdriver.AlignCountTrue): "count_true",
string(stackdriver.AlignCountFalse): "count_false",
string(stackdriver.AlignFractionTrue): "fraction_true",
string(stackdriver.AlignPercentile99): "percentile_99",
string(stackdriver.AlignPercentile95): "percentile_95",
string(stackdriver.AlignPercentile50): "percentile_50",
string(stackdriver.AlignPercentile05): "percentile_05",
string(stackdriver.AlignPercentChange): "percent_change",
}

if method, ok := alignmentMethodMap[target.PerSeriesAligner]; ok {
alignment = &grabana.StackdriverAlignment{
Period: target.AlignmentPeriod,
Method: method,
}
} else {
converter.logger.Warn("unhandled stackdriver perSeriesAligner: target skipped", zap.Any("perSeriesAligner", target.PerSeriesAligner))
}
}

return &grabana.Target{
Stackdriver: &grabana.StackdriverTarget{
Type: strings.ToLower(target.MetricKind),
Metric: target.MetricType,
Filters: converter.convertStackdriverFilters(target),
Aggregation: aggregation,
Alignment: alignment,
Legend: target.AliasBy,
Ref: target.RefID,
},
}
}

func (converter *JSON) convertStackdriverFilters(target sdk.Target) grabana.StackdriverFilters {
filters := grabana.StackdriverFilters{
Eq: map[string]string{},
Neq: map[string]string{},
Matches: map[string]string{},
NotMatches: map[string]string{},
}

var leftOperand, rightOperand, operator *string
for i := range target.Filters {
if target.Filters[i] == "AND" {
continue
}

if leftOperand == nil {
leftOperand = &target.Filters[i]
continue
}
if operator == nil {
operator = &target.Filters[i]
continue
}
if rightOperand == nil {
rightOperand = &target.Filters[i]
}

if leftOperand != nil && operator != nil && rightOperand != nil {
switch *operator {
case "=":
filters.Eq[*leftOperand] = *rightOperand
case "!=":
filters.Neq[*leftOperand] = *rightOperand
case "=~":
filters.Matches[*leftOperand] = *rightOperand
case "!=~":
filters.NotMatches[*leftOperand] = *rightOperand
default:
converter.logger.Warn("unhandled stackdriver filter operator: filter skipped", zap.Any("operator", *operator))
}

leftOperand = nil
rightOperand = nil
operator = nil
}
}

return filters

return prometheusTarget
}

func panelSpan(panel sdk.Panel) float32 {
Expand Down
Loading

0 comments on commit c349b3c

Please sign in to comment.