Слияние кода завершено, страница обновится автоматически
//
// Copyright 2022 SkyAPM org
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package go2sky
import (
"context"
"sync"
"testing"
"github.com/SkyAPM/go2sky/propagation"
)
const (
sample = 1
traceID = "1f2d4bf47bf711eab794acde48001122"
parentSegmentID = "1e7c204a7bf711eab858acde48001122"
parentSpanID = 0
parentService = "service"
parentServiceInstance = "instance"
parentEndpoint = "/foo/bar"
addressUsedAtClient = "foo.svc:8787"
)
var header string
func init() {
scx := propagation.SpanContext{
Sample: sample,
TraceID: traceID,
ParentSegmentID: parentSegmentID,
ParentSpanID: parentSpanID,
ParentService: parentService,
ParentServiceInstance: parentServiceInstance,
ParentEndpoint: parentEndpoint,
AddressUsedAtClient: addressUsedAtClient,
}
header = scx.EncodeSW8()
}
func TestTracer_EntryAndExit(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(2)
tracer, err := NewTracer("service", WithReporter(&NoopReporter{wg: wg}))
if err != nil {
t.Error(err)
}
entrySpan, ctx, err := tracer.CreateEntrySpan(context.Background(), "/rest/api", func(key string) (string, error) {
return "", nil
})
if err != nil {
t.Error(err)
}
exitSpan, err := tracer.CreateExitSpan(ctx, "/foo/bar", "foo.svc:8787", func(key, value string) error {
scx := propagation.SpanContext{}
if key == propagation.Header {
err = scx.DecodeSW8(value)
if err != nil {
t.Fail()
}
}
if key == propagation.HeaderCorrelation {
err = scx.DecodeSW8Correlation(value)
if err != nil {
t.Fail()
}
}
return nil
})
if err != nil {
t.Error(err)
}
exitSpan.End()
entrySpan.End()
wg.Wait()
}
func TestTracer_Entry(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(1)
reporter := &NoopReporter{wg: wg}
tracer, err := NewTracer("service", WithReporter(reporter))
if err != nil {
t.Error(err)
}
entrySpan, _, err := tracer.CreateEntrySpan(context.Background(), "/rest/api", func(key string) (string, error) {
return header, nil
})
if err != nil {
t.Error(err)
}
entrySpan.End()
wg.Wait()
span := reporter.Spans[0]
if span.Context().TraceID != traceID {
t.Fail()
}
if len(span.Refs()) != 1 {
t.Fail()
}
}
func TestTracer_EntryAndExitInTrace(t *testing.T) {
wg := &sync.WaitGroup{}
wg.Add(2)
tracer, err := NewTracer("service", WithInstance("instance"), WithReporter(&NoopReporter{wg: wg}))
if err != nil {
t.Error(err)
}
entrySpan, ctx, err := tracer.CreateEntrySpan(context.Background(), "/rest/api", func(key string) (string, error) {
return header, nil
})
if err != nil {
t.Error(err)
}
sc := propagation.SpanContext{}
exitSpan, err := tracer.CreateExitSpan(ctx, "/foo/bar", "foo.svc:8786", func(key, value string) error {
if key == propagation.Header {
err = sc.DecodeSW8(value)
if err != nil {
t.Fail()
}
}
if key == propagation.HeaderCorrelation {
err = sc.DecodeSW8Correlation(value)
if err != nil {
t.Fail()
}
}
return nil
})
if err != nil {
t.Error(err)
}
if sc.Sample != sample {
t.Fail()
}
if sc.TraceID != traceID {
t.Fail()
}
if sc.ParentSpanID != 1 {
t.Fail()
}
if sc.ParentService != "service" {
t.Fail()
}
if sc.ParentServiceInstance != "instance" {
t.Fail()
}
if sc.ParentEndpoint != "/rest/api" {
t.Fail()
}
if sc.AddressUsedAtClient != "foo.svc:8786" {
t.Fail()
}
exitSpan.End()
entrySpan.End()
wg.Wait()
}
type NoopReporter struct {
wg *sync.WaitGroup
Spans []ReportedSpan
}
func (*NoopReporter) Boot(service string, serviceInstance string, cdsWatchers []AgentConfigChangeWatcher) {
}
func (r *NoopReporter) Send(spans []ReportedSpan) {
r.Spans = spans
for range spans {
r.wg.Done()
}
}
func (NoopReporter) Close() {
}
Вы можете оставить комментарий после Вход в систему
Неприемлемый контент может быть отображен здесь и не будет показан на странице. Вы можете проверить и изменить его с помощью соответствующей функции редактирования.
Если вы подтверждаете, что содержание не содержит непристойной лексики/перенаправления на рекламу/насилия/вульгарной порнографии/нарушений/пиратства/ложного/незначительного или незаконного контента, связанного с национальными законами и предписаниями, вы можете нажать «Отправить» для подачи апелляции, и мы обработаем ее как можно скорее.
Опубликовать ( 0 )