|
| 1 | +// Copyright 2012-present Oliver Eilhard. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-license. |
| 3 | +// See http://olivere.mit-license.org/license.txt for details. |
| 4 | + |
| 5 | +package opentracing |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/opentracing/opentracing-go" |
| 14 | + "github.com/opentracing/opentracing-go/mocktracer" |
| 15 | + |
| 16 | + "github.com/olivere/elastic/v7" |
| 17 | +) |
| 18 | + |
| 19 | +func TestTransportIntegration(t *testing.T) { |
| 20 | + // Mock tracer |
| 21 | + tracer := mocktracer.New() |
| 22 | + opentracing.InitGlobalTracer(tracer) |
| 23 | + |
| 24 | + // Setup a simple transport |
| 25 | + tr := NewTransport() |
| 26 | + httpClient := &http.Client{ |
| 27 | + Transport: tr, |
| 28 | + } |
| 29 | + |
| 30 | + // Create a simple Ping request via Elastic |
| 31 | + client, err := elastic.NewClient( |
| 32 | + elastic.SetURL("http://127.0.0.1:9210"), |
| 33 | + elastic.SetHealthcheck(false), |
| 34 | + elastic.SetSniff(false), |
| 35 | + elastic.SetBasicAuth("elastic", "elastic"), |
| 36 | + elastic.SetHttpClient(httpClient), |
| 37 | + ) |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + _, err = client.Search("_all").Query(elastic.NewMatchAllQuery()).Do(context.Background()) |
| 42 | + if err != nil { |
| 43 | + t.Fatal(err) |
| 44 | + } |
| 45 | + |
| 46 | + // Check the data written into tracer |
| 47 | + spans := tracer.FinishedSpans() |
| 48 | + if want, have := 1, len(spans); want != have { |
| 49 | + t.Fatalf("want %d finished spans, have %d", want, have) |
| 50 | + } |
| 51 | + span := spans[0] |
| 52 | + |
| 53 | + if want, have := "PerformRequest", span.OperationName; want != have { |
| 54 | + t.Fatalf("want Span.OperationName=%q, have %q", want, have) |
| 55 | + } |
| 56 | + if want, have := "github.com/olivere/elastic/v7", span.Tag("component"); want != have { |
| 57 | + t.Fatalf("want component tag=%q, have %q", want, have) |
| 58 | + } |
| 59 | + httpURL, ok := span.Tag("http.url").(string) |
| 60 | + if !ok || httpURL == "" { |
| 61 | + t.Fatalf("want http.url tag=%q to be a non-empty string (found type %T)", "http.url", span.Tag("http.url")) |
| 62 | + } |
| 63 | + if want, have := "http://127.0.0.1:9210/_all/_search", httpURL; want != have { |
| 64 | + t.Fatalf("want http.url tag=%q, have %q", want, have) |
| 65 | + } |
| 66 | + if strings.Contains(httpURL, "elastic") { |
| 67 | + t.Fatalf("want http.url tag %q to not contain username and/or password: %s", "URL", span.Tag("http.url")) |
| 68 | + } |
| 69 | + if want, have := "POST", span.Tag("http.method"); want != have { |
| 70 | + t.Fatalf("want http.method tag=%q, have %q", want, have) |
| 71 | + } |
| 72 | + if want, have := uint16(http.StatusOK), span.Tag("http.status_code"); want != have { |
| 73 | + t.Fatalf("want http.status_code tag=%v (%T), have %v (%T)", want, want, have, have) |
| 74 | + } |
| 75 | +} |
0 commit comments