Documentation ¶
Index ¶
- Constants
- func AddRequestId(t any) http.Header
- func EnvStr() string
- func FmtRFC3339Millis(t time.Time) string
- func HttpCode(code int) int
- func HttpStatus(code int) string
- func IsDebugEnvironment() bool
- func IsProdEnvironment() bool
- func IsStageEnvironment() bool
- func IsTestEnvironment() bool
- func NewInvalidBodyTypeError(t any) error
- func NewRequestIdContext(ctx context.Context, requestId string) context.Context
- func NewRequestIdContextFromHeader(h http.Header) context.Context
- func OutputFormatter(ts time.Time, code int, status, requestId string, errs []error, trace []string) string
- func ParseRFC3339Millis(ts string) (time.Time, error)
- func ParseTimestamp2(s string) (time.Time, error)
- func RequestId(t any) string
- func RequestIdFromContext(ctx any) string
- func SetErrorFormatter(fn Formatter)
- func SetErrorLogger(fn Logger)
- func SetOutputFormatter()
- func SetProdEnvironment()
- func SetStageEnvironment()
- func SetTestEnvironment()
- type Attr
- type Attributable
- type Bypass
- type ErrorHandler
- type Formatter
- type Log
- type Logger
- type Nillable
- type Output
- type Status
Examples ¶
Constants ¶
const ( TimestampName = "timestamp" StatusName = "status" CodeName = "code" TraceName = "trace" RequestIdName = "request-id" ErrorsName = "errors" )
const ( XRequestId = "x-request-id" XRelatesTo = "x-relates-to" )
const ( StatusInvalidContent = int(90) // Content is not available, is nil, or is of the wrong type, usually found via unmarshalling StatusIOError = int(91) // I/O operation failed StatusJsonDecodeError = int(92) // Json decoding failed StatusJsonEncodeError = int(93) // Json encoding failed StatusContentEncodingError = int(94) // Content encoding error StatusNotProvided = int(95) // No status is available StatusRateLimited = int(96) // Rate limited StatusNotStarted = int(97) // Not started StatusHaveContent = int(98) // Content is available StatusGzipEncodingError = int(99) // Gzip encoding error StatusGzipDecodingError = int(100) // Gzip decoding error StatusInvalidArgument = 3 //codes.InvalidArgument // The client specified an invalid argument. Note that this differs from FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments that are problematic regardless of the state of the system (e.g., a malformed file name). StatusDeadlineExceeded = 4 //codes.DeadlineExceeded // The deadline expired before the operation could complete. For operations that change the state of the system, this error may be returned even if the operation has completed successfully. For example, a successful response from a server could have been delayed long )
const (
EnvKey = "RUNTIME_ENV"
)
const (
PkgPath = "github/advanced-go/core/runtime"
)
Variables ¶
This section is empty.
Functions ¶
func AddRequestId ¶
AddRequestId - add a request to an http.Request or an http.Header
Example ¶
func ExampleRequestId_New() { id := RequestId(nil) fmt.Printf("test: RequestId(nil) -> [empty:%v]\n", len(id) == 0) id = RequestId(context.Background()) fmt.Printf("test: RequestId(ctx) -> [empty:%v]\n", len(id) == 0) req, _ := http.NewRequest("", "https.www.google.com", nil) id = RequestId(req) fmt.Printf("test: RequestId(request) -> [empty:%v]\n", len(id) == 0) h := make(http.Header) id = RequestId(h) fmt.Printf("test: RequestId(header) -> [empty:%v]\n", len(id) == 0) //Output: //test: RequestId(nil) -> [empty:false] //test: RequestId(ctx) -> [empty:false] //test: RequestId(request) -> [empty:false] //test: RequestId(header) -> [empty:false]
}
h := AddRequestId(nil) fmt.Printf("test: AddRequestId(nil) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0) head := make(http.Header) h = AddRequestId(head) fmt.Printf("test: AddRequestId(head) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0) head = make(http.Header) head.Add(XRequestId, "123-45-head") h = AddRequestId(head) fmt.Printf("test: AddRequestId(head) -> %v\n", h.Get(XRequestId)) req, _ := http.NewRequest("", "https.www.google.com", nil) h = AddRequestId(req) fmt.Printf("test: RequestId(request) -> [empty:%v]\n", len(h.Get(XRequestId)) == 0) req, _ = http.NewRequest("", "https.www.google.com", nil) req.Header.Set(XRequestId, "123-456-request") h = AddRequestId(req) fmt.Printf("test: RequestId(request) -> %v\n", h.Get(XRequestId))
Output: test: AddRequestId(nil) -> [empty:false] test: AddRequestId(head) -> [empty:false] test: AddRequestId(head) -> 123-45-head test: RequestId(request) -> [empty:false] test: RequestId(request) -> 123-456-request
func FmtRFC3339Millis ¶
FmtRFC3339Millis - format time https://datatracker.ietf.org/doc/html/rfc3339
func HttpStatus ¶
HttpStatus - string representation of status code
func IsDebugEnvironment ¶
func IsDebugEnvironment() bool
IsDebugEnvironment - determine if debug environment
func IsProdEnvironment ¶
func IsProdEnvironment() bool
IsProdEnvironment - determine if production environment
func IsStageEnvironment ¶
func IsStageEnvironment() bool
IsStageEnvironment - determine if staging environment
func IsTestEnvironment ¶
func IsTestEnvironment() bool
IsTestEnvironment - determine if test environment
func NewInvalidBodyTypeError ¶
NewInvalidBodyTypeError - invalid type error
func NewRequestIdContext ¶
NewRequestIdContext - creates a new Context with a request id
func NewRequestIdContextFromHeader ¶
NewRequestIdContextFromHeader - creates a new Context with a request id from the request headers
func OutputFormatter ¶
func OutputFormatter(ts time.Time, code int, status, requestId string, errs []error, trace []string) string
OutputFormatter - formatter for special output formatting
func ParseRFC3339Millis ¶
ParseRFC3339Millis - parse a string into a time.Time, using the following string : 2023-04-14T14:14:45.522Z
func ParseTimestamp2 ¶
ParseTimestamp2 - parse a string into a time.Time, using the following string : 2023-04-14 14:14:45.522460
func RequestId ¶
RequestId - return a request id from any type and will create a new one if not found
Example ¶
id := RequestId("123-456-string") fmt.Printf("test: RequestId(string) -> %v\n", id) ctx := NewRequestIdContext(context.Background(), "123-456-context") id = RequestId(ctx) fmt.Printf("test: RequestId(ctx) -> %v\n", id) req, _ := http.NewRequest("", "https.www.google.com", nil) req.Header.Set(XRequestId, "123-456-request") id = RequestId(req) fmt.Printf("test: RequestId(request) -> %v\n", id) h := make(http.Header) h.Set(XRequestId, "123-456-header") id = RequestId(h) fmt.Printf("test: RequestId(header) -> %v\n", id)
Output: test: RequestId(string) -> 123-456-string test: RequestId(ctx) -> 123-456-context test: RequestId(request) -> 123-456-request test: RequestId(header) -> 123-456-header
func RequestIdFromContext ¶
RequestIdFromContext - return the requestId from a context
func SetErrorFormatter ¶
func SetErrorFormatter(fn Formatter)
SetErrorFormatter - optional override of error formatting
func SetOutputFormatter ¶
func SetOutputFormatter()
SetOutputFormatter - optional override of output formatting
Types ¶
type Attr ¶
func Attributes ¶
type Attributable ¶
type Attributable interface {
Attributes() []Attr
}
type ErrorHandler ¶
ErrorHandler - error handler interface
type Formatter ¶
type Formatter func(ts time.Time, code int, status, requestId string, errs []error, trace []string) string
Formatter - output formatting type
type Status ¶
type Status struct { Code int `json:"code"` // contains filtered or unexported fields }