89 lines
2.1 KiB
Go
Raw Normal View History

2020-03-24 14:15:01 +01:00
package tracing
import (
"fmt"
"strconv"
"go.opencensus.io/trace"
2020-03-30 07:23:43 +02:00
2020-03-30 13:17:49 +02:00
"github.com/caos/zitadel/internal/api/grpc"
2020-03-30 07:23:43 +02:00
"github.com/caos/zitadel/internal/errors"
2020-03-24 14:15:01 +01:00
)
type Span struct {
span *trace.Span
attributes []trace.Attribute
}
func CreateSpan(span *trace.Span) *Span {
return &Span{span: span, attributes: []trace.Attribute{}}
}
func (s *Span) End() {
if s.span == nil {
return
}
s.span.AddAttributes(s.attributes...)
s.span.End()
}
func (s *Span) EndWithError(err error) {
s.SetStatusByError(err)
s.End()
}
func (s *Span) SetStatusByError(err error) {
if s.span == nil {
return
}
s.span.SetStatus(statusFromError(err))
}
func statusFromError(err error) trace.Status {
feat: localized messages (#328) * fix: project by id loads project from view and from eventstore * fix: correct search key for role * feat(auth): my user changes * fix: improve error handling in change converters * fix: log-id * feat(translations): event type translations * feat: localized translations * fix(translations): correct yaml format * chore: example * fix: remove unused code * correct checkSSL in sql * chore(modules): update * chore: refactor interceptors * fix: improvments * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/en.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/en.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/en.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/en.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/en.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/de.yaml Co-authored-by: Florian Forster <florian@caos.ch> * Update internal/static/i18n/en.yaml Co-authored-by: Florian Forster <florian@caos.ch> * chore(translations): start with upper case on Code * chore(middleware): move funcs * add message to grpc web generation * translation in mgmt and fixes * fix authoptions * fix console statik Co-authored-by: Florian Forster <florian@caos.ch> Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2020-07-08 09:48:11 +02:00
code, msg, _, _ := grpc.ExtractCaosError(err)
2020-03-30 13:17:49 +02:00
return trace.Status{Code: int32(code), Message: msg}
2020-03-24 14:15:01 +01:00
}
// AddAnnotation creates an annotation. The annotation will not be added to the tracing use Annotate(msg) afterwards
func (s *Span) AddAnnotation(key string, value interface{}) *Span {
attribute, err := toTraceAttribute(key, value)
if err != nil {
return s
}
s.attributes = append(s.attributes, attribute)
return s
}
// Annotate creates an annotation in tracing. Before added annotations will be set
func (s *Span) Annotate(message string) *Span {
if s.span == nil {
return s
}
s.span.Annotate(s.attributes, message)
s.attributes = []trace.Attribute{}
return s
}
func (s *Span) Annotatef(format string, addiations ...interface{}) *Span {
s.Annotate(fmt.Sprintf(format, addiations...))
return s
}
func toTraceAttribute(key string, value interface{}) (attr trace.Attribute, err error) {
switch value := value.(type) {
case bool:
return trace.BoolAttribute(key, value), nil
case string:
return trace.StringAttribute(key, value), nil
}
if valueInt, err := convertToInt64(value); err == nil {
return trace.Int64Attribute(key, valueInt), nil
}
2020-03-30 07:23:43 +02:00
return attr, errors.ThrowInternal(nil, "TRACE-jlq3s", "Attribute is not of type bool, string or int64")
2020-03-24 14:15:01 +01:00
}
func convertToInt64(value interface{}) (int64, error) {
valueString := fmt.Sprintf("%v", value)
return strconv.ParseInt(valueString, 10, 64)
}