Skip to content

Commit

Permalink
Merge pull request #46 from dongxuny/master
Browse files Browse the repository at this point in the history
[cursor] Add parent as prometheus label for trace tracking
  • Loading branch information
dongxuny authored Mar 15, 2022
2 parents 6032590 + 3de52db commit cbda3a7
Showing 1 changed file with 43 additions and 8 deletions.
51 changes: 43 additions & 8 deletions cursor/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func init() {
"instance",
"appVersion",
"appName",
"parent",
"operation",
"status",
},
Expand Down Expand Up @@ -89,11 +90,12 @@ func SummaryVec() *prometheus.SummaryVec {
func Click() *pointer {
return &pointer{
start: time.Now(),
operation: funcName(),
parent: parentName(),
operation: operationName(),
}
}

func LogError(err error) {
func Error(err error) {
if err == nil {
return
}
Expand Down Expand Up @@ -169,17 +171,24 @@ type Cursor struct {
}

func (c *Cursor) Click() *pointer {
operation := operationName()

if c.Event != nil {
c.Event.StartTimer(operation)
}

return &pointer{
entryName: c.entryName,
entryType: c.entryType,
start: time.Now(),
operation: funcName(),
operation: operation,
parent: parentName(),
logger: c.Logger,
event: c.Event,
}
}

func (c *Cursor) LogError(err error) {
func (c *Cursor) Error(err error) {
if err == nil {
return
}
Expand Down Expand Up @@ -212,7 +221,7 @@ type promLabel struct {
values []string
}

func (l *promLabel) getValues(op string, entryName, entryType string, err error) []string {
func (l *promLabel) getValues(parent, op, entryName, entryType string, err error) []string {
label.mutex.Lock()
defer label.mutex.Unlock()

Expand All @@ -221,7 +230,7 @@ func (l *promLabel) getValues(op string, entryName, entryType string, err error)
status = "ERROR"
}

res := append(l.values, op, status)
res := append(l.values, parent, op, status)
res[0] = entryName
res[1] = entryType

Expand All @@ -232,6 +241,7 @@ func (l *promLabel) getValues(op string, entryName, entryType string, err error)

type pointer struct {
start time.Time
parent string
operation string
err error
event rkquery.Event
Expand Down Expand Up @@ -275,17 +285,22 @@ func (c *pointer) ObserveError(err error) error {
func (c *pointer) Release() {
elapsedNano := time.Now().Sub(c.start).Nanoseconds()

observer, _ := summaryVec.GetMetricWithLabelValues(label.getValues(c.operation, c.entryName, c.entryType, c.err)...)
observer, _ := summaryVec.GetMetricWithLabelValues(
label.getValues(c.parent, c.operation, c.entryName, c.entryType, c.err)...)
if observer == nil {
return
}

if c.event != nil {
c.event.EndTimer(c.operation)
}

observer.Observe(float64(elapsedNano))
}

// ************* helper functions *************

func funcName() string {
func operationName() string {
pc, _, _, ok := runtime.Caller(2)
if !ok {
return "unknown"
Expand All @@ -301,6 +316,26 @@ func funcName() string {
return fName
}

func parentName() string {
pc, file, _, ok := runtime.Caller(3)
if !ok {
return "-"
}

fName := runtime.FuncForPC(pc).Name()
if strings.Contains(file, "@") {
return "-"
}

// 1: try to check whether it is nested, trim prefix of file path
fName = fName[strings.LastIndex(fName, "/")+1:]
fName = strings.ReplaceAll(fName, "(", "")
fName = strings.ReplaceAll(fName, ")", "")
fName = strings.ReplaceAll(fName, "*", "")

return fName
}

func stacks() []uintptr {
const depth = 32
var pcs [depth]uintptr
Expand Down

0 comments on commit cbda3a7

Please sign in to comment.