-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.md.tpl
129 lines (110 loc) · 3.59 KB
/
README.md.tpl
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
# Jettison
[![Go Report Card](https://goreportcard.com/badge/github.com/luno/jettison?style=flat-square)](https://goreportcard.com/report/github.com/luno/jettison)
[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/luno/jettison)
## What is it?
Jettison is a library providing structured logs and errors in a way that's
interoperable with gRPC. It does this under the hood by serialising message
details to protobuf messages and attaching them to gRPC `Status` objects -
see [the gRPC docs](https://godoc.org/google.golang.org/grpc/status) for
details of how this works. Jettison is also compatible with the Go 2 error
spec, which can be found [in the draft design page](https://go.googlesource.com/proposal/+/master/design/go2draft.md).
## Features
Jettison is in alpha, but the following features are planned:
* [✓] Simple, gRPC-compatible utilities for building up structured errors/logs.
* [✓] Support for error identification and unwrapping as per the Go 2 spec.
* [✓] Structured error/log formatting utilities for both machines (JSON) and
humans.
* [✕] Key/value pair type support for compatibility with Elasticsearch.
## API
### Errors
The `jettison/errors` package provides functions for creating and working with
gRPC-compatible error values. You can attach arbitrary metadata to jettison
errors, decorate them with source/stacktrace information and wrap errors to
form a chain as the stack unwinds. Passing jettison errors over gRPC preserves
all metadata structure, in contrast to other error types (which get marshalled
to a string by default).
Jettison also provides gRPC middleware that automatically groups the errors
in a chain by the gRPC server (or "hop") they originated from.
See the `jettison/example` package for a more complete usage example, including
a gRPC server/client passing jettison errors over the wire.
```GO
import (
"github.com/luno/jettison"
"github.com/luno/jettison/errors"
)
func ExampleNew() {{ "ExampleNew" | code }}
```
### Logs
The `jettison/log` package provides structured JSON logging, with additional
utilities for logging jettison errors. You can attach metadata to logs in the
same manner as you attach metadata to errors.
```GO
import (
"context"
"github.com/luno/jettison"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/log"
)
func ExampleInfo() {{ "ExampleInfo" | code }}
func ExampleError() {{ "ExampleError" | code }}
```
An example log written via `log.Info`:
```JSON
{
"message": "entering the example function",
"source": "jettison/example/example.go:9",
"level": "info",
"parameters": [
{
"key": "key",
"value": "value"
}
]
}
```
An example log written via `log.Error`:
```JSON
{
"message": "a jettison error",
"source": "jettison/example/example.go:18",
"level": "error",
"hops": [
{
"binary": "example",
"errors": [
{
"message": "a jettison error",
"source": "jettison/example/example.go:14",
"parameters": [
{
"key": "key",
"value": "value"
}
]
}
]
}
],
"parameters": [
{
"key": "key",
"value": "value"
},
{
"key": "another_key",
"value": "another_value"
}
]
}
```
### Utilities
The `jettison/j` package contains aliases for common jettison options,
saving you a couple of keystrokes:
```GO
import (
"github.com/luno/jettison/errors"
"github.com/luno/jettison/j"
)
func ExampleKS() {{ "ExampleKS" | code }}
func ExampleKV() {{ "ExampleKV" | code }}
```