1package config
2
3import (
4	"encoding/json"
5	"time"
6
7	"github.com/go-graphite/carbonapi/cache"
8	"github.com/go-graphite/carbonapi/cmd/carbonapi/interfaces"
9	"github.com/go-graphite/carbonapi/limiter"
10	zipperCfg "github.com/go-graphite/carbonapi/zipper/config"
11	zipperTypes "github.com/go-graphite/carbonapi/zipper/types"
12
13	"github.com/lomik/zapwriter"
14)
15
16var DefaultLoggerConfig = zapwriter.Config{
17	Logger:           "",
18	File:             "stdout",
19	Level:            "info",
20	Encoding:         "console",
21	EncodingTime:     "iso8601",
22	EncodingDuration: "seconds",
23}
24
25type CacheConfig struct {
26	Type              string   `mapstructure:"type"`
27	Size              int      `mapstructure:"size_mb"`
28	MemcachedServers  []string `mapstructure:"memcachedServers"`
29	DefaultTimeoutSec int32    `mapstructure:"defaultTimeoutSec"`
30}
31
32type GraphiteConfig struct {
33	Pattern  string
34	Host     string
35	Interval time.Duration
36	Prefix   string
37}
38
39type Define struct {
40	Name     string `mapstructure:"name"`
41	Template string `mapstructure:"template"`
42}
43
44type ExpvarConfig struct {
45	Listen       string `mapstructure:"listen"`
46	Enabled      bool   `mapstructure:"enabled"`
47	PProfEnabled bool   `mapstructure:"pprofEnabled"`
48}
49
50type ConfigType struct {
51	ExtrapolateExperiment      bool               `mapstructure:"extrapolateExperiment"`
52	Logger                     []zapwriter.Config `mapstructure:"logger"`
53	Listen                     string             `mapstructure:"listen"`
54	Buckets                    int                `mapstructure:"buckets"`
55	Concurency                 int                `mapstructure:"concurency"`
56	ResponseCacheConfig        CacheConfig        `mapstructure:"cache"`
57	BackendCacheConfig         CacheConfig        `mapstructure:"backendCache"`
58	Cpus                       int                `mapstructure:"cpus"`
59	TimezoneString             string             `mapstructure:"tz"`
60	UnicodeRangeTables         []string           `mapstructure:"unicodeRangeTables"`
61	Graphite                   GraphiteConfig     `mapstructure:"graphite"`
62	IdleConnections            int                `mapstructure:"idleConnections"`
63	PidFile                    string             `mapstructure:"pidFile"`
64	SendGlobsAsIs              *bool              `mapstructure:"sendGlobsAsIs"`
65	AlwaysSendGlobsAsIs        *bool              `mapstructure:"alwaysSendGlobsAsIs"`
66	MaxBatchSize               int                `mapstructure:"maxBatchSize"`
67	Zipper                     string             `mapstructure:"zipper"`
68	Upstreams                  zipperCfg.Config   `mapstructure:"upstreams"`
69	ExpireDelaySec             int32              `mapstructure:"expireDelaySec"`
70	GraphiteWeb09Compatibility bool               `mapstructure:"graphite09compat"`
71	IgnoreClientTimeout        bool               `mapstructure:"ignoreClientTimeout"`
72	DefaultColors              map[string]string  `mapstructure:"defaultColors"`
73	GraphTemplates             string             `mapstructure:"graphTemplates"`
74	FunctionsConfigs           map[string]string  `mapstructure:"functionsConfig"`
75	HeadersToPass              []string           `mapstructure:"headersToPass"`
76	HeadersToLog               []string           `mapstructure:"headersToLog"`
77	Define                     []Define           `mapstructure:"define"`
78	Prefix                     string             `mapstructure:"prefix"`
79	Expvar                     ExpvarConfig       `mapstructure:"expvar"`
80	NotFoundStatusCode         int                `mapstructure:"notFoundStatusCode"`
81	HTTPResponseStackTrace     bool               `mapstructure:"httpResponseStackTrace"`
82
83	ResponseCache cache.BytesCache `mapstructure:"-" json:"-"`
84	BackendCache  cache.BytesCache `mapstructure:"-" json:"-"`
85
86	DefaultTimeZone *time.Location `mapstructure:"-" json:"-"`
87
88	// ZipperInstance is API entry to carbonzipper
89	ZipperInstance interfaces.CarbonZipper `mapstructure:"-" json:"-"`
90
91	// Limiter limits concurrent zipper requests
92	Limiter limiter.SimpleLimiter `mapstructure:"-" json:"-"`
93}
94
95// skipcq: CRT-P0003
96func (c ConfigType) String() string {
97	data, err := json.Marshal(c)
98	if err != nil {
99		return "Failed to marshal config: " + err.Error()
100	} else {
101		return string(data)
102	}
103}
104
105var Config = ConfigType{
106	ExtrapolateExperiment: false,
107	Listen:                "[::]:8081",
108	Buckets:               10,
109	Concurency:            1000,
110	MaxBatchSize:          100,
111	ResponseCacheConfig: CacheConfig{
112		Type:              "mem",
113		DefaultTimeoutSec: 60,
114	},
115	BackendCacheConfig: CacheConfig{
116		Type:              "null",
117		DefaultTimeoutSec: 0,
118	},
119	TimezoneString: "",
120	Graphite: GraphiteConfig{
121		Pattern:  "{prefix}.{fqdn}",
122		Host:     "",
123		Interval: 60 * time.Second,
124		Prefix:   "carbon.api",
125	},
126	Cpus:            0,
127	IdleConnections: 10,
128	PidFile:         "",
129
130	ResponseCache: cache.NullCache{},
131	BackendCache:  cache.NullCache{},
132
133	DefaultTimeZone: time.Local,
134	Logger:          []zapwriter.Config{DefaultLoggerConfig},
135
136	Upstreams: zipperCfg.Config{
137		Timeouts: zipperTypes.Timeouts{
138			Render:  10000 * time.Second,
139			Find:    2 * time.Second,
140			Connect: 200 * time.Millisecond,
141		},
142		KeepAliveInterval: 30 * time.Second,
143
144		MaxIdleConnsPerHost: 100,
145	},
146	ExpireDelaySec:             10 * 60,
147	GraphiteWeb09Compatibility: false,
148	Prefix:                     "",
149	Expvar: ExpvarConfig{
150		Listen:       "",
151		Enabled:      true,
152		PProfEnabled: false,
153	},
154	NotFoundStatusCode:     200,
155	HTTPResponseStackTrace: true,
156}
157