Ejemplo n.º 1
0
// Build builds the image.
func (b *Builder) Build(ctx context.Context, w io.Writer, opts builder.BuildOptions) (image string, err error) {
	log.Printf("Starting build: id=%s repository=%s branch=%s sha=%s",
		opts.ID,
		opts.Repository,
		opts.Branch,
		opts.Sha,
	)

	// Embed the reporter in the context.Context.
	ctx = reporter.WithReporter(ctx, b.reporter())

	if b.Timeout != 0 {
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(ctx, b.Timeout)
		defer cancel() // Release resources.
	}

	reporter.AddContext(ctx, "options", opts)
	defer reporter.Monitor(ctx)

	defer func() {
		if err != nil {
			reporter.Report(ctx, err)
		}
	}()

	image, err = b.builder.Build(ctx, w, opts)
	return
}
Ejemplo n.º 2
0
// newContext builds a new base Context object.
func newContext(c *cli.Context) (ctx *Context, err error) {
	ctx = &Context{
		Context: c,
		netCtx:  context.Background(),
	}

	ctx.reporter, err = newReporter(ctx)
	if err != nil {
		return
	}

	ctx.logger, err = newLogger(ctx)
	if err != nil {
		return
	}

	ctx.stats, err = newStats(ctx)
	if err != nil {
		return
	}

	if ctx.reporter != nil {
		ctx.netCtx = reporter.WithReporter(ctx.netCtx, ctx.reporter)
	}
	if ctx.logger != nil {
		ctx.netCtx = logger.WithLogger(ctx.netCtx, ctx.logger)
	}
	if ctx.stats != nil {
		ctx.netCtx = stats.WithStats(ctx.netCtx, ctx.stats)
	}

	return
}
Ejemplo n.º 3
0
// ServeHTTPContext implements the httpx.Handler interface. It recovers from
// panics and returns an error for upstream middleware to handle.
func (h *Recovery) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) (err error) {
	ctx = reporter.WithReporter(ctx, h.Reporter)

	// Add the request to the context.
	reporter.AddRequest(ctx, r)

	// Add the request id
	reporter.AddContext(ctx, "request_id", httpx.RequestID(ctx))

	defer func() {
		if v := recover(); v != nil {
			err = fmt.Errorf("%v", v)

			if v, ok := v.(error); ok {
				err = v
			}

			reporter.Report(ctx, err)

			return
		}
	}()

	err = h.handler.ServeHTTPContext(ctx, w, r)

	return
}
Ejemplo n.º 4
0
func Example() {
	ctx := reporter.WithReporter(context.Background(), hb.NewReporter("dcb8affa"))
	req, _ := http.NewRequest("GET", "/api/foo", nil)
	req.Header.Set("Content-Type", "application/json")

	reporter.AddContext(ctx, "request_id", "1234")
	reporter.AddRequest(ctx, req)
	reporter.Report(ctx, errBoom)
	// Output:
}
Ejemplo n.º 5
0
// newRootContext returns a new root context.Context with an error reporter and
// logger embedded in the context.
func newRootContext(c *cli.Context) (context.Context, error) {
	r, err := newReporter(c)
	if err != nil {
		return nil, err
	}
	l := newLogger()

	ctx := context.Background()
	if r != nil {
		ctx = reporter.WithReporter(ctx, r)
	}
	if l != nil {
		ctx = logger.WithLogger(ctx, l)
	}

	return ctx, nil
}
Ejemplo n.º 6
0
// newTestServer returns a new httptest.Server for testing empire's http server.
func newTestServer(t testing.TB, e *empire.Empire, opts server.Options) *Server {
	if e == nil {
		e = NewEmpire(t)
	}

	// Log reporter errors to stderr
	ctx := reporter.WithReporter(context.Background(), reporter.ReporterFunc(func(ctx context.Context, err error) error {
		fmt.Fprintf(os.Stderr, "reported error: %v\n", err)
		return nil
	}))

	s := server.New(e, opts)
	svr := httptest.NewUnstartedServer(middleware.Handler(ctx, s))
	u, _ := url.Parse(svr.URL)
	s.URL = u
	return &Server{
		Empire: e,
		Server: s,
		svr:    svr,
	}
}