github.com/waldiirawan/apm-agent-go/v2@v2.2.2/fmt_test.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package apm_test
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"log"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	"github.com/waldiirawan/apm-agent-go/v2"
    29  	"github.com/waldiirawan/apm-agent-go/v2/apmtest"
    30  )
    31  
    32  func ExampleTraceFormatter() {
    33  	apmtest.WithTransaction(func(ctx context.Context) {
    34  		span, ctx := apm.StartSpan(ctx, "name", "type")
    35  		defer span.End()
    36  
    37  		// The %+v format will add
    38  		//
    39  		//     "trace.id=... transaction.id=... span.id=..."
    40  		//
    41  		// to the log output.
    42  		log.Printf("ERROR [%+v] blah blah", apm.TraceFormatter(ctx))
    43  	})
    44  }
    45  
    46  func TestTraceFormatterNothing(t *testing.T) {
    47  	f := apm.TraceFormatter(context.Background())
    48  	for _, format := range []string{
    49  		"%v", "%t", "%x", "%s",
    50  		"%+v", "%+t", "%+x", "%+s",
    51  	} {
    52  		out := fmt.Sprintf(format, f)
    53  		assert.Equal(t, "", out)
    54  	}
    55  }
    56  
    57  func TestTraceFormatterTransaction(t *testing.T) {
    58  	var results []string
    59  	tx, _, _ := apmtest.WithTransaction(func(ctx context.Context) {
    60  		f := apm.TraceFormatter(ctx)
    61  		for _, format := range []string{
    62  			"%v", "%t", "%x", "%s",
    63  			"%+v", "%+t", "%+x", "%+s",
    64  		} {
    65  			out := fmt.Sprintf(format, f)
    66  			results = append(results, out)
    67  		}
    68  	})
    69  	assert.Equal(t, []string{
    70  		fmt.Sprintf("%x %x", tx.TraceID, tx.ID),
    71  		fmt.Sprintf("%x", tx.TraceID),
    72  		fmt.Sprintf("%x", tx.ID),
    73  		"",
    74  		fmt.Sprintf("trace.id=%x transaction.id=%x", tx.TraceID, tx.ID),
    75  		fmt.Sprintf("trace.id=%x", tx.TraceID),
    76  		fmt.Sprintf("transaction.id=%x", tx.ID),
    77  		"",
    78  	}, results)
    79  }
    80  
    81  func TestTraceFormatterSpan(t *testing.T) {
    82  	var results []string
    83  	tx, spans, _ := apmtest.WithTransaction(func(ctx context.Context) {
    84  		span, ctx := apm.StartSpan(ctx, "name", "type")
    85  		defer span.End()
    86  
    87  		f := apm.TraceFormatter(ctx)
    88  		for _, format := range []string{
    89  			"%v", "%t", "%x", "%s",
    90  			"%+v", "%+t", "%+x", "%+s",
    91  		} {
    92  			out := fmt.Sprintf(format, f)
    93  			results = append(results, out)
    94  		}
    95  	})
    96  	span := spans[0]
    97  	assert.Equal(t, []string{
    98  		fmt.Sprintf("%x %x %x", tx.TraceID, tx.ID, span.ID),
    99  		fmt.Sprintf("%x", tx.TraceID),
   100  		fmt.Sprintf("%x", tx.ID),
   101  		fmt.Sprintf("%x", span.ID),
   102  		fmt.Sprintf("trace.id=%x transaction.id=%x span.id=%x", tx.TraceID, tx.ID, span.ID),
   103  		fmt.Sprintf("trace.id=%x", tx.TraceID),
   104  		fmt.Sprintf("transaction.id=%x", tx.ID),
   105  		fmt.Sprintf("span.id=%x", span.ID),
   106  	}, results)
   107  }