-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: ground work for rich message support (#121)
* feat: add rich message API * feat: add field base tag and default field helper * format: fix naming and missing comments * docs: add missing comments for public api * test(util): add and update util tests
- Loading branch information
Showing
15 changed files
with
447 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package types | ||
|
||
import "sort" | ||
|
||
// Field is a Key/Value pair used for extra data in log messages | ||
type Field struct { | ||
Key string | ||
Value string | ||
} | ||
|
||
// FieldsFromMap creates a Fields slice from a map, optionally sorting keys | ||
func FieldsFromMap(fieldMap map[string]string, sorted bool) []Field { | ||
keys := make([]string, 0, len(fieldMap)) | ||
fields := make([]Field, 0, len(fieldMap)) | ||
|
||
for key := range fieldMap { | ||
keys = append(keys, key) | ||
} | ||
|
||
if sorted { | ||
sort.Strings(keys) | ||
} | ||
|
||
for i, key := range keys { | ||
fields[i].Key = key | ||
fields[i].Value = fieldMap[key] | ||
} | ||
|
||
return fields | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package types | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// MessageLevel is used to denote the urgency of a message item | ||
type MessageLevel uint8 | ||
|
||
const ( | ||
// Unknown is the default message level | ||
Unknown MessageLevel = iota | ||
// Debug is the lowest kind of known message level | ||
Debug | ||
// Info is generally used as the "normal" message level | ||
Info | ||
// Warning is generally used to denote messages that might be OK, but can cause problems | ||
Warning | ||
// Error is generally used for messages about things that did not go as planned | ||
Error | ||
messageLevelCount | ||
// MessageLevelCount is used to create arrays that maps levels to other values | ||
MessageLevelCount = int(messageLevelCount) | ||
) | ||
|
||
var messageLevelStrings = [MessageLevelCount]string{ | ||
"Unknown", | ||
"Debug", | ||
"Info", | ||
"Warning", | ||
"Error", | ||
} | ||
|
||
func (level MessageLevel) String() string { | ||
if level >= messageLevelCount { | ||
return messageLevelStrings[0] | ||
} | ||
return messageLevelStrings[level] | ||
} | ||
|
||
// MessageItem is an entry in a notification being sent by a service | ||
type MessageItem struct { | ||
Text string | ||
Timestamp time.Time | ||
Level MessageLevel | ||
Fields []Field | ||
} | ||
|
||
// WithField appends the key/value pair to the message items fields | ||
func (mi *MessageItem) WithField(key, value string) *MessageItem { | ||
mi.Fields = append(mi.Fields, Field{ | ||
Key: key, | ||
Value: value, | ||
}) | ||
return mi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package types | ||
|
||
// MessageLimit is used for declaring the payload limits for services upstream APIs | ||
type MessageLimit struct { | ||
ChunkSize int | ||
TotalChunkSize int | ||
|
||
// Maximum number of chunks (including the last chunk for meta data) | ||
ChunkCount int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package types | ||
|
||
// RichSender is the interface needed to implement to send rich notifications | ||
type RichSender interface { | ||
SendItems(items []MessageItem, params Params) error | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.