github.com/waldiirawan/apm-agent-go/v2@v2.2.2/context_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  	"net/http"
    23  	"net/url"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"github.com/waldiirawan/apm-agent-go/v2"
    30  	"github.com/waldiirawan/apm-agent-go/v2/apmtest"
    31  	"github.com/waldiirawan/apm-agent-go/v2/model"
    32  )
    33  
    34  func TestContextLabels(t *testing.T) {
    35  	type customInt int
    36  	tx := testSendTransaction(t, func(tx *apm.Transaction) {
    37  		tx.Context.SetLabel("foo", "bar")
    38  		tx.Context.SetLabel("foo", "bar!") // Last instance wins
    39  		tx.Context.SetLabel("bar", "baz")
    40  		tx.Context.SetLabel("baz", 123.456)
    41  		tx.Context.SetLabel("qux", true)
    42  		tx.Context.SetLabel("quux", customInt(123))
    43  	})
    44  	assert.Equal(t, model.IfaceMap{
    45  		{Key: "bar", Value: "baz"},
    46  		{Key: "baz", Value: 123.456},
    47  		{Key: "foo", Value: "bar!"},
    48  		{Key: "quux", Value: 123.0},
    49  		{Key: "qux", Value: true},
    50  	}, tx.Context.Tags)
    51  }
    52  
    53  func TestContextUser(t *testing.T) {
    54  	t.Run("email", func(t *testing.T) {
    55  		tx := testSendTransaction(t, func(tx *apm.Transaction) {
    56  			tx.Context.SetUserEmail("testing@host.invalid")
    57  		})
    58  		assert.Equal(t, &model.User{Email: "testing@host.invalid"}, tx.Context.User)
    59  	})
    60  	t.Run("username", func(t *testing.T) {
    61  		tx := testSendTransaction(t, func(tx *apm.Transaction) {
    62  			tx.Context.SetUsername("schnibble")
    63  		})
    64  		assert.Equal(t, &model.User{Username: "schnibble"}, tx.Context.User)
    65  	})
    66  	t.Run("id", func(t *testing.T) {
    67  		tx := testSendTransaction(t, func(tx *apm.Transaction) {
    68  			tx.Context.SetUserID("123")
    69  		})
    70  		assert.Equal(t, &model.User{ID: "123"}, tx.Context.User)
    71  	})
    72  }
    73  
    74  func TestContextFramework(t *testing.T) {
    75  	t.Run("name_unspecified", func(t *testing.T) {
    76  		tx := testSendTransaction(t, func(tx *apm.Transaction) {
    77  			tx.Context.SetFramework("", "1.0")
    78  		})
    79  		assert.Nil(t, tx.Context)
    80  	})
    81  	t.Run("version_specified", func(t *testing.T) {
    82  		tx := testSendTransaction(t, func(tx *apm.Transaction) {
    83  			tx.Context.SetFramework("framework", "1.0")
    84  		})
    85  		require.NotNil(t, tx.Context)
    86  		require.NotNil(t, tx.Context.Service)
    87  		assert.Equal(t, &model.Framework{
    88  			Name:    "framework",
    89  			Version: "1.0",
    90  		}, tx.Context.Service.Framework)
    91  	})
    92  	t.Run("version_unspecified", func(t *testing.T) {
    93  		tx := testSendTransaction(t, func(tx *apm.Transaction) {
    94  			tx.Context.SetFramework("framework", "")
    95  		})
    96  		require.NotNil(t, tx.Context)
    97  		require.NotNil(t, tx.Context.Service)
    98  		assert.Equal(t, &model.Framework{
    99  			Name:    "framework",
   100  			Version: "unspecified",
   101  		}, tx.Context.Service.Framework)
   102  	})
   103  }
   104  
   105  func TestContextCustom(t *testing.T) {
   106  	type arbitraryStruct struct {
   107  		Field string
   108  	}
   109  	tx := testSendTransaction(t, func(tx *apm.Transaction) {
   110  		tx.Context.SetCustom("string", "string")
   111  		tx.Context.SetCustom("bool", true)
   112  		tx.Context.SetCustom("number", 1.23)
   113  		tx.Context.SetCustom("struct", arbitraryStruct{Field: "foo"})
   114  	})
   115  	require.NotNil(t, tx.Context)
   116  	assert.Equal(t, model.IfaceMap{
   117  		{Key: "bool", Value: true},
   118  		{Key: "number", Value: 1.23},
   119  		{Key: "string", Value: "string"},
   120  		{Key: "struct", Value: map[string]interface{}{"Field": "foo"}},
   121  	}, tx.Context.Custom)
   122  }
   123  
   124  func TestContextSetUsernamePrecedence(t *testing.T) {
   125  	tx := testSendTransaction(t, func(tx *apm.Transaction) {
   126  		tx.Context.SetUsername("frieda")
   127  		req, _ := http.NewRequest("GET", "/", nil)
   128  		req.URL.User = url.User("fred")
   129  		tx.Context.SetHTTPRequest(req)
   130  	})
   131  	require.NotNil(t, tx.Context)
   132  	require.NotNil(t, "", tx.Context.User)
   133  	assert.Equal(t, "frieda", tx.Context.User.Username)
   134  }
   135  
   136  func testSendTransaction(t *testing.T, f func(tx *apm.Transaction)) model.Transaction {
   137  	transaction, _, _ := apmtest.WithTransaction(func(ctx context.Context) {
   138  		f(apm.TransactionFromContext(ctx))
   139  	})
   140  	return transaction
   141  }