github.com/blend/go-sdk@v1.20240719.1/logger/event.go (about) 1 /* 2 3 Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package logger 9 10 import ( 11 "context" 12 "io" 13 "time" 14 ) 15 16 // Event is an interface representing methods necessary to trigger listeners. 17 type Event interface { 18 GetFlag() string 19 } 20 21 // TimestampProvider is a type that provides a timestamp. 22 type TimestampProvider interface { 23 GetTimestamp() time.Time 24 } 25 26 // TextWritable is an event that can be written. 27 type TextWritable interface { 28 WriteText(TextFormatter, io.Writer) 29 } 30 31 // JSONWritable is a type that implements a decompose method. 32 // This is used by the json serializer. 33 type JSONWritable interface { 34 Decompose() map[string]interface{} 35 } 36 37 // GetEventTimestamp returns the timestamp for an event. 38 // It first checks if the event implements timestamp provider, if it does it returns that value. 39 // Then it checks if there is a timestamp on the context, if there is one it returns that value. 40 // Then it checks if there is a triggered timestamp on the context, if there is one it returns that value. 41 // Then it generates a new timestamp in utc. 42 func GetEventTimestamp(ctx context.Context, e Event) time.Time { 43 if typed, ok := e.(TimestampProvider); ok { 44 return typed.GetTimestamp() 45 } 46 if timestamp := GetTimestamp(ctx); !timestamp.IsZero() { 47 return timestamp 48 } 49 if timestamp := GetTriggerTimestamp(ctx); !timestamp.IsZero() { 50 return timestamp 51 } 52 return time.Now().UTC() 53 }