forked from trickstercache/trickster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
162 lines (142 loc) · 6.23 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
* Copyright 2018 Comcast Cable Communications Management, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import "github.com/BurntSushi/toml"
type Config struct {
Caching CachingConfig `toml:"cache"`
DefaultOriginURL string // to capture a CLI origin url
Logging LoggingConfig `toml:"logging"`
Main GeneralConfig `toml:"main"`
Metrics MetricsConfig `toml:"metrics"`
Origins map[string]PrometheusOriginConfig `toml:"origins"`
ProxyServer ProxyServerConfig `toml:"proxy_server"`
}
// GeneralConfig is a collection of general configuration values.
type GeneralConfig struct {
// InstanceID represents a unique ID for the current instance, when multiple instances on the same host
InstanceID int `toml:"instance_id"`
// Environment indicates the operating environment of the running instance (e.g., "dev", "stage", "prod")
Environment string
// ConfigFile represents the physical filepath to the Trickster Configuration
ConfigFile string
// Hostname is populated with the self-resolved Hostname where the instance is running
Hostname string
}
// ProxyServerConfig is a collection of configurations for the main http listener for the application
type ProxyServerConfig struct {
// ListenAddress is IP address for the main http listener for the application
ListenAddress string `toml:"listen_address"`
// ListenPort is TCP Port for the main http listener for the application
ListenPort int `toml:"listen_port"`
}
// CachingConfig is a collection of defining the Trickster Caching Behavior
type CachingConfig struct {
// CacheType represents the type of cache that we wish to use: "boltdb", "memory", "filesystem", or "redis"
CacheType string `toml:"cache_type"`
RecordTTLSecs int64 `toml:"record_ttl_secs"`
Redis RedisCacheConfig `toml:"redis"`
Filesystem FilesystemCacheConfig `toml:"filesystem"`
ReapSleepMS int64 `toml:"reap_sleep_ms"`
Compression bool `toml:"compression"`
BoltDB BoltDBCacheConfig `toml:"boltdb"`
}
// RedisCacheConfig is a collection of Configurations for Connecting to Redis
type RedisCacheConfig struct {
// Protocol represents the connection method (e.g., "tcp", "unix", etc.)
Protocol string `toml:"protocol"`
// Endpoint represents FQDN:port or IPAddress:Port of the Redis server
Endpoint string `toml:"endpoint"`
// Password can be set when using password protected redis instance.
Password string `toml:"password"`
}
// BoltDBCacheConfig is a collection of Configurations for storing cached data on the Filesystem
type BoltDBCacheConfig struct {
// Filename represents the filename (including path) of the BotlDB database
Filename string `toml:"filename"`
// Bucket represents the name of the bucket within BoltDB under which Trickster's keys will be stored.
Bucket string `toml:"bucket"`
}
// FilesystemCacheConfig is a collection of Configurations for storing cached data on the Filesystem
type FilesystemCacheConfig struct {
// CachePath represents the path on disk where our cache will live
CachePath string `toml:"cache_path"`
}
// PrometheusOriginConfig is a collection of configurations for prometheus origins proxied by Trickster
// You can override these on a per-request basis with url-params
type PrometheusOriginConfig struct {
OriginURL string `toml:"origin_url"`
APIPath string `toml:"api_path"`
IgnoreNoCacheHeader bool `toml:"ignore_no_cache_header"`
MaxValueAgeSecs int64 `toml:"max_value_age_secs"`
FastForwardDisable bool `toml:"fast_forward_disable"`
}
// MetricsConfig is a collection of Metrics Collection configurations
type MetricsConfig struct {
// ListenAddress is IP address from which the Application Metrics are available for pulling at /metrics
ListenAddress string `toml:"listen_address"`
// ListenPort is TCP Port from which the Application Metrics are available for pulling at /metrics
ListenPort int `toml:"listen_port"`
}
// LoggingConfig is a collection of Logging configurations
type LoggingConfig struct {
// LogFile provides the filepath to the instances's logfile. Set as empty string to Log to Console
LogFile string `toml:"log_file"`
// LogLevel provides the most granular level (e.g., DEBUG, INFO, ERROR) to log
LogLevel string `toml:"log_level"`
}
// NewConfig returns a Config initialized with default values.
func NewConfig() *Config {
defaultCachePath := "/tmp/trickster"
defaultBoltDBFile := "trickster.db"
return &Config{
Caching: CachingConfig{
CacheType: ctMemory,
RecordTTLSecs: 21600,
Redis: RedisCacheConfig{Protocol: "tcp", Endpoint: "redis:6379"},
Filesystem: FilesystemCacheConfig{CachePath: defaultCachePath},
BoltDB: BoltDBCacheConfig{Filename: defaultBoltDBFile, Bucket: "trickster"},
ReapSleepMS: 1000,
Compression: true,
},
Logging: LoggingConfig{
LogFile: "",
LogLevel: "INFO",
},
Main: GeneralConfig{
ConfigFile: "/etc/trickster/trickster.conf",
Hostname: "localhost.unknown",
},
Metrics: MetricsConfig{
ListenPort: 8082,
},
Origins: map[string]PrometheusOriginConfig{
"default": defaultOriginConfig(),
},
ProxyServer: ProxyServerConfig{
ListenPort: 9090,
},
}
}
func defaultOriginConfig() PrometheusOriginConfig {
return PrometheusOriginConfig{
OriginURL: "http://prometheus:9090/",
APIPath: prometheusAPIv1Path,
IgnoreNoCacheHeader: true,
MaxValueAgeSecs: 86400, // Keep datapoints up to 24 hours old
}
}
// LoadFile loads application configuration from a TOML-formatted file.
func (c *Config) LoadFile(path string) error {
_, err := toml.DecodeFile(path, &c)
return err
}