github.com/waldiirawan/apm-agent-go/v2@v2.2.2/example_tracecontext_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  	"html/template"
    23  	"os"
    24  
    25  	"github.com/waldiirawan/apm-agent-go/v2"
    26  )
    27  
    28  func ExampleTransaction_EnsureParent() {
    29  	tx := apm.DefaultTracer().StartTransactionOptions("name", "type", apm.TransactionOptions{
    30  		TraceContext: apm.TraceContext{
    31  			Trace: apm.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
    32  			Span:  apm.SpanID{0, 1, 2, 3, 4, 5, 6, 7},
    33  		},
    34  	})
    35  	defer tx.Discard()
    36  
    37  	tpl := template.Must(template.New("").Parse(`
    38  <script src="elastic-apm-js-base/dist/bundles/elastic-apm-js-base.umd.min.js"></script>
    39  <script>
    40    elasticApm.init({
    41      serviceName: '',
    42      serverUrl: 'http://localhost:8200',
    43      pageLoadTraceId: {{.TraceContext.Trace}},
    44      pageLoadSpanId: {{.EnsureParent}},
    45      pageLoadSampled: {{.Sampled}},
    46    })
    47  </script>
    48  `))
    49  
    50  	if err := tpl.Execute(os.Stdout, tx); err != nil {
    51  		panic(err)
    52  	}
    53  
    54  	// Output:
    55  	// <script src="elastic-apm-js-base/dist/bundles/elastic-apm-js-base.umd.min.js"></script>
    56  	// <script>
    57  	//   elasticApm.init({
    58  	//     serviceName: '',
    59  	//     serverUrl: 'http://localhost:8200',
    60  	//     pageLoadTraceId: "000102030405060708090a0b0c0d0e0f",
    61  	//     pageLoadSpanId: "0001020304050607",
    62  	//     pageLoadSampled:  false ,
    63  	//   })
    64  	// </script>
    65  }
    66  
    67  func ExampleTransaction_EnsureParent_nilTransaction() {
    68  	tpl := template.Must(template.New("").Parse(`
    69  <script>
    70  elasticApm.init({
    71    {{.TraceContext.Trace}},
    72    {{.EnsureParent}},
    73    {{.Sampled}},
    74  })
    75  </script>
    76  `))
    77  
    78  	// Demonstrate that Transaction's TraceContext, EnsureParent,
    79  	// and Sampled methods will not panic when called with a nil
    80  	// Transaction.
    81  	tx := apm.TransactionFromContext(context.Background())
    82  	if err := tpl.Execute(os.Stdout, tx); err != nil {
    83  		panic(err)
    84  	}
    85  
    86  	// Output:
    87  	// <script>
    88  	// elasticApm.init({
    89  	//   "00000000000000000000000000000000",
    90  	//   "0000000000000000",
    91  	//    false ,
    92  	// })
    93  	// </script>
    94  }