func addUserTableQueryStats(queryServiceStats *QueryServiceStats, ctx context.Context, tableName string, queryType string, duration int64) { username := callerid.GetPrincipal(callerid.EffectiveCallerIDFromContext(ctx)) if username == "" { username = callerid.GetUsername(callerid.ImmediateCallerIDFromContext(ctx)) } queryServiceStats.UserTableQueryCount.Add([]string{tableName, username, queryType}, 1) queryServiceStats.UserTableQueryTimesNs.Add([]string{tableName, username, queryType}, int64(duration)) }
func (c *errorClient) Rollback(ctx context.Context, session *vtgatepb.Session) error { // The client sends the error request through the callerid, as there are no other parameters cid := callerid.EffectiveCallerIDFromContext(ctx) request := callerid.GetPrincipal(cid) if err := requestToError(request); err != nil { return err } return c.fallbackClient.Rollback(ctx, session) }
// Format returns a printable version of the connection info. func (txc *TxConnection) Format(params url.Values) string { return fmt.Sprintf( "%v\t'%v'\t'%v'\t%v\t%v\t%.6f\t%v\t%v\t\n", txc.TransactionID, callerid.GetPrincipal(txc.EffectiveCallerID), callerid.GetUsername(txc.ImmediateCallerID), txc.StartTime.Format(time.StampMicro), txc.EndTime.Format(time.StampMicro), txc.EndTime.Sub(txc.StartTime).Seconds(), txc.Conclusion, strings.Join(txc.Queries, ";"), ) }
func (txc *TxConnection) log(conclusion string) { txc.Conclusion = conclusion txc.EndTime = time.Now() username := callerid.GetPrincipal(txc.EffectiveCallerID) if username == "" { username = callerid.GetUsername(txc.ImmediateCallerID) } duration := txc.EndTime.Sub(txc.StartTime) txc.pool.queryServiceStats.UserTransactionCount.Add([]string{username, conclusion}, 1) txc.pool.queryServiceStats.UserTransactionTimesNs.Add([]string{username, conclusion}, int64(duration)) if txc.LogToFile.Get() != 0 { log.Infof("Logged transaction: %s", txc.Format(nil)) } TxLogger.Send(txc) }
func (txc *TxConnection) discard(conclusion string) { txc.Conclusion = conclusion txc.EndTime = time.Now() username := callerid.GetPrincipal(txc.EffectiveCallerID) if username == "" { username = callerid.GetUsername(txc.ImmediateCallerID) } duration := txc.EndTime.Sub(txc.StartTime) txc.pool.queryServiceStats.UserTransactionCount.Add([]string{username, conclusion}, 1) txc.pool.queryServiceStats.UserTransactionTimesNs.Add([]string{username, conclusion}, int64(duration)) txc.pool.activePool.Unregister(txc.TransactionID) txc.DBConn.Recycle() // Ensure PoolConnection won't be accessed after Recycle. txc.DBConn = nil if txc.LogToFile.Get() != 0 { log.Infof("Logged transaction: %s", txc.Format(nil)) } TxLogger.Send(txc) }
// RunTests performs the necessary testsuite for CallerID operations func RunTests(t *testing.T, im *querypb.VTGateCallerID, ef *vtrpcpb.CallerID, newContext func(context.Context, *vtrpcpb.CallerID, *querypb.VTGateCallerID) context.Context) { ctx := context.TODO() ctxim := callerid.ImmediateCallerIDFromContext(ctx) // For Contexts without immediate CallerID, ImmediateCallerIDFromContext should fail if ctxim != nil { t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim) } // For Contexts without effective CallerID, EffectiveCallerIDFromContext should fail ctxef := callerid.EffectiveCallerIDFromContext(ctx) if ctxef != nil { t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef) } ctx = newContext(ctx, nil, nil) ctxim = callerid.ImmediateCallerIDFromContext(ctx) // For Contexts with nil immediate CallerID, ImmediateCallerIDFromContext should fail if ctxim != nil { t.Errorf("Expect nil from ImmediateCallerIDFromContext, but got %v", ctxim) } // For Contexts with nil effective CallerID, EffectiveCallerIDFromContext should fail ctxef = callerid.EffectiveCallerIDFromContext(ctx) if ctxef != nil { t.Errorf("Expect nil from EffectiveCallerIDFromContext, but got %v", ctxef) } // Test GetXxx on nil receivers, should get all empty strings if u := callerid.GetUsername(ctxim); u != "" { t.Errorf("Expect empty string from GetUsername(nil), but got %v", u) } if p := callerid.GetPrincipal(ctxef); p != "" { t.Errorf("Expect empty string from GetPrincipal(nil), but got %v", p) } if c := callerid.GetComponent(ctxef); c != "" { t.Errorf("Expect empty string from GetComponent(nil), but got %v", c) } if s := callerid.GetSubcomponent(ctxef); s != "" { t.Errorf("Expect empty string from GetSubcomponent(nil), but got %v", s) } ctx = newContext(ctx, ef, im) ctxim = callerid.ImmediateCallerIDFromContext(ctx) // retrieved immediate CallerID should be equal to the one we put into Context if !reflect.DeepEqual(ctxim, im) { t.Errorf("Expect %v from ImmediateCallerIDFromContext, but got %v", im, ctxim) } if u := callerid.GetUsername(im); u != FakeUsername { t.Errorf("Expect %v from GetUsername(im), but got %v", FakeUsername, u) } ctxef = callerid.EffectiveCallerIDFromContext(ctx) // retrieved effective CallerID should be equal to the one we put into Context if !reflect.DeepEqual(ctxef, ef) { t.Errorf("Expect %v from EffectiveCallerIDFromContext, but got %v", ef, ctxef) } if p := callerid.GetPrincipal(ef); p != FakePrincipal { t.Errorf("Expect %v from GetPrincipal(ef), but got %v", FakePrincipal, p) } if c := callerid.GetComponent(ef); c != FakeComponent { t.Errorf("Expect %v from GetComponent(ef), but got %v", FakeComponent, c) } if s := callerid.GetSubcomponent(ef); s != FakeSubcomponent { t.Errorf("Expect %v from GetSubcomponent(ef), but got %v", FakeSubcomponent, s) } }
<thead> <tr> <th>Transaction id</th> <th>Effective caller</th> <th>Immediate caller</th> <th>Start</th> <th>End</th> <th>Duration</th> <th>Decision</th> <th>Statements</th> </tr> </thead> `) txlogzFuncMap = template.FuncMap{ "stampMicro": func(t time.Time) string { return t.Format(time.StampMicro) }, "getEffectiveCaller": func(e *vtrpcpb.CallerID) string { return callerid.GetPrincipal(e) }, "getImmediateCaller": func(i *querypb.VTGateCallerID) string { return callerid.GetUsername(i) }, } txlogzTmpl = template.Must(template.New("example").Funcs(txlogzFuncMap).Parse(` <tr class="{{.ColorLevel}}"> <td>{{.TransactionID}}</td> <td>{{.EffectiveCallerID | getEffectiveCaller}}</td> <td>{{.ImmediateCallerID | getImmediateCaller}}</td> <td>{{.StartTime | stampMicro}}</td> <td>{{.EndTime | stampMicro}}</td> <td>{{.Duration}}</td> <td>{{.Conclusion}}</td> <td> {{ range .Queries }} {{.}}<br> {{ end}}
// EffectiveCaller returns the effective caller stored in LogStats.ctx func (stats *LogStats) EffectiveCaller() string { return callerid.GetPrincipal(callerid.EffectiveCallerIDFromContext(stats.ctx)) }