-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexporter.go
248 lines (214 loc) · 5.56 KB
/
exporter.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package cmd
import (
"context"
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/thegreenwebfoundation/grid-intensity-go/pkg/provider"
)
const (
labelLocation = "location"
labelNode = "node"
labelProvider = "provider"
labelRegion = "region"
labelUnits = "units"
namespace = "grid_intensity"
nodeKey = "node"
regionKey = "region"
)
func init() {
exporterCmd.Flags().StringP(locationKey, "l", "", "Location code for provider")
exporterCmd.Flags().StringP(nodeKey, "n", "", "Node where the exporter is running")
exporterCmd.Flags().StringP(providerKey, "p", provider.Ember, "Provider of carbon intensity data")
exporterCmd.Flags().StringP(regionKey, "r", "", "Region where the exporter is running")
// Also support environment variables.
viper.SetEnvPrefix("grid_intensity")
viper.BindEnv(locationKey)
viper.BindEnv(providerKey)
viper.BindEnv(regionKey)
viper.BindEnv(nodeKey)
rootCmd.AddCommand(exporterCmd)
}
var (
averageDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "carbon", "average"),
"Average carbon intensity for the electricity grid in this location.",
[]string{
labelLocation,
labelNode,
labelProvider,
labelRegion,
labelUnits,
},
nil,
)
marginalDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "carbon", "marginal"),
"Marginal carbon intensity for the electricity grid in this location.",
[]string{
labelLocation,
labelNode,
labelProvider,
labelRegion,
labelUnits,
},
nil,
)
relativeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "carbon", "relative"),
"Relative carbon intensity for the electricity grid in this location.",
[]string{
labelLocation,
labelNode,
labelProvider,
labelRegion,
labelUnits,
},
nil,
)
exporterCmd = &cobra.Command{
Use: "exporter",
Short: "Metrics for carbon intensity data for electricity grids",
Long: `A prometheus exporter for getting the carbon intensity data for
electricity grids.
This can be used to make your software carbon aware so it runs at times when
the grid is greener or at locations where carbon intensity is lower.
grid-intensity exporter --provider Ember --location IE --region eu-west-1 --node worker-1
grid-intensity exporter -p Ember -l BOL`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag(locationKey, cmd.Flags().Lookup(locationKey))
viper.BindPFlag(nodeKey, cmd.Flags().Lookup(nodeKey))
viper.BindPFlag(providerKey, cmd.Flags().Lookup(providerKey))
viper.BindPFlag(regionKey, cmd.Flags().Lookup(regionKey))
},
Run: func(cmd *cobra.Command, args []string) {
err := runExporter()
if err != nil {
log.Fatal(err)
}
},
}
)
type Exporter struct {
client provider.Interface
location string
node string
provider string
region string
}
type ExporterConfig struct {
Location string
Node string
Provider string
Region string
}
func NewExporter(config ExporterConfig) (*Exporter, error) {
var client provider.Interface
var err error
if config.Location == "" {
return nil, fmt.Errorf("location must be set")
}
// Cache filename is empty so we use in-memory cache.
client, err = getClient(config.Provider, "")
if err != nil {
return nil, err
}
e := &Exporter{
client: client,
location: config.Location,
node: config.Node,
provider: config.Provider,
region: config.Region,
}
return e, nil
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
ctx := context.Background()
result, err := e.client.GetCarbonIntensity(ctx, e.location)
if err != nil {
log.Printf("failed to get carbon intensity %#v", err)
}
for _, data := range result {
desc, err := getMetricDesc(data)
if err != nil {
log.Printf("failed to get metric description %#v", err)
continue
}
ch <- prometheus.MustNewConstMetric(
desc,
prometheus.GaugeValue,
data.Value,
data.Location,
e.node,
data.Provider,
e.region,
data.Units,
)
}
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
if e.provider == provider.WattTime {
ch <- marginalDesc
ch <- relativeDesc
} else {
ch <- averageDesc
}
}
func getMetricDesc(data provider.CarbonIntensity) (*prometheus.Desc, error) {
switch data.MetricType {
case provider.AbsoluteMetricType:
switch data.EmissionsType {
case provider.AverageEmissionsType:
return averageDesc, nil
case provider.MarginalEmissionsType:
return marginalDesc, nil
default:
return nil, fmt.Errorf("unknown emissions type %s", data.EmissionsType)
}
case provider.RelativeMetricType:
return relativeDesc, nil
}
return nil, fmt.Errorf("unknown metric type %s", data.MetricType)
}
func runExporter() error {
providerName, err := readConfig(providerKey)
if err != nil {
return err
}
locationCode, err := readConfig(locationKey)
if err != nil {
return err
}
node, err := readConfig(nodeKey)
if err != nil {
return err
}
region, err := readConfig(regionKey)
if err != nil {
return err
}
c := ExporterConfig{
Location: locationCode,
Node: node,
Provider: providerName,
Region: region,
}
exporter, err := NewExporter(c)
if err != nil {
return err
}
err = writeConfig()
if err != nil {
return err
}
fmt.Printf("Using provider %q with location %q\n", providerName, locationCode)
fmt.Println("Metrics available at :8000/metrics")
prometheus.MustRegister(exporter)
http.Handle("/metrics", promhttp.Handler())
log.Fatalln(http.ListenAndServe(":8000", nil))
return nil
}