Merge pull request #69 from unistack-org/master

merge master
This commit is contained in:
Василий Толстов 2021-12-28 09:30:34 +03:00 committed by GitHub
commit 6e6c31b5dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 79 additions and 47 deletions

View File

@ -53,6 +53,22 @@ func (e *Error) Error() string {
return string(b) return string(b)
} }
/*
// Generator struct holds id of error
type Generator struct {
id string
}
// Generator can emit new error with static id
func NewGenerator(id string) *Generator {
return &Generator{id: id}
}
func (g *Generator) BadRequest(format string, args ...interface{}) error {
return BadRequest(g.id, format, args...)
}
*/
// New generates a custom error // New generates a custom error
func New(id, detail string, code int32) error { func New(id, detail string, code int32) error {
return &Error{ return &Error{
@ -75,121 +91,121 @@ func Parse(err string) *Error {
} }
// BadRequest generates a 400 error. // BadRequest generates a 400 error.
func BadRequest(id, format string, a ...interface{}) error { func BadRequest(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 400, Code: 400,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(400), Status: http.StatusText(400),
} }
} }
// Unauthorized generates a 401 error. // Unauthorized generates a 401 error.
func Unauthorized(id, format string, a ...interface{}) error { func Unauthorized(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 401, Code: 401,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(401), Status: http.StatusText(401),
} }
} }
// Forbidden generates a 403 error. // Forbidden generates a 403 error.
func Forbidden(id, format string, a ...interface{}) error { func Forbidden(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 403, Code: 403,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(403), Status: http.StatusText(403),
} }
} }
// NotFound generates a 404 error. // NotFound generates a 404 error.
func NotFound(id, format string, a ...interface{}) error { func NotFound(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 404, Code: 404,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(404), Status: http.StatusText(404),
} }
} }
// MethodNotAllowed generates a 405 error. // MethodNotAllowed generates a 405 error.
func MethodNotAllowed(id, format string, a ...interface{}) error { func MethodNotAllowed(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 405, Code: 405,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(405), Status: http.StatusText(405),
} }
} }
// Timeout generates a 408 error. // Timeout generates a 408 error.
func Timeout(id, format string, a ...interface{}) error { func Timeout(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 408, Code: 408,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(408), Status: http.StatusText(408),
} }
} }
// Conflict generates a 409 error. // Conflict generates a 409 error.
func Conflict(id, format string, a ...interface{}) error { func Conflict(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 409, Code: 409,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(409), Status: http.StatusText(409),
} }
} }
// InternalServerError generates a 500 error. // InternalServerError generates a 500 error.
func InternalServerError(id, format string, a ...interface{}) error { func InternalServerError(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 500, Code: 500,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(500), Status: http.StatusText(500),
} }
} }
// NotImplemented generates a 501 error // NotImplemented generates a 501 error
func NotImplemented(id, format string, a ...interface{}) error { func NotImplemented(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 501, Code: 501,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(501), Status: http.StatusText(501),
} }
} }
// BadGateway generates a 502 error // BadGateway generates a 502 error
func BadGateway(id, format string, a ...interface{}) error { func BadGateway(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 502, Code: 502,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(502), Status: http.StatusText(502),
} }
} }
// ServiceUnavailable generates a 503 error // ServiceUnavailable generates a 503 error
func ServiceUnavailable(id, format string, a ...interface{}) error { func ServiceUnavailable(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 503, Code: 503,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(503), Status: http.StatusText(503),
} }
} }
// GatewayTimeout generates a 504 error // GatewayTimeout generates a 504 error
func GatewayTimeout(id, format string, a ...interface{}) error { func GatewayTimeout(id, format string, args ...interface{}) error {
return &Error{ return &Error{
ID: id, ID: id,
Code: 504, Code: 504,
Detail: fmt.Sprintf(format, a...), Detail: fmt.Sprintf(format, args...),
Status: http.StatusText(504), Status: http.StatusText(504),
} }
} }

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.16
require ( require (
github.com/ef-ds/deque v1.0.4 github.com/ef-ds/deque v1.0.4
github.com/golang-jwt/jwt/v4 v4.1.0 github.com/golang-jwt/jwt/v4 v4.2.0
github.com/imdario/mergo v0.3.12 github.com/imdario/mergo v0.3.12
github.com/patrickmn/go-cache v2.1.0+incompatible github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4 github.com/silas/dag v0.0.0-20210626123444-3804bac2d6d4

4
go.sum
View File

@ -1,7 +1,7 @@
github.com/ef-ds/deque v1.0.4 h1:iFAZNmveMT9WERAkqLJ+oaABF9AcVQ5AjXem/hroniI= github.com/ef-ds/deque v1.0.4 h1:iFAZNmveMT9WERAkqLJ+oaABF9AcVQ5AjXem/hroniI=
github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg= github.com/ef-ds/deque v1.0.4/go.mod h1:gXDnTC3yqvBcHbq2lcExjtAcVrOnJCbMcZXmuj8Z4tg=
github.com/golang-jwt/jwt/v4 v4.1.0 h1:XUgk2Ex5veyVFVeLm0xhusUTQybEbexJXrvPNOKkSY0= github.com/golang-jwt/jwt/v4 v4.2.0 h1:besgBTC8w8HjP6NzQdxwKH9Z5oQMZ24ThTrHp3cZ8eU=
github.com/golang-jwt/jwt/v4 v4.1.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg= github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=

View File

@ -51,6 +51,4 @@ func TestExtractEndpoint(t *testing.T) {
if endpoints[0].Response != "TestResponse" { if endpoints[0].Response != "TestResponse" {
t.Fatalf("Expected TestResponse got %s", endpoints[0].Response) t.Fatalf("Expected TestResponse got %s", endpoints[0].Response)
} }
t.Logf("XXX %#+v\n", endpoints[0])
} }

View File

@ -18,11 +18,11 @@ var (
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
labels = make([]tracer.Label, 0, len(md)) labels = make([]tracer.Label, 0, len(md))
for k, v := range md { for k, v := range md {
labels = append(labels, tracer.String(k, v)) labels = append(labels, tracer.LabelString(k, v))
} }
} }
if err != nil { if err != nil {
labels = append(labels, tracer.Bool("error", true)) labels = append(labels, tracer.LabelBool("error", true))
} }
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -33,11 +33,11 @@ var (
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
labels = make([]tracer.Label, 0, len(md)) labels = make([]tracer.Label, 0, len(md))
for k, v := range md { for k, v := range md {
labels = append(labels, tracer.String(k, v)) labels = append(labels, tracer.LabelString(k, v))
} }
} }
if err != nil { if err != nil {
labels = append(labels, tracer.Bool("error", true)) labels = append(labels, tracer.LabelBool("error", true))
} }
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -48,11 +48,11 @@ var (
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
labels = make([]tracer.Label, 0, len(md)) labels = make([]tracer.Label, 0, len(md))
for k, v := range md { for k, v := range md {
labels = append(labels, tracer.String(k, v)) labels = append(labels, tracer.LabelString(k, v))
} }
} }
if err != nil { if err != nil {
labels = append(labels, tracer.Bool("error", true)) labels = append(labels, tracer.LabelBool("error", true))
} }
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -63,11 +63,11 @@ var (
if md, ok := metadata.FromIncomingContext(ctx); ok { if md, ok := metadata.FromIncomingContext(ctx); ok {
labels = make([]tracer.Label, 0, len(md)) labels = make([]tracer.Label, 0, len(md))
for k, v := range md { for k, v := range md {
labels = append(labels, tracer.String(k, v)) labels = append(labels, tracer.LabelString(k, v))
} }
} }
if err != nil { if err != nil {
labels = append(labels, tracer.Bool("error", true)) labels = append(labels, tracer.LabelBool("error", true))
} }
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -78,11 +78,11 @@ var (
if md, ok := metadata.FromIncomingContext(ctx); ok { if md, ok := metadata.FromIncomingContext(ctx); ok {
labels = make([]tracer.Label, 0, len(md)) labels = make([]tracer.Label, 0, len(md))
for k, v := range md { for k, v := range md {
labels = append(labels, tracer.String(k, v)) labels = append(labels, tracer.LabelString(k, v))
} }
} }
if err != nil { if err != nil {
labels = append(labels, tracer.Bool("error", true)) labels = append(labels, tracer.LabelBool("error", true))
} }
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -93,11 +93,11 @@ var (
if md, ok := metadata.FromOutgoingContext(ctx); ok { if md, ok := metadata.FromOutgoingContext(ctx); ok {
labels = make([]tracer.Label, 0, len(md)) labels = make([]tracer.Label, 0, len(md))
for k, v := range md { for k, v := range md {
labels = append(labels, tracer.String(k, v)) labels = append(labels, tracer.LabelString(k, v))
} }
} }
if err != nil { if err != nil {
labels = append(labels, tracer.Bool("error", true)) labels = append(labels, tracer.LabelBool("error", true))
} }
sp.SetLabels(labels...) sp.SetLabels(labels...)
} }
@ -229,7 +229,10 @@ func (ot *tWrapper) Call(ctx context.Context, req client.Request, rsp interface{
} }
} }
sp := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, endpoint)
}
defer sp.Finish() defer sp.Finish()
err := ot.Client.Call(ctx, req, rsp, opts...) err := ot.Client.Call(ctx, req, rsp, opts...)
@ -249,7 +252,10 @@ func (ot *tWrapper) Stream(ctx context.Context, req client.Request, opts ...clie
} }
} }
sp := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, endpoint)
}
defer sp.Finish() defer sp.Finish()
stream, err := ot.Client.Stream(ctx, req, opts...) stream, err := ot.Client.Stream(ctx, req, opts...)
@ -262,7 +268,10 @@ func (ot *tWrapper) Stream(ctx context.Context, req client.Request, opts ...clie
} }
func (ot *tWrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error { func (ot *tWrapper) Publish(ctx context.Context, msg client.Message, opts ...client.PublishOption) error {
sp := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, msg.Topic())
}
defer sp.Finish() defer sp.Finish()
err := ot.Client.Publish(ctx, msg, opts...) err := ot.Client.Publish(ctx, msg, opts...)
@ -282,7 +291,10 @@ func (ot *tWrapper) ServerHandler(ctx context.Context, req server.Request, rsp i
} }
} }
sp := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, fmt.Sprintf("%s.%s", req.Service(), req.Endpoint()))
}
defer sp.Finish() defer sp.Finish()
err := ot.serverHandler(ctx, req, rsp) err := ot.serverHandler(ctx, req, rsp)
@ -295,7 +307,10 @@ func (ot *tWrapper) ServerHandler(ctx context.Context, req server.Request, rsp i
} }
func (ot *tWrapper) ServerSubscriber(ctx context.Context, msg server.Message) error { func (ot *tWrapper) ServerSubscriber(ctx context.Context, msg server.Message) error {
sp := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, msg.Topic())
}
defer sp.Finish() defer sp.Finish()
err := ot.serverSubscriber(ctx, msg) err := ot.serverSubscriber(ctx, msg)
@ -339,7 +354,10 @@ func (ot *tWrapper) ClientCallFunc(ctx context.Context, addr string, req client.
} }
} }
sp := tracer.SpanFromContext(ctx) sp, ok := tracer.SpanFromContext(ctx)
if !ok {
ctx, sp = ot.opts.Tracer.Start(ctx, endpoint)
}
defer sp.Finish() defer sp.Finish()
err := ot.clientCallFunc(ctx, addr, req, rsp, opts) err := ot.clientCallFunc(ctx, addr, req, rsp, opts)