forked from chrissnell/crabby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_log.go
181 lines (157 loc) · 4.14 KB
/
storage_log.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
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"sync"
"time"
)
// LogConfig describes the YAML-provided configuration for a logging
// storage backend
type LogConfig struct {
File string `yaml:"file"`
Format FormatConfig `yaml:"format"`
Time TimeConfig `yaml:"time"`
}
type FormatConfig struct {
Metric string `yaml:"metric"`
Event string `yaml:"event"`
Tag string `yaml:"tag"`
TagSeparator string `yaml:"tag-seperator"`
}
type TimeConfig struct {
Location string `yaml:"location"`
Format string `yaml:"format"`
}
type LogStorage struct {
Stream *os.File
Format FormatConfig
Location *time.Location
TimeFormat string
}
// StartStorageEngine creates a goroutine loop to receive metrics and send
// them off to the log file
func (l LogStorage) StartStorageEngine(ctx context.Context, wg *sync.WaitGroup) (chan<- Metric, chan<- Event) {
metricChan := make(chan Metric, 10)
eventChan := make(chan Event, 10)
// Start processing the metrics we receive
go l.processMetricsAndEvents(ctx, wg, metricChan, eventChan)
return metricChan, eventChan
}
func (l LogStorage) processMetricsAndEvents(ctx context.Context, wg *sync.WaitGroup, mchan <-chan Metric, echan <-chan Event) {
wg.Add(1)
defer wg.Done()
for {
select {
case m := <-mchan:
err := l.sendMetric(m)
if err != nil {
log.Println(err)
}
case e := <-echan:
err := l.sendEvent(e)
if err != nil {
log.Println(err)
}
case <-ctx.Done():
log.Println("Cancellation request recieved. Cancelling metrics processor.")
l.Stream.Close()
return
}
}
}
func (l LogStorage) BuildTagFormatString(tags map[string]string) string {
if len(tags) == 0 {
return ""
}
var sb strings.Builder
for name, value := range tags {
replacer := strings.NewReplacer(
"%name", name,
"%value", value,
)
sb.WriteString(replacer.Replace(l.Format.Tag))
sb.WriteString(l.Format.TagSeparator)
}
return strings.TrimSuffix(sb.String(), l.Format.TagSeparator)
}
func (l LogStorage) BuildMetricFormatString(m Metric) string {
replacer := strings.NewReplacer(
"%job", m.Job,
"%timing", m.Timing,
"%value", fmt.Sprintf("%.6g", m.Value),
"%time", m.Timestamp.In(l.Location).Format(l.TimeFormat),
"%url", m.URL,
"%tags", l.BuildTagFormatString(m.Tags))
return replacer.Replace(l.Format.Metric)
}
func (l LogStorage) BuildEventFormatString(e Event) string {
replacer := strings.NewReplacer(
"%name", e.Name,
"%status", fmt.Sprint(e.ServerStatus),
"%time", e.Timestamp.In(l.Location).Format(l.TimeFormat),
"%tags", l.BuildTagFormatString(e.Tags))
return replacer.Replace(l.Format.Event)
}
// sendMetric sends a metric value to the log file
func (l LogStorage) sendMetric(m Metric) error {
_, err := l.Stream.WriteString(l.BuildMetricFormatString(m))
if err != nil {
return err
}
return nil
}
// sendEvent sends an event to the log file
func (l LogStorage) sendEvent(e Event) error {
_, err := l.Stream.WriteString(l.BuildEventFormatString(e))
if err != nil {
return err
}
return nil
}
func NewLogStorage(c *Config) (LogStorage, error) {
var outStream *os.File
var l = LogStorage{}
switch c.Storage.Log.File {
case "stdout":
outStream = os.Stdout
case "stderr":
outStream = os.Stderr
default:
var err error
outStream, err = os.OpenFile(c.Storage.Log.File, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
// Don't defer fileStream close until processor is cancelled.
if err != nil {
return l, err
}
}
if c.Storage.Log.Time.Location == "" {
c.Storage.Log.Time.Location = "Local"
}
location, err := time.LoadLocation(c.Storage.Log.Time.Location)
if err != nil {
return l, err
}
l.TimeFormat = c.Storage.Log.Time.Format
l.Location = location
l.Format = c.Storage.Log.Format
if l.TimeFormat == "" {
l.TimeFormat = "2006/01/02 15:04:05"
}
if l.Format.Metric == "" {
l.Format.Metric = "%time [M: %job] %timing: %value (%tags)\n"
}
if l.Format.Event == "" {
l.Format.Event = "%time [E: %name] status: %status (%tags)\n"
}
if l.Format.Tag == "" {
l.Format.Tag = "%name: %value"
}
if l.Format.TagSeparator == "" {
l.Format.TagSeparator = ", "
}
l.Stream = outStream
return l, nil
}