forked from juju/charmstore
/
log.go
203 lines (187 loc) · 6.2 KB
/
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package v5 // import "gopkg.in/juju/charmstore.v5/internal/v5"
import (
"encoding/json"
"io"
"net/http"
"strconv"
"gopkg.in/errgo.v1"
"gopkg.in/juju/charm.v6"
"gopkg.in/juju/charmrepo.v3/csclient/params"
"gopkg.in/mgo.v2/bson"
"gopkg.in/juju/charmstore.v5/internal/mongodoc"
)
// GET /log
// https://github.com/juju/charmstore/blob/v4/docs/API.md#get-log
//
// POST /log
// https://github.com/juju/charmstore/blob/v4/docs/API.md#post-log
func (h *ReqHandler) serveLog(w http.ResponseWriter, req *http.Request) error {
if err := h.authenticateAdmin(req); err != nil {
return errgo.Mask(err, errgo.Any)
}
switch req.Method {
case "GET":
return h.getLogs(w, req)
case "POST":
return h.postLogs(w, req)
}
return errgo.WithCausef(nil, params.ErrMethodNotAllowed, "%s method not allowed", req.Method)
}
func (h *ReqHandler) getLogs(w http.ResponseWriter, req *http.Request) error {
w.Header().Set("content-type", "application/json")
encoder := json.NewEncoder(w)
// Retrieve values from the query string.
limit, err := intValue(req.Form.Get("limit"), 1, 1000)
if err != nil {
return badRequestf(err, "invalid limit value")
}
offset, err := intValue(req.Form.Get("skip"), 0, 0)
if err != nil {
return badRequestf(err, "invalid skip value")
}
id := req.Form.Get("id")
strLevel := req.Form.Get("level")
strType := req.Form.Get("type")
// Build the Mongo query.
query := make(bson.D, 0, 3)
if id != "" {
url, err := charm.ParseURL(id)
if err != nil {
return badRequestf(err, "invalid id value")
}
query = append(query, bson.DocElem{"urls", url})
}
if strLevel != "" {
logLevel, ok := paramsLogLevels[params.LogLevel(strLevel)]
if !ok {
return badRequestf(nil, "invalid log level value")
}
query = append(query, bson.DocElem{"level", logLevel})
}
if strType != "" {
logType, ok := paramsLogTypes[params.LogType(strType)]
if !ok {
return badRequestf(nil, "invalid log type value")
}
query = append(query, bson.DocElem{"type", logType})
}
// Retrieve the logs.
outputStarted := false
closingContent := "[]"
var log mongodoc.Log
iter := h.Store.DB.Logs().Find(query).Sort("-_id").Skip(offset).Limit(limit).Iter()
for iter.Next(&log) {
// Start writing the response body. The logs are streamed, but we wrap
// the output in square brackets and we separate entries with commas so
// that it's more easy for clients to parse the response.
closingContent = "]"
if outputStarted {
if err := writeString(w, ","); err != nil {
return errgo.Notef(err, "cannot write response")
}
} else {
if err := writeString(w, "["); err != nil {
return errgo.Notef(err, "cannot write response")
}
outputStarted = true
}
logResponse := ¶ms.LogResponse{
Data: json.RawMessage(log.Data),
Level: mongodocLogLevels[log.Level],
Type: mongodocLogTypes[log.Type],
URLs: log.URLs,
Time: log.Time.UTC(),
}
if err := encoder.Encode(logResponse); err != nil {
// Since we only allow properly encoded JSON messages to be stored
// in the database, this should never happen. Moreover, at this
// point we already sent a chunk of the 200 response, so we just
// log the error.
logger.Errorf("cannot marshal log: %s", err)
}
}
if err := iter.Close(); err != nil {
return errgo.Notef(err, "cannot retrieve logs")
}
// Close the JSON list, or just write an empty list, depending on whether
// we had results.
if err := writeString(w, closingContent); err != nil {
return errgo.Notef(err, "cannot write response")
}
return nil
}
func (h *ReqHandler) postLogs(w http.ResponseWriter, req *http.Request) error {
// Check the request content type.
if ctype := req.Header.Get("Content-Type"); ctype != "application/json" {
return badRequestf(nil, "unexpected Content-Type %q; expected 'application/json'", ctype)
}
// Unmarshal the request body.
var logs []params.Log
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&logs); err != nil {
return badRequestf(err, "cannot unmarshal body")
}
for _, log := range logs {
// Validate the provided level and type.
logLevel, ok := paramsLogLevels[log.Level]
if !ok {
return badRequestf(nil, "invalid log level")
}
logType, ok := paramsLogTypes[log.Type]
if !ok {
return badRequestf(nil, "invalid log type")
}
// Add the log to the database.
if err := h.Store.AddLog(log.Data, logLevel, logType, log.URLs); err != nil {
return errgo.Notef(err, "cannot add log")
}
}
return nil
}
func writeString(w io.Writer, content string) error {
_, err := w.Write([]byte(content))
return err
}
// TODO (frankban): use slices instead of maps for the data structures below.
var (
// mongodocLogLevels maps internal mongodoc log levels to API ones.
mongodocLogLevels = map[mongodoc.LogLevel]params.LogLevel{
mongodoc.InfoLevel: params.InfoLevel,
mongodoc.WarningLevel: params.WarningLevel,
mongodoc.ErrorLevel: params.ErrorLevel,
}
// paramsLogLevels maps API params log levels to internal mongodoc ones.
paramsLogLevels = map[params.LogLevel]mongodoc.LogLevel{
params.InfoLevel: mongodoc.InfoLevel,
params.WarningLevel: mongodoc.WarningLevel,
params.ErrorLevel: mongodoc.ErrorLevel,
}
// mongodocLogTypes maps internal mongodoc log types to API ones.
mongodocLogTypes = map[mongodoc.LogType]params.LogType{
mongodoc.IngestionType: params.IngestionType,
mongodoc.LegacyStatisticsType: params.LegacyStatisticsType,
}
// paramsLogTypes maps API params log types to internal mongodoc ones.
paramsLogTypes = map[params.LogType]mongodoc.LogType{
params.IngestionType: mongodoc.IngestionType,
params.LegacyStatisticsType: mongodoc.LegacyStatisticsType,
}
)
// intValue checks that the given string value is a number greater than or
// equal to the given minValue. If the provided value is an empty string, the
// defaultValue is returned without errors.
func intValue(strValue string, minValue, defaultValue int) (int, error) {
if strValue == "" {
return defaultValue, nil
}
value, err := strconv.Atoi(strValue)
if err != nil {
return 0, errgo.New("value must be a number")
}
if value < minValue {
return 0, errgo.Newf("value must be >= %d", minValue)
}
return value, nil
}