This repository has been archived by the owner on Feb 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
hooks.go
137 lines (117 loc) · 3.75 KB
/
hooks.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
// Copyright 2019, Pure Storage Inc.
//
// 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 hooks
import (
"context"
"github.com/PureStorage-OpenConnect/pure1-unplugged/pkg/clients/elastic"
"github.com/PureStorage-OpenConnect/pure1-unplugged/pkg/common/timing"
log "github.com/sirupsen/logrus"
)
// NewErrorLogHook crates a hook to store logs with the matching levels in elastic
func NewErrorLogHook(source string, logLevels []log.Level, client *elastic.Client) (*ElasticHook, error) {
ctx, cancel := context.WithCancel(context.TODO())
err := client.CreateErrorLogTemplate(ctx)
if err != nil {
log.WithError(err).Error("ErrorLogHook failed to verify index template")
cancel()
return nil, err
}
return &ElasticHook{
client: client,
ctx: ctx,
ctxCancel: cancel,
fire: errorLogHookFireAsync,
index: elastic.GetErrorLogIndexName,
levels: logLevels,
source: source,
}, nil
}
// NewStageTimerHook creates a hook to store logs from the StageTimer in elastic
func NewStageTimerHook(source string, client *elastic.Client) (*ElasticHook, error) {
ctx, cancel := context.WithCancel(context.TODO())
err := client.CreateTimerLogTemplate(ctx)
if err != nil {
log.WithError(err).Error("StageTimerHook failed to verify index template")
cancel()
return nil, err
}
return &ElasticHook{
client: client,
ctx: ctx,
ctxCancel: cancel,
fire: stageTimerFireFuncAsync,
index: elastic.GetTimerLogIndexName,
levels: []log.Level{log.DebugLevel},
source: source,
}, nil
}
// Cancel is required for the implementation
func (hook *ElasticHook) Cancel() {
hook.ctxCancel()
}
// Fire is required for the implementation
func (hook *ElasticHook) Fire(entry *log.Entry) error {
return hook.fire(entry, hook)
}
// Levels is required for the implementation
func (hook *ElasticHook) Levels() []log.Level {
return hook.levels
}
func errorLogHookFireAsync(entry *log.Entry, hook *ElasticHook) error {
go errorLogHookFire(entry, hook)
return nil
}
func errorLogHookFire(entry *log.Entry, hook *ElasticHook) error {
_, err := hook.client.Client().
Index().
Index(hook.index()).
Type("_doc").
BodyJson(processErrorLogEntry(entry, hook)).
Do(hook.ctx)
return err
}
func processErrorLogEntry(entry *log.Entry, hook *ElasticHook) map[string]interface{} {
data := entry.Data
data["frame_file"] = entry.Caller.File
data["frame_function"] = entry.Caller.Function
data["frame_line"] = entry.Caller.Line
data["level"] = entry.Level.String()
data["message"] = entry.Message
data["source"] = hook.source
data["timestamp"] = entry.Time.UTC().Unix()
return data
}
func processStageTimerEntry(entry *log.Entry, hook *ElasticHook) map[string]interface{} {
data := entry.Data
data["timestamp"] = entry.Time.UTC().Unix()
data["source"] = hook.source
return data
}
func stageTimerFireFuncAsync(entry *log.Entry, hook *ElasticHook) error {
go stageTimerFireFunc(entry, hook)
return nil
}
func stageTimerFireFunc(entry *log.Entry, hook *ElasticHook) error {
// Ignore all logs that are not from the stage timer
if entry.Message != timing.DebugMessage {
return nil
}
_, err := hook.client.Client().
Index().
Index(hook.index()).
Type("_doc").
BodyJson(processStageTimerEntry(entry, hook)).
Do(hook.ctx)
return err
}