This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
logUtils.go
159 lines (130 loc) · 4.51 KB
/
logUtils.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
package api
import (
"context"
"net/http"
"strings"
"sync"
"time"
"github.com/benbjohnson/clock"
"github.com/keptn/go-utils/pkg/api/models"
v2 "github.com/keptn/go-utils/pkg/api/utils/v2"
"github.com/keptn/go-utils/pkg/common/httputils"
)
const v1LogPath = "/v1/log"
var defaultSyncInterval = 1 * time.Minute
type LogsV1Interface interface {
ILogHandler
}
//go:generate moq -pkg utils_mock -skip-ensure -out ./fake/log_handler_mock.go . ILogHandler
type ILogHandler interface {
// Log appends the specified logs to the log cache.
Log(logs []models.LogEntry)
// Flush flushes the log cache.
Flush() error
// GetLogs gets logs with the specified parameters.
GetLogs(params models.GetLogsParams) (*models.GetLogsResponse, error)
// DeleteLogs deletes logs matching the specified log filter.
DeleteLogs(filter models.LogFilter) error
Start(ctx context.Context)
}
type LogHandler struct {
logHandler *v2.LogHandler
BaseURL string
AuthToken string
AuthHeader string
HTTPClient *http.Client
Scheme string
LogCache []models.LogEntry
TheClock clock.Clock
SyncInterval time.Duration
lock sync.Mutex
}
// NewLogHandler returns a new LogHandler
func NewLogHandler(baseURL string) *LogHandler {
return NewLogHandlerWithHTTPClient(baseURL, &http.Client{Transport: getClientTransport(nil)})
}
// NewLogHandlerWithHTTPClient returns a new LogHandler that uses the specified http.Client
func NewLogHandlerWithHTTPClient(baseURL string, httpClient *http.Client) *LogHandler {
return &LogHandler{
BaseURL: httputils.TrimHTTPScheme(baseURL),
HTTPClient: httpClient,
Scheme: "http",
LogCache: []models.LogEntry{},
TheClock: clock.New(),
SyncInterval: defaultSyncInterval,
logHandler: v2.NewLogHandlerWithHTTPClient(baseURL, httpClient),
}
}
// NewAuthenticatedLogHandler returns a new EventHandler that authenticates at the endpoint via the provided token
// Deprecated: use APISet instead
func NewAuthenticatedLogHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *LogHandler {
if httpClient == nil {
httpClient = &http.Client{}
}
httpClient.Transport = getClientTransport(httpClient.Transport)
return createAuthenticatedLogHandler(baseURL, authToken, authHeader, httpClient, scheme)
}
func createAuthenticatedLogHandler(baseURL string, authToken string, authHeader string, httpClient *http.Client, scheme string) *LogHandler {
v2LogHandler := v2.NewAuthenticatedLogHandler(baseURL, authToken, authHeader, httpClient, scheme)
baseURL = strings.TrimRight(baseURL, "/")
if !strings.HasSuffix(baseURL, shipyardControllerBaseURL) {
baseURL += "/" + shipyardControllerBaseURL
}
return &LogHandler{
BaseURL: httputils.TrimHTTPScheme(baseURL),
AuthHeader: authHeader,
AuthToken: authToken,
HTTPClient: httpClient,
Scheme: scheme,
LogCache: []models.LogEntry{},
TheClock: clock.New(),
SyncInterval: defaultSyncInterval,
logHandler: v2LogHandler,
}
}
func (lh *LogHandler) getBaseURL() string {
return lh.BaseURL
}
func (lh *LogHandler) getAuthToken() string {
return lh.AuthToken
}
func (lh *LogHandler) getAuthHeader() string {
return lh.AuthHeader
}
func (lh *LogHandler) getHTTPClient() *http.Client {
return lh.HTTPClient
}
// Log appends the specified logs to the log cache.
func (lh *LogHandler) Log(logs []models.LogEntry) {
lh.ensureHandlerIsSet()
lh.logHandler.Log(logs, v2.LogsLogOptions{})
}
// GetLogs gets logs with the specified parameters.
func (lh *LogHandler) GetLogs(params models.GetLogsParams) (*models.GetLogsResponse, error) {
lh.ensureHandlerIsSet()
return lh.logHandler.GetLogs(context.TODO(), params, v2.LogsGetLogsOptions{})
}
// DeleteLogs deletes logs matching the specified log filter.
func (lh *LogHandler) DeleteLogs(params models.LogFilter) error {
lh.ensureHandlerIsSet()
return lh.logHandler.DeleteLogs(context.TODO(), params, v2.LogsDeleteLogsOptions{})
}
func (lh *LogHandler) Start(ctx context.Context) {
lh.ensureHandlerIsSet()
lh.logHandler.Start(ctx, v2.LogsStartOptions{})
}
// Flush flushes the log cache.
func (lh *LogHandler) Flush() error {
lh.ensureHandlerIsSet()
return lh.logHandler.Flush(context.TODO(), v2.LogsFlushOptions{})
}
func (lh *LogHandler) ensureHandlerIsSet() {
if lh.logHandler != nil {
return
}
if lh.AuthToken != "" {
lh.logHandler = v2.NewAuthenticatedLogHandler(lh.BaseURL, lh.AuthToken, lh.AuthHeader, lh.HTTPClient, lh.Scheme)
} else {
lh.logHandler = v2.NewLogHandlerWithHTTPClient(lh.BaseURL, lh.HTTPClient)
}
}