1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-07-15 01:04:25 +02:00

exporter(stackdriver): fix ExportSpans when ctx is not nil (#294)

* exporter(stackdriver): fix ExportSpans when ctx is not nil

- problem: if ctx is not, calling cancel() will panic
- nil Context already handled in newContextWithTimeout
- add with test

* update go.sum

* mock traceserver for traceclient

* make precommit

* - respect option.Context when get google cred and traceclient

* Revise with timeout, context

- rename blackbox tests package to stackdriver_test
- remove context is nil checking, add to initialization
- add tests for timeout
This commit is contained in:
Cheng-Lung Sung
2019-11-15 01:45:17 +08:00
committed by rghetia
parent f403198454
commit a228bafec5
6 changed files with 188 additions and 22 deletions

View File

@ -144,6 +144,13 @@ func WithContext(ctx context.Context) func(o *options) {
}
}
// WithTimeout sets the timeout for trace exporter and metric exporter
func WithTimeout(t time.Duration) func(o *options) {
return func(o *options) {
o.Timeout = t
}
}
func (o *options) handleError(err error) {
if o.OnError != nil {
o.OnError(err)
@ -168,16 +175,12 @@ type Exporter struct {
// TODO(yoshifumi): add a metrics exporter one the spec definition
// process and the sampler implementation are done.
func NewExporter(opts ...Option) (*Exporter, error) {
o := options{}
o := options{Context: context.Background()}
for _, opt := range opts {
opt(&o)
}
if o.ProjectID == "" {
ctx := o.Context
if ctx == nil {
ctx = context.Background()
}
creds, err := google.FindDefaultCredentials(ctx, traceapi.DefaultAuthScopes()...)
creds, err := google.FindDefaultCredentials(o.Context, traceapi.DefaultAuthScopes()...)
if err != nil {
return nil, fmt.Errorf("Stackdriver: %v", err)
}
@ -197,9 +200,6 @@ func NewExporter(opts ...Option) (*Exporter, error) {
}
func newContextWithTimeout(ctx context.Context, timeout time.Duration) (context.Context, func()) {
if ctx == nil {
ctx = context.Background()
}
if timeout <= 0 {
timeout = defaultTimeout
}