Beispiel #1
0
func (q SqlQuery) logQuery(ctx context.Context, query string, args []interface{}) {
	log.
		Ctx(ctx).
		WithField("args", args).
		WithField("sql", query).
		Debug("query sql")
}
Beispiel #2
0
func logEndOfRequest(ctx context.Context, duration time.Duration, mw mutil.WriterProxy) {
	log.Ctx(ctx).WithFields(log.F{
		"status":   mw.Status(),
		"bytes":    mw.BytesWritten(),
		"duration": duration,
	}).Info("Finished request")
}
Beispiel #3
0
// WriteEvent does the actual work of formatting an SSE compliant message
// sending it over the provided ResponseWriter and flushing.
func WriteEvent(ctx context.Context, w http.ResponseWriter, e Event) {
	if e.Error != nil {
		fmt.Fprint(w, "event: err\n")
		fmt.Fprintf(w, "data: %s\n\n", e.Error.Error())
		w.(http.Flusher).Flush()
		log.Ctx(ctx).Error(e.Error)
		return
	}

	// TODO: add tests to ensure retry get's properly rendered
	if e.Retry != 0 {
		fmt.Fprintf(w, "retry: %d\n", e.Retry)
	}

	if e.ID != "" {
		fmt.Fprintf(w, "id: %s\n", e.ID)
	}

	if e.Event != "" {
		fmt.Fprintf(w, "event: %s\n", e.Event)
	}

	fmt.Fprintf(w, "data: %s\n\n", getJSON(e.Data))
	w.(http.Flusher).Flush()
}
Beispiel #4
0
func logStartOfRequest(ctx context.Context, r *http.Request) {
	log.Ctx(ctx).WithFields(log.F{
		"path":   r.URL.String(),
		"method": r.Method,
		"ip":     r.RemoteAddr,
		"host":   r.Host,
	}).Info("Starting request")
}
Beispiel #5
0
func (r *Repo) log(typ string, start time.Time, query string, args []interface{}) {
	log.
		Ctx(r.logCtx()).
		WithField("args", args).
		WithField("sql", query).
		WithField("dur", time.Since(start).String()).
		Debugf("sql: %s", typ)
}
Beispiel #6
0
// Prepare sets the action's App field based upon the goji context
func (action *Action) Prepare(c web.C, w http.ResponseWriter, r *http.Request) {
	base := &action.Base
	base.Prepare(c, w, r)
	action.App = action.GojiCtx.Env["app"].(*App)

	if action.Ctx != nil {
		action.Log = log.Ctx(action.Ctx)
	} else {
		action.Log = log.DefaultLogger
	}
}
Beispiel #7
0
// Tick triggers the system to update itself with any new data available.
func (sys *System) Tick(ctx context.Context) {
	sys.Init()
	logger := log.Ctx(ctx)

	logger.
		WithField("queued", sys.SubmissionQueue.String()).
		Debug("ticking txsub system")

	addys := sys.SubmissionQueue.Addresses()
	if len(addys) > 0 {
		curSeq, err := sys.Sequences.Get(addys)
		if err != nil {
			logger.WithStack(err).Error(err)
		} else {
			sys.SubmissionQueue.Update(curSeq)
		}
	}

	for _, hash := range sys.Pending.Pending(ctx) {
		r := sys.Results.ResultByHash(ctx, hash)

		if r.Err == nil {
			logger.WithField("hash", hash).Debug("finishing open submission")
			sys.Pending.Finish(ctx, r)
			continue
		}

		_, ok := r.Err.(*FailedTransactionError)

		if ok {
			logger.WithField("hash", hash).Debug("finishing open submission")
			sys.Pending.Finish(ctx, r)
			continue
		}

		if r.Err != ErrNoResults {
			logger.WithStack(r.Err).Error(r.Err)
		}
	}

	stillOpen, err := sys.Pending.Clean(ctx, sys.SubmissionTimeout)
	if err != nil {
		logger.WithStack(err).Error(err)
	}

	sys.Metrics.OpenSubmissionsGauge.Update(int64(stillOpen))
	sys.Metrics.BufferedSubmissionsGauge.Update(int64(sys.SubmissionQueue.Size()))
}
Beispiel #8
0
func render(ctx context.Context, w http.ResponseWriter, p P) {

	Inflate(ctx, &p)

	w.Header().Set("Content-Type", "application/problem+json; charset=utf-8")
	js, err := json.MarshalIndent(p, "", "  ")

	if err != nil {
		err := errors.Wrap(err, 1)
		log.Ctx(ctx).WithStack(err).Error(err)
		http.Error(w, "error rendering problem", http.StatusInternalServerError)
		return
	}

	w.WriteHeader(p.Status)
	w.Write(js)
}
Beispiel #9
0
// Negotiate inspects the Accept header of the provided request and determines
// what the most appropriate response type should be.  Defaults to HAL.
func Negotiate(ctx context.Context, r *http.Request) string {
	alternatives := []string{MimeHal, MimeJSON, MimeEventStream, MimeRaw}
	accept := r.Header.Get("Accept")

	if accept == "" {
		return MimeHal
	}

	result := goautoneg.Negotiate(r.Header.Get("Accept"), alternatives)

	log.Ctx(ctx).WithFields(log.F{
		"content_type": result,
		"accept":       accept,
	}).Debug("Negotiated content type")

	return result
}
Beispiel #10
0
func renderErr(ctx context.Context, w http.ResponseWriter, err error) {
	origErr := err

	if err, ok := err.(*errors.Error); ok {
		origErr = err.Err
	}

	p, ok := errToProblemMap[origErr]

	// If this error is not a registered error
	// log it and replace it with a 500 error
	if !ok {
		log.Ctx(ctx).WithStack(err).Error(err)
		p = ServerError
	}

	render(ctx, w, p)
}
Beispiel #11
0
func (r *Repo) logRollback() {
	log.Ctx(r.logCtx()).Debug("sql: rollbacl")
}
Beispiel #12
0
func (r *Repo) logCommit() {
	log.Ctx(r.logCtx()).Debug("sql: commit")
}
Beispiel #13
0
func (r *Repo) logBegin() {
	log.Ctx(r.logCtx()).Debug("sql: begin")
}