istio.io/istio@v0.0.0-20240520182934-d79c90f27776/tests/integration/telemetry/tracing/zipkin/client_tracing_test.go (about)

     1  //go:build integ
     2  // +build integ
     3  
     4  // Copyright Istio Authors. All Rights Reserved.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //     http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  
    18  package zipkin
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/google/uuid"
    27  
    28  	"istio.io/istio/pkg/test/framework"
    29  	"istio.io/istio/pkg/test/util/retry"
    30  	"istio.io/istio/tests/integration/telemetry/tracing"
    31  )
    32  
    33  // TestClientTracing exercises the trace generation features of Istio, based on the Envoy Trace driver for zipkin using
    34  // client initiated tracing using envoy traceheader.
    35  // The test verifies that all expected spans (a client span and a server span for each service call in the sample bookinfo app)
    36  // are generated and that they are all a part of the same distributed trace with correct hierarchy and name.
    37  func TestClientTracing(t *testing.T) {
    38  	framework.NewTest(t).
    39  		Run(func(t framework.TestContext) {
    40  			appNsInst := tracing.GetAppNamespace()
    41  			for _, cluster := range t.Clusters().ByNetwork()[t.Clusters().Default().NetworkName()] {
    42  				cluster := cluster
    43  				t.NewSubTest(cluster.StableName()).Run(func(ctx framework.TestContext) {
    44  					retry.UntilSuccessOrFail(ctx, func() error {
    45  						// Send test traffic with a trace header.
    46  						id := uuid.NewString()
    47  						extraHeader := map[string][]string{
    48  							tracing.TraceHeader: {id},
    49  						}
    50  						err := tracing.SendTraffic(ctx, extraHeader, cluster)
    51  						if err != nil {
    52  							return fmt.Errorf("cannot send traffic from cluster %s: %v", cluster.Name(), err)
    53  						}
    54  						traces, err := tracing.GetZipkinInstance().QueryTraces(100,
    55  							fmt.Sprintf("server.%s.svc.cluster.local:80/*", appNsInst.Name()), "")
    56  						if err != nil {
    57  							return fmt.Errorf("cannot get traces from zipkin: %v", err)
    58  						}
    59  						if !tracing.VerifyEchoTraces(ctx, appNsInst.Name(), cluster.Name(), traces) {
    60  							return errors.New("cannot find expected traces")
    61  						}
    62  						return nil
    63  					}, retry.Delay(3*time.Second), retry.Timeout(80*time.Second))
    64  				})
    65  
    66  			}
    67  		})
    68  }