github.com/matrixorigin/matrixone@v0.7.0/pkg/util/trace/impl/motrace/mo_trace_test.go (about)

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Portions of this file are additionally subject to the following
    16  // copyright.
    17  //
    18  // Copyright (C) 2022 Matrix Origin.
    19  //
    20  // Modified the behavior and the interface of the step.
    21  
    22  package motrace
    23  
    24  import (
    25  	"context"
    26  	"github.com/matrixorigin/matrixone/pkg/util/trace"
    27  	"runtime"
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  var _1TxnID = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}
    34  var _1SesID = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}
    35  var _1TraceID trace.TraceID = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}
    36  var _2TraceID trace.TraceID = [16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2}
    37  var _10F0TraceID trace.TraceID = [16]byte{0x09, 0x87, 0x65, 0x43, 0x21, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0}
    38  var _1SpanID trace.SpanID = [8]byte{0, 0, 0, 0, 0, 0, 0, 1}
    39  var _2SpanID trace.SpanID = [8]byte{0, 0, 0, 0, 0, 0, 0, 2}
    40  var _16SpanID trace.SpanID = [8]byte{0, 0, 0, 0, 0, 0x12, 0x34, 0x56}
    41  
    42  func TestMOTracer_Start(t *testing.T) {
    43  	if runtime.GOOS == `linux` {
    44  		t.Skip()
    45  		return
    46  	}
    47  	type fields struct {
    48  		Enable bool
    49  	}
    50  	type args struct {
    51  		ctx  context.Context
    52  		name string
    53  		opts []trace.SpanOption
    54  	}
    55  	rootCtx := trace.ContextWithSpanContext(context.Background(), trace.SpanContextWithIDs(_1TraceID, _1SpanID))
    56  	stmCtx := trace.ContextWithSpanContext(context.Background(), trace.SpanContextWithID(_1TraceID, trace.SpanKindStatement))
    57  	dAtA := make([]byte, 24)
    58  	span := trace.SpanFromContext(stmCtx)
    59  	c := span.SpanContext()
    60  	cnt, err := c.MarshalTo(dAtA)
    61  	require.Nil(t, err)
    62  	require.Equal(t, 24, cnt)
    63  	sc := &trace.SpanContext{}
    64  	err = sc.Unmarshal(dAtA)
    65  	require.Nil(t, err)
    66  	remoteCtx := trace.ContextWithSpanContext(context.Background(), *sc)
    67  	tests := []struct {
    68  		name             string
    69  		fields           fields
    70  		args             args
    71  		wantNewRoot      bool
    72  		wantTraceId      trace.TraceID
    73  		wantParentSpanId trace.SpanID
    74  		wantKind         trace.SpanKind
    75  	}{
    76  		{
    77  			name:             "normal",
    78  			fields:           fields{Enable: true},
    79  			args:             args{ctx: rootCtx, name: "normal", opts: []trace.SpanOption{}},
    80  			wantNewRoot:      false,
    81  			wantTraceId:      _1TraceID,
    82  			wantParentSpanId: _1SpanID,
    83  			wantKind:         trace.SpanKindInternal,
    84  		},
    85  		{
    86  			name:             "newRoot",
    87  			fields:           fields{Enable: true},
    88  			args:             args{ctx: rootCtx, name: "newRoot", opts: []trace.SpanOption{trace.WithNewRoot(true)}},
    89  			wantNewRoot:      true,
    90  			wantTraceId:      _1TraceID,
    91  			wantParentSpanId: _1SpanID,
    92  			wantKind:         trace.SpanKindInternal,
    93  		},
    94  		{
    95  			name:             "statement",
    96  			fields:           fields{Enable: true},
    97  			args:             args{ctx: stmCtx, name: "newStmt", opts: []trace.SpanOption{}},
    98  			wantNewRoot:      false,
    99  			wantTraceId:      _1TraceID,
   100  			wantParentSpanId: trace.NilSpanID,
   101  			wantKind:         trace.SpanKindStatement,
   102  		},
   103  		{
   104  			name:             "empty",
   105  			fields:           fields{Enable: true},
   106  			args:             args{ctx: context.Background(), name: "backgroundCtx", opts: []trace.SpanOption{}},
   107  			wantNewRoot:      true,
   108  			wantTraceId:      trace.NilTraceID,
   109  			wantParentSpanId: _1SpanID,
   110  			wantKind:         trace.SpanKindInternal,
   111  		},
   112  		{
   113  			name:             "remote",
   114  			fields:           fields{Enable: true},
   115  			args:             args{ctx: remoteCtx, name: "remoteCtx", opts: []trace.SpanOption{}},
   116  			wantNewRoot:      false,
   117  			wantTraceId:      _1TraceID,
   118  			wantParentSpanId: trace.NilSpanID,
   119  			wantKind:         trace.SpanKindRemote,
   120  		},
   121  	}
   122  	tracer := &MOTracer{
   123  		TracerConfig: trace.TracerConfig{Name: "motrace_test"},
   124  		provider:     defaultMOTracerProvider(),
   125  	}
   126  	for _, tt := range tests {
   127  		t.Run(tt.name, func(t1 *testing.T) {
   128  			tracer.provider.enable = tt.fields.Enable
   129  			newCtx, span := tracer.Start(tt.args.ctx, tt.args.name, tt.args.opts...)
   130  			if !tt.wantNewRoot {
   131  				require.Equal(t1, tt.wantTraceId, span.SpanContext().TraceID)
   132  				require.Equal(t1, tt.wantParentSpanId, span.ParentSpanContext().SpanID)
   133  				require.Equal(t1, tt.wantParentSpanId, trace.SpanFromContext(newCtx).ParentSpanContext().SpanID)
   134  			} else {
   135  				require.NotEqualf(t1, tt.wantTraceId, span.SpanContext().TraceID, "want %s, but got %s", tt.wantTraceId.String(), span.SpanContext().TraceID.String())
   136  				require.NotEqual(t1, tt.wantParentSpanId, span.ParentSpanContext().SpanID)
   137  				require.NotEqual(t1, tt.wantParentSpanId, trace.SpanFromContext(newCtx).ParentSpanContext().SpanID)
   138  			}
   139  			require.Equal(t1, tt.wantKind, trace.SpanFromContext(newCtx).ParentSpanContext().Kind)
   140  			require.Equal(t1, span, trace.SpanFromContext(newCtx))
   141  		})
   142  	}
   143  }
   144  
   145  func TestSpanContext_MarshalTo(t *testing.T) {
   146  	type fields struct {
   147  		TraceID trace.TraceID
   148  		SpanID  trace.SpanID
   149  	}
   150  	type args struct {
   151  		dAtA []byte
   152  	}
   153  	tests := []struct {
   154  		name      string
   155  		fields    fields
   156  		args      args
   157  		want      int
   158  		wantBytes []byte
   159  	}{
   160  		{
   161  			name: "normal",
   162  			fields: fields{
   163  				TraceID: trace.NilTraceID,
   164  				SpanID:  _16SpanID,
   165  			},
   166  			args: args{dAtA: make([]byte, 24)},
   167  			want: 24,
   168  			//                1  2  3  4  5  6  7  8, 1  2  3  4  5  6  7  8--1  2  3  4  5  6     7     8
   169  			wantBytes: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x34, 0x56},
   170  		},
   171  		{
   172  			name: "not-zero",
   173  			fields: fields{
   174  				TraceID: _10F0TraceID,
   175  				SpanID:  _16SpanID,
   176  			},
   177  			args: args{dAtA: make([]byte, 24)},
   178  			want: 24,
   179  			//                1     2     3     4     5     6     7     8,    1  2  3  4  5  6  7  8--1  2  3  4  5  6     7     8
   180  			wantBytes: []byte{0x09, 0x87, 0x65, 0x43, 0x21, 0xff, 0xff, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x34, 0x56},
   181  		},
   182  	}
   183  	for _, tt := range tests {
   184  		t.Run(tt.name, func(t *testing.T) {
   185  			c := &trace.SpanContext{
   186  				TraceID: tt.fields.TraceID,
   187  				SpanID:  tt.fields.SpanID,
   188  				Kind:    trace.SpanKindRemote,
   189  			}
   190  			got, err := c.MarshalTo(tt.args.dAtA)
   191  			require.Equal(t, nil, err)
   192  			require.Equal(t, got, tt.want)
   193  			require.Equal(t, tt.wantBytes, tt.args.dAtA)
   194  			newC := &trace.SpanContext{}
   195  			err = newC.Unmarshal(tt.args.dAtA)
   196  			require.Equal(t, nil, err)
   197  			require.Equal(t, c, newC)
   198  			require.Equal(t, tt.fields.TraceID, newC.TraceID)
   199  			require.Equal(t, tt.fields.SpanID, newC.SpanID)
   200  		})
   201  	}
   202  }