Skip to content

Commit

Permalink
Add support for graphTemplates
Browse files Browse the repository at this point in the history
- Implement support for template argument for /render handler.
  Template file can be specified as graphTemplates parameter.
  It supports yaml and toml files, but not the 'ini' style conf
- Make colors to look like in graphite-web in PR graphite-project/graphite-web#2239
- Allow to redefined default colors (see carbonapi.example.yaml/toml for examples)

Fixes #163
  • Loading branch information
Vladimir Smirnov committed Feb 28, 2018
1 parent b50e54e commit 21c5a3c
Show file tree
Hide file tree
Showing 11 changed files with 536 additions and 135 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ CHANGELOG
**Master**
- [Fix] `lineWidth` param is not working
- [Fix] Support `&` in metric name
- [Fix] default colors for png and svg are now defined as in graphite-web after https://github.com/graphite-project/graphite-web/pull/2239
- [Improvement] You can override default colors through config
- [Improvement] It's now possible to specify 'template=' option to png or svg renders. It will use templates that's specified by 'graphTemplates' option in config. Template config format is not compatible with graphite-web though and uses either toml or yaml. Options naming is also different and follows URL override style.
- [Code] Minor API changes in expr/png: MarshalPNGRequest and MarshalSVGRequest now requires to specify template.

**0.9.2**
- [Improvement] "pipe" function chaining syntax. Same way as in https://github.com/graphite-project/graphite-web/pull/2042 (thx. to @lomik)
Expand Down
9 changes: 9 additions & 0 deletions carbonapi.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ maxBatchSize = 100
pidFile = ""
sendGlobsAsIs = false
tz = ""
graphTemplates = "graphTemplates.example.toml"

[cache]
defaultTimeoutSec = 60
Expand Down Expand Up @@ -38,6 +39,14 @@ file = "carbonapi.log"
level = "info"
logger = ""

[defaultColors]
red = "c80032"
green = "00c800"
blue = "6464ff"
darkred = "ff0000"
darkgreen = "00ff0"
darkblue = "0000ff"

[upstreams]
backends = ["http://10.0.0.1:8080", "http://10.0.0.2:8080", "http://192.168.0.100:8080", "http://192.168.0.200:8080", "http://192.168.1.212:8080"]
buckets = 10
Expand Down
16 changes: 11 additions & 5 deletions carbonapi.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,9 @@ upstreams:
# "http://host:port" array of instances of carbonserver stores
# This is the *ONLY* config element in this section that MUST be specified.
backends:
- "http://10.0.0.1:8080"
- "http://10.0.0.2:8080"
- "http://192.168.0.100:8080"
- "http://192.168.0.200:8080"
- "http://192.168.1.212:8080"
- "http://127.0.0.2:8080"
- "http://127.0.0.3:8080"
- "http://127.0.0.4:8080"

carbonsearch:
# Instance of carbonsearch backend
Expand All @@ -108,7 +106,15 @@ upstreams:
# If not zero, enabled cache for find requests
# This parameter controls when it will expire (in seconds)
# Default: 600 (10 minutes)
graphTemplates: graphTemplates.example.yaml
expireDelaySec: 10
defaultColors:
"red": "c80032"
"green": "00c800"
"blue": "6464ff"
"darkred": "ff0000"
"darkgreen": "00ff0"
"darkblue": "0000ff"
logger:
- logger: ""
file: "stderr"
Expand Down
9 changes: 5 additions & 4 deletions expr/png/cairo.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,12 +713,12 @@ func MarshalPNG(params PictureParams, results []*types.MetricData) []byte {
return marshalCairo(params, results, cairoPNG)
}

func MarshalSVGRequest(r *http.Request, results []*types.MetricData) []byte {
return marshalCairo(GetPictureParams(r, results), results, cairoSVG)
func MarshalSVGRequest(r *http.Request, results []*types.MetricData, templateName string) []byte {
return marshalCairo(GetPictureParamsWithTemplate(r, templateName, results), results, cairoSVG)
}

func MarshalPNGRequest(r *http.Request, results []*types.MetricData) []byte {
return marshalCairo(GetPictureParams(r, results), results, cairoPNG)
func MarshalPNGRequest(r *http.Request, results []*types.MetricData, templateName string) []byte {
return marshalCairo(GetPictureParamsWithTemplate(r, templateName, results), results, cairoPNG)
}

func marshalCairo(p PictureParams, results []*types.MetricData, backend cairoBackend) []byte {
Expand Down Expand Up @@ -855,6 +855,7 @@ func marshalCairo(p PictureParams, results []*types.MetricData, backend cairoBac

func drawGraph(cr *cairoSurfaceContext, params *Params, results []*types.MetricData) {
var minNumberOfPoints, maxNumberOfPoints int32

params.secondYAxis = false

params.startTime = -1
Expand Down
69 changes: 65 additions & 4 deletions expr/png/graphutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package png
import (
"image/color"
"strconv"
"strings"
)

func getBool(s string, def bool) bool {
Expand Down Expand Up @@ -52,12 +53,61 @@ func getInt(s string, def int) int {
return int(n)
}

func string2RGBA(clr string) color.RGBA {
if c, ok := colors[clr]; ok {
return c
}
c, err := hexToRGBA(clr)
if err != nil {
return color.RGBA{0, 0, 0, 255}
}
return *c
}

// https://code.google.com/p/sadbox/source/browse/color/hex.go
// hexToRGBA converts an Hex string to a RGB triple.
func hexToRGBA(h string) (*color.RGBA, error) {
var r, g, b uint8
if len(h) > 0 && h[0] == '#' {
h = h[1:]
}

if len(h) == 3 {
h = h[:1] + h[:1] + h[1:2] + h[1:2] + h[2:] + h[2:]
}

alpha := byte(255)

if len(h) == 6 {
if rgb, err := strconv.ParseUint(string(h), 16, 32); err == nil {
r = uint8(rgb >> 16)
g = uint8(rgb >> 8)
b = uint8(rgb)
} else {
return nil, err
}
}

if len(h) == 8 {
if rgb, err := strconv.ParseUint(string(h), 16, 32); err == nil {
r = uint8(rgb >> 24)
g = uint8(rgb >> 16)
b = uint8(rgb >> 8)
alpha = uint8(rgb)
} else {
return nil, err
}
}

return &color.RGBA{r, g, b, alpha}, nil
}

var colors = map[string]color.RGBA{
// Graphite default colors
"black": {0x00, 0x00, 0x00, 0xff},
"white": {0xff, 0xff, 0xff, 0xff},
"blue": {0x64, 0x64, 0xff, 0xff},
"green": {0x00, 0xc8, 0x00, 0xff},
"blue": {0x00, 0x00, 0xff, 0xff},
"green": {0x00, 0xff, 0x00, 0xff},
"red": {0xff, 0x00, 0x00, 0xff},
"yellow": {0xff, 0xff, 0x00, 0xff},
"orange": {0xff, 0xa5, 0x00, 0xff},
Expand All @@ -71,8 +121,8 @@ var colors = map[string]color.RGBA{
"pink": {0xff, 0x64, 0x64, 0xff},
"gold": {0xc8, 0xc8, 0x00, 0xff},
"rose": {0xc8, 0x96, 0xc8, 0xff},
"darkblue": {0x00, 0x00, 0xff, 0xff},
"darkgreen": {0x00, 0xff, 0x00, 0xff},
"darkblue": {0x00, 0x21, 0x73, 0xff},
"darkgreen": {0x00, 0xc8, 0x00, 0xff},
"darkred": {0xc8, 0x00, 0x32, 0xff},
"darkgray": {0x6f, 0x6f, 0x6f, 0xff},
"darkgrey": {0x6f, 0x6f, 0x6f, 0xff},
Expand Down Expand Up @@ -159,3 +209,14 @@ var colors = map[string]color.RGBA{
"lightyellow": {0xff, 0xff, 0xe0, 0xff},
"ivory": {0xff, 0xff, 0xf0, 0xff},
}

func SetColor(name, rgba string) error {
color, err := hexToRGBA(rgba)
if err != nil {
return err
}

name = strings.ToLower(name)
colors[name] = *color
return nil
}
Loading

0 comments on commit 21c5a3c

Please sign in to comment.