This repository was archived by the owner on Mar 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tracing: refactor tracing code in transaction
Decouples tracing code from RunStepFuncLocally() func and added a middleware which implements transaction.StepManager to trace step funcs. This will provide a better trace management and clean code. Signed-off-by: Oshank Kumar <[email protected]>
- Loading branch information
Showing
7 changed files
with
129 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"go.opencensus.io/plugin/ochttp" | ||
) | ||
|
||
// Tracing is a http middleware to be use for trace incoming http.Request | ||
func Tracing(next http.Handler) http.Handler { | ||
return &ochttp.Handler{Handler: next} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package transaction | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/gluster/glusterd2/glusterd2/transaction" | ||
|
||
"go.opencensus.io/trace" | ||
) | ||
|
||
func newTracingManager(next StepManager) StepManager { | ||
return &tracingManager{next} | ||
} | ||
|
||
type tracingManager struct { | ||
next StepManager | ||
} | ||
|
||
// RunStep is a middleware which creates tracing span for step.DoFunc | ||
func (t *tracingManager) RunStep(ctx context.Context, step *transaction.Step, txnCtx transaction.TxnCtx) (err error) { | ||
spanName := fmt.Sprintf("RunStep/%s", step.DoFunc) | ||
ctx, span := trace.StartSpan(ctx, spanName) | ||
defer span.End() | ||
|
||
defer func() { | ||
attrs := []trace.Attribute{ | ||
trace.StringAttribute("reqID", txnCtx.GetTxnReqID()), | ||
} | ||
if err != nil { | ||
span.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()}) | ||
} | ||
span.AddAttributes(attrs...) | ||
}() | ||
|
||
return t.next.RunStep(ctx, step, txnCtx) | ||
} | ||
|
||
// RollBackStep is a middleware which creates tracing span for step.UndoFunc | ||
func (t *tracingManager) RollBackStep(ctx context.Context, step *transaction.Step, txnCtx transaction.TxnCtx) (err error) { | ||
spanName := fmt.Sprintf("RollBackStep/%s", step.UndoFunc) | ||
ctx, span := trace.StartSpan(ctx, spanName) | ||
defer span.End() | ||
|
||
defer func() { | ||
attrs := []trace.Attribute{ | ||
trace.StringAttribute("reqID", txnCtx.GetTxnReqID()), | ||
} | ||
if err != nil { | ||
span.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()}) | ||
} | ||
span.AddAttributes(attrs...) | ||
}() | ||
|
||
return t.next.RollBackStep(ctx, step, txnCtx) | ||
} | ||
|
||
// SyncStep is a middleware which creates tracing span for Sync steps | ||
func (t *tracingManager) SyncStep(ctx context.Context, stepIndex int, txn *Txn) (err error) { | ||
spanName := fmt.Sprintf("SyncStep/%s", txn.Steps[stepIndex].DoFunc) | ||
ctx, span := trace.StartSpan(ctx, spanName) | ||
defer span.End() | ||
|
||
defer func() { | ||
attrs := []trace.Attribute{ | ||
trace.StringAttribute("reqID", txn.Ctx.GetTxnReqID()), | ||
} | ||
if err != nil { | ||
span.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: err.Error()}) | ||
} | ||
span.AddAttributes(attrs...) | ||
}() | ||
|
||
return t.next.SyncStep(ctx, stepIndex, txn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters