-
Notifications
You must be signed in to change notification settings - Fork 98
CloudEvents integration #404
Changes from 11 commits
cf400e5
81d60cb
51df7bd
c0bdb89
a2ce635
905fde5
03fd349
a5e7cea
e9df992
8ec5042
3279ff6
762cff0
ce49e51
4fb5991
7060f07
579655f
46f2a6d
e112c82
8363564
1625a02
38978ab
c2ded5e
88f54d7
0079e78
42ad46c
0c80f71
fd4178c
ab0a8a0
ecb4674
4400d63
a244ad5
92a8069
57a9646
d477bc0
92ec35f
c065b69
7bf1dfd
8dae643
423a226
f6e8f21
53102a4
a44628c
8e008f5
a2f5f24
bfbaeec
22a27b0
614e9c3
9b48a3e
e4984f4
f1df8b5
a5441c4
ba7969c
e4c9714
736037a
cb04faa
e025f7e
9d0aa38
00a1ca2
f1df44c
b9b2efc
afc1ba0
7de8e5f
f7242a3
f4ae257
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,23 +16,27 @@ for invoking function. By default Events API runs on `:4000` port. | |
|
||
### Event Definition | ||
|
||
All data that passes through the Event Gateway is formatted as an Event, based on our default Event schema: | ||
All data that passes through the Event Gateway is formatted as an CloudEvent, based on CloudEvent schema: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please specify spec version here. |
||
|
||
* `event` - `string` - the event name | ||
* `id` - `string` - the event's instance universally unique ID (provided by the event gateway) | ||
* `receivedAt` - `number` - the time (milliseconds) when the Event was received by the Event Gateway (provided by the event gateway) | ||
* `data` - type depends on `dataType` - the event payload | ||
* `dataType` - `string` - the mime type of `data` payload | ||
* `event-type` - `string` - the event name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove list of fields from here and leave only example and link to original spec. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want me to remove the example too or just this list of fields? |
||
* `event-id` - `string` - the event's instance universally unique ID (provided by the event gateway) | ||
* `cloud-events-version` - `string` - the version of CloudEvent definition | ||
* `source` - `string` - source for the event | ||
* `event-time` - `string` - RFC 3339 formatted time when the Event was received by the Event Gateway (provided by the event gateway) | ||
* `data` - type depends on `content-type` - the event payload | ||
* `content-type` - `string` - the mime type of `data` payload | ||
|
||
Example: | ||
|
||
```json | ||
{ | ||
"event": "myapp.user.created", | ||
"id": "66dfc31d-6844-42fd-b1a7-a489a49f65f3", | ||
"receivedAt": 1500897327098, | ||
"event-type": "myapp.user.created", | ||
"event-id": "66dfc31d-6844-42fd-b1a7-a489a49f65f3", | ||
"cloud-events-version": "0.1", | ||
"source": "", // TBD | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This need to be updated. |
||
"event-time": "1990-12-31T23:59:60Z", | ||
"data": { "foo": "bar" }, | ||
"dataType": "application/json" | ||
"content-type": "application/json" | ||
} | ||
``` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,26 +8,34 @@ import ( | |
"go.uber.org/zap/zapcore" | ||
|
||
uuid "github.com/satori/go.uuid" | ||
"github.com/serverless/event-gateway/internal/zap" | ||
) | ||
|
||
// Event is a default event structure. All data that passes through the Event Gateway is formatted as an Event, based on | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update the comment here that this struct is based on CloudEvents spec? |
||
// this schema. | ||
type Event struct { | ||
Type Type `json:"event"` | ||
ID string `json:"id"` | ||
ReceivedAt uint64 `json:"receivedAt"` | ||
Data interface{} `json:"data"` | ||
DataType string `json:"dataType"` | ||
EventType Type `json:"event-type"` | ||
EventTypeVersion string `json:"event-type-version"` | ||
CloudEventsVersion string `json:"cloud-events-version"` | ||
Source string `json:"source"` | ||
EventID string `json:"event-id"` | ||
EventTime time.Time `json:"event-time"` | ||
SchemaURL string `json:"schema-url"` | ||
ContentType string `json:"content-type"` | ||
Extensions zap.MapStringInterface `json:"extensions"` | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
// New return new instance of Event. | ||
func New(eventType Type, mime string, payload interface{}) *Event { | ||
return &Event{ | ||
Type: eventType, | ||
ID: uuid.NewV4().String(), | ||
ReceivedAt: uint64(time.Now().UnixNano() / int64(time.Millisecond)), | ||
DataType: mime, | ||
Data: payload, | ||
EventType: eventType, | ||
CloudEventsVersion: "0.1", | ||
Source: "", | ||
EventID: uuid.NewV4().String(), | ||
EventTime: time.Now(), | ||
ContentType: mime, | ||
Data: payload, | ||
} | ||
} | ||
|
||
|
@@ -42,17 +50,22 @@ const TypeHTTP = Type("http") | |
|
||
// MarshalLogObject is a part of zapcore.ObjectMarshaler interface | ||
func (e Event) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
enc.AddString("type", string(e.Type)) | ||
enc.AddString("id", e.ID) | ||
enc.AddUint64("receivedAt", e.ReceivedAt) | ||
enc.AddString("event-type", string(e.EventType)) | ||
enc.AddString("event-type-version", e.EventTypeVersion) | ||
enc.AddString("cloud-events-version", e.CloudEventsVersion) | ||
enc.AddString("source", e.Source) | ||
enc.AddString("event-id", e.EventID) | ||
enc.AddString("event-time", e.EventTime.String()) | ||
enc.AddString("schema-url", e.SchemaURL) | ||
enc.AddString("content-type", e.ContentType) | ||
e.Extensions.MarshalLogObject(enc) | ||
payload, _ := json.Marshal(e.Data) | ||
enc.AddString("data", string(payload)) | ||
enc.AddString("dataType", e.DataType) | ||
|
||
return nil | ||
} | ||
|
||
// IsSystem indicates if th event is a system event. | ||
func (e Event) IsSystem() bool { | ||
return strings.HasPrefix(string(e.Type), "gateway.") | ||
return strings.HasPrefix(string(e.EventType), "gateway.") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,33 @@ | ||
package zap | ||
|
||
import "go.uber.org/zap/zapcore" | ||
import ( | ||
"encoding/json" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// Strings is a string array that implements MarshalLogArray. | ||
type Strings []string | ||
|
||
// MapStringInterface is a map that implements MarshalLogObject. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please move it under |
||
type MapStringInterface map[string]interface{} | ||
|
||
// MarshalLogArray implementation | ||
func (ss Strings) MarshalLogArray(enc zapcore.ArrayEncoder) error { | ||
for _, s := range ss { | ||
enc.AppendString(s) | ||
} | ||
return nil | ||
} | ||
|
||
// MarshalLogObject implementation | ||
func (msi MapStringInterface) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
for key, val := range msi { | ||
v, err := json.Marshal(val) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need json.Marshal here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not casting to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because It can be a nested object or an array. |
||
if err != nil { | ||
enc.AddString(key, string(v)) | ||
} else { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"as a CloudEvent"