Skip to content

Commit 8e17683

Browse files
committed
Per new api
1 parent cb68edd commit 8e17683

File tree

3 files changed

+80
-91
lines changed

3 files changed

+80
-91
lines changed

client.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ type (
1414
AppID string
1515
AppName string
1616
}
17+
18+
Fields map[string]interface{}
19+
20+
SearchParameters struct {
21+
Query string `json:"query"`
22+
QueryString string `json:"query_string"`
23+
Since string `json:"since"`
24+
Sort []string `json:"sort"`
25+
Size int `json:"size"`
26+
From int `json:"from"`
27+
}
1728
)
1829

1930
const (
@@ -67,7 +78,7 @@ func (c *Client) Log() (log *Log) {
6778
BatchSize: 60,
6879
DispatchInterval: 60,
6980
}
70-
log.resetLogEntries()
81+
log.resetEntries()
7182
return
7283
}
7384

@@ -78,3 +89,12 @@ func (c *Client) Store() *Store {
7889
logger: c.logger,
7990
}
8091
}
92+
93+
func (f Fields) Add(key string, value interface{}) Fields {
94+
f[key] = value
95+
return f
96+
}
97+
98+
func (f Fields) Get(key string) interface{} {
99+
return f[key]
100+
}

log.go

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ type (
1313
// Log defines the LabStack log service.
1414
Log struct {
1515
sling *sling.Sling
16-
logs []*logEntry
16+
entries []Fields
1717
timer <-chan time.Time
1818
mutex sync.RWMutex
1919
logger *glog.Logger
2020
AppID string
2121
AppName string
22-
Tags []string
2322
Level Level
2423
BatchSize int
2524
DispatchInterval int
@@ -28,16 +27,6 @@ type (
2827
// Level defines the log level.
2928
Level int
3029

31-
// logEntry defines a log entry.
32-
logEntry struct {
33-
Time string `json:"time"`
34-
AppID string `json:"app_id"`
35-
AppName string `json:"app_name"`
36-
Tags []string `json:"tags"`
37-
Level string `json:"level"`
38-
Message string `json:"message"`
39-
}
40-
4130
// LogError defines the log error.
4231
LogError struct {
4332
Code int `json:"code"`
@@ -63,43 +52,43 @@ var levels = map[Level]string{
6352
LevelFatal: "FATAL",
6453
}
6554

66-
func (l *Log) resetLogEntries() {
55+
func (l *Log) resetEntries() {
6756
l.mutex.Lock()
6857
defer l.mutex.Unlock()
69-
l.logs = make([]*logEntry, 0, l.BatchSize)
58+
l.entries = make([]Fields, 0, l.BatchSize)
7059
}
7160

72-
func (l *Log) appendLogEntry(lm *logEntry) {
61+
func (l *Log) appendEntry(f Fields) {
7362
l.mutex.Lock()
7463
defer l.mutex.Unlock()
75-
l.logs = append(l.logs, lm)
64+
l.entries = append(l.entries, f)
7665
}
7766

78-
func (l *Log) listLogEntries() []*logEntry {
67+
func (l *Log) listEntries() []Fields {
7968
l.mutex.Lock()
8069
defer l.mutex.Unlock()
81-
logs := make([]*logEntry, len(l.logs))
82-
for i, log := range l.logs {
83-
logs[i] = log
70+
entries := make([]Fields, len(l.entries))
71+
for i, entry := range l.entries {
72+
entries[i] = entry
8473
}
85-
return logs
74+
return entries
8675
}
8776

8877
func (l *Log) logsLength() int {
8978
l.mutex.RLock()
9079
defer l.mutex.RUnlock()
91-
return len(l.logs)
80+
return len(l.entries)
9281
}
9382

9483
func (l *Log) dispatch() error {
95-
if len(l.logs) == 0 {
84+
if len(l.entries) == 0 {
9685
return nil
9786
}
9887

99-
defer l.resetLogEntries()
88+
defer l.resetEntries()
10089

10190
le := new(LogError)
102-
_, err := l.sling.Post("").BodyJSON(l.listLogEntries()).Receive(nil, le)
91+
_, err := l.sling.Post("").BodyJSON(l.listEntries()).Receive(nil, le)
10392
if err != nil {
10493
return err
10594
}
@@ -110,32 +99,32 @@ func (l *Log) dispatch() error {
11099
}
111100

112101
// Debug logs a message with DEBUG level.
113-
func (l *Log) Debug(format string, args ...interface{}) {
114-
l.Log(LevelDebug, format, args...)
102+
func (l *Log) Debug(fields Fields) {
103+
l.Log(LevelDebug, fields)
115104
}
116105

117106
// Info logs a message with INFO level.
118-
func (l *Log) Info(format string, args ...interface{}) {
119-
l.Log(LevelInfo, format, args...)
107+
func (l *Log) Info(fields Fields) {
108+
l.Log(LevelInfo, fields)
120109
}
121110

122111
// Warn logs a message with WARN level.
123-
func (l *Log) Warn(format string, args ...interface{}) {
124-
l.Log(LevelWarn, format, args...)
112+
func (l *Log) Warn(fields Fields) {
113+
l.Log(LevelWarn, fields)
125114
}
126115

127116
// Error logs a message with ERROR level.
128-
func (l *Log) Error(format string, args ...interface{}) {
129-
l.Log(LevelError, format, args...)
117+
func (l *Log) Error(fields Fields) {
118+
l.Log(LevelError, fields)
130119
}
131120

132121
// Fatal logs a message with FATAL level.
133-
func (l *Log) Fatal(format string, args ...interface{}) {
134-
l.Log(LevelFatal, format, args...)
122+
func (l *Log) Fatal(fields Fields) {
123+
l.Log(LevelFatal, fields)
135124
}
136125

137126
// Log logs a message with log level.
138-
func (l *Log) Log(level Level, format string, args ...interface{}) {
127+
func (l *Log) Log(level Level, fields Fields) {
139128
if level < l.Level {
140129
return
141130
}
@@ -152,16 +141,11 @@ func (l *Log) Log(level Level, format string, args ...interface{}) {
152141
}()
153142
}
154143

155-
message := fmt.Sprintf(format, args...)
156-
lm := &logEntry{
157-
Time: time.Now().Format(rfc3339Milli),
158-
AppID: l.AppID,
159-
AppName: l.AppName,
160-
Tags: l.Tags,
161-
Level: levels[level],
162-
Message: message,
163-
}
164-
l.appendLogEntry(lm)
144+
fields.Add("time", time.Now().Format(rfc3339Milli)).
145+
Add("app_id", l.AppID).
146+
Add("app_name", l.AppName).
147+
Add("level", levels[level])
148+
l.appendEntry(fields)
165149

166150
// Dispatch batch
167151
if l.logsLength() >= l.BatchSize {

store.go

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package labstack
22

33
import (
4-
"time"
4+
"fmt"
55

66
"github.com/dghubble/sling"
77
"github.com/labstack/gommon/log"
@@ -14,25 +14,12 @@ type (
1414
logger *log.Logger
1515
}
1616

17-
// StoreEntry defines the store entry
18-
StoreEntry struct {
19-
Key string `json:"key"`
20-
Value interface{} `json:"value"`
21-
CreatedAt time.Time `json:"created_at"`
22-
UpdatedAt time.Time `json:"updated_at"`
23-
}
24-
25-
// storeQueryParams defines the query parameters for find entries.
26-
storeQueryParams struct {
27-
Filters string `url:"filters"`
28-
Limit int `url:"limit"`
29-
Offset int `url:"offset"`
30-
}
17+
Document Fields
3118

32-
// StoreQueryResponse defines the query response.
33-
StoreQueryResponse struct {
34-
Total int64 `json:"total"`
35-
Entries []*StoreEntry `json:"entries"`
19+
// StoreSearchResponse defines the query response.
20+
StoreSearchResponse struct {
21+
Total int64 `json:"total"`
22+
Documents []Document `json:"documents"`
3623
}
3724

3825
// StoreError defines the store error.
@@ -42,57 +29,54 @@ type (
4229
}
4330
)
4431

45-
func (s *Store) Insert(key string, value interface{}) (*StoreEntry, error) {
46-
e := &StoreEntry{
47-
Key: key,
48-
Value: value,
49-
}
32+
func (s *Store) Insert(collection string, document Document) (Document, error) {
5033
se := new(StoreError)
51-
_, err := s.sling.Post("/store").BodyJSON(e).Receive(e, se)
34+
_, err := s.sling.Post("/store/"+collection).
35+
BodyJSON(&document).
36+
Receive(&document, se)
5237
if err != nil {
5338
return nil, err
5439
}
5540
if se.Code == 0 {
56-
return e, nil
41+
return document, nil
5742
}
5843
return nil, se
5944
}
6045

61-
func (s *Store) Get(key string) (*StoreEntry, error) {
62-
e := new(StoreEntry)
46+
func (s *Store) Get(collection, id string) (Document, error) {
47+
doc := Document{}
6348
se := new(StoreError)
64-
_, err := s.sling.Get("/store/"+key).Receive(e, se)
49+
_, err := s.sling.Get(fmt.Sprintf("/store/%s/%s", collection, id)).
50+
Receive(&doc, se)
6551
if err != nil {
6652
return nil, err
6753
}
6854
if se.Code == 0 {
69-
return e, nil
55+
return doc, nil
7056
}
7157
return nil, se
7258
}
7359

74-
func (s *Store) Query(filters string, limit, offset int) (*StoreQueryResponse, error) {
75-
qr := new(StoreQueryResponse)
60+
func (s *Store) Search(collection string, parameters *SearchParameters) (*StoreSearchResponse, error) {
61+
sr := new(StoreSearchResponse)
7662
se := new(StoreError)
77-
_, err := s.sling.Get("/store").QueryStruct(&storeQueryParams{
78-
Filters: filters,
79-
Limit: limit,
80-
Offset: offset,
81-
}).Receive(qr, se)
63+
_, err := s.sling.Post(fmt.Sprintf("/store/%s/query", collection)).
64+
BodyJSON(parameters).
65+
Receive(sr, se)
8266
if err != nil {
8367
return nil, err
8468
}
8569
if se.Code == 0 {
86-
return qr, nil
70+
return sr, nil
8771
}
8872
return nil, se
8973
}
9074

91-
func (s *Store) Update(key string, value interface{}) error {
75+
func (s *Store) Update(collection string, id string, document Document) error {
9276
se := new(StoreError)
93-
_, err := s.sling.Put("/store/"+key).BodyJSON(&StoreEntry{
94-
Value: value,
95-
}).Receive(nil, se)
77+
_, err := s.sling.Patch(fmt.Sprintf("/store/%s/%s", collection, id)).
78+
BodyJSON(&document).
79+
Receive(nil, se)
9680
if err != nil {
9781
return err
9882
}
@@ -102,9 +86,10 @@ func (s *Store) Update(key string, value interface{}) error {
10286
return se
10387
}
10488

105-
func (s *Store) Delete(key string) error {
89+
func (s *Store) Delete(collection string, id string) error {
10690
se := new(StoreError)
107-
_, err := s.sling.Delete("/store/"+key).Receive(nil, se)
91+
_, err := s.sling.Delete(fmt.Sprintf("/store/%s/%s", collection, id)).
92+
Receive(nil, se)
10893
if err != nil {
10994
return err
11095
}

0 commit comments

Comments
 (0)