Example #1
0
func (db *DB) BeginC(ctx context.Context) (*Tx, error) {
	span, _ := ot.StartSpanFromContext(ctx, "BeginC")
	defer span.Finish()

	type txAndError struct {
		tx  *sql.Tx
		err error
	}
	result := make(chan txAndError, 1)

	go func() {
		tx, err := db.Begin()
		result <- txAndError{tx, err}
	}()

	select {
	case <-ctx.Done():
		go func() {
			if r := <-result; r.tx != nil {
				r.tx.Rollback()
			}
		}()
		return nil, ctx.Err()
	case r := <-result:
		return &Tx{r.tx}, r.err
	}
}
Example #2
0
func foo(ctx context.Context, name string, count int) {
	span, nctx := ot.StartSpanFromContext(ctx, fmt.Sprintf("%s%d", name, count))
	defer span.Finish()
	if count == 3 {
		return
	}
	foo(nctx, name, count+1)
}
Example #3
0
func (db *DB) PingC(ctx context.Context) error {
	span, _ := ot.StartSpanFromContext(ctx, "Ping")
	defer span.Finish()

	result := make(chan error, 1)

	go func() {
		result <- db.Ping()
	}()

	select {
	case <-ctx.Done():
		return ctx.Err()
	case err := <-result:
		return err
	}
}
Example #4
0
func TestTracer(t *testing.T) {
	log.SetFlags(0)

	var tracer ot.Tracer
	tracer = &Tracer{}
	ot.InitGlobalTracer(tracer)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		serverSpan, err := tracer.Join(
			"serverSpan",
			ot.TextMap,
			ot.HTTPHeaderTextMapCarrier(r.Header))
		if err != nil {
			panic(err)
		}
		time.Sleep(time.Second * 1)
		serverSpan.Finish()
		fmt.Fprintln(w, "Hello, client")
	}))
	defer ts.Close()

	span, nctx := ot.StartSpanFromContext(context.TODO(), "main_test")
	defer span.Finish()

	foo(nctx, "bar", 0)

	httpClient := &http.Client{}
	httpReq, _ := http.NewRequest("GET", ts.URL, nil)

	// Transmit the span's TraceContext as HTTP headers on our
	// outbound request.
	tracer.Inject(
		span,
		ot.TextMap,
		ot.HTTPHeaderTextMapCarrier(httpReq.Header))

	if _, err := httpClient.Do(httpReq); err != nil {
		panic(err)
	}
}
Example #5
0
func (tx *Tx) QueryRowC(ctx context.Context, query string, args ...interface{}) *Row {
	span, nctx := ot.StartSpanFromContext(ctx, "QueryRowC")
	defer span.Finish()
	return queryrow(nctx, func() *sql.Row { return tx.QueryRow(query, args...) })
}
Example #6
0
func (s *Stmt) QueryRowC(ctx context.Context, args ...interface{}) *Row {
	span, nctx := ot.StartSpanFromContext(ctx, "QueryRowC")
	defer span.Finish()
	return queryrow(nctx, func() *sql.Row { return s.QueryRow(args...) })
}
Example #7
0
func (tx *Tx) QueryC(ctx context.Context, q string, args ...interface{}) (*sql.Rows, error) {
	span, nctx := ot.StartSpanFromContext(ctx, "QueryC")
	defer span.Finish()
	return query(nctx, func() (*sql.Rows, error) { return tx.Query(q, args...) })
}
Example #8
0
func (s *Stmt) QueryC(ctx context.Context, args ...interface{}) (*sql.Rows, error) {
	span, nctx := ot.StartSpanFromContext(ctx, "QueryC")
	defer span.Finish()
	return query(nctx, func() (*sql.Rows, error) { return s.Query(args...) })
}
Example #9
0
func (tx *Tx) ExecC(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
	span, nctx := ot.StartSpanFromContext(ctx, "ExecC")
	defer span.Finish()
	return execute(nctx, func() (sql.Result, error) { return tx.Exec(query, args...) })
}
Example #10
0
func (s *Stmt) ExecC(ctx context.Context, args ...interface{}) (sql.Result, error) {
	span, nctx := ot.StartSpanFromContext(ctx, "ExecC")
	defer span.Finish()
	return execute(nctx, func() (sql.Result, error) { return s.Exec(args...) })
}
Example #11
0
func (tx *Tx) PrepareC(ctx context.Context, query string) (*Stmt, error) {
	span, nctx := ot.StartSpanFromContext(ctx, "PrepareC")
	defer span.Finish()
	return prepare(tx, nctx, query)
}