github.com/gogf/gf/v2@v2.7.4/os/gmetric/gmetric_z_unit_internal_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gmetric 8 9 import ( 10 "testing" 11 12 "github.com/gogf/gf/v2/test/gtest" 13 ) 14 15 func truncateGlobalAttributes() { 16 globalAttributesMu.Lock() 17 defer globalAttributesMu.Unlock() 18 globalAttributes = make([]globalAttributeItem, 0) 19 } 20 21 func Test_GlobalAttributes(t *testing.T) { 22 defer truncateGlobalAttributes() 23 gtest.C(t, func(t *gtest.T) { 24 SetGlobalAttributes(Attributes{ 25 NewAttribute("global", "gl"), 26 }, SetGlobalAttributesOption{ 27 Instrument: "", 28 InstrumentVersion: "", 29 InstrumentPattern: "", 30 }) 31 32 SetGlobalAttributes(Attributes{ 33 NewAttribute("a", 1), 34 }, SetGlobalAttributesOption{ 35 Instrument: "ins_a", 36 InstrumentVersion: "v1.0", 37 InstrumentPattern: "", 38 }) 39 SetGlobalAttributes(Attributes{ 40 NewAttribute("b", 2), 41 }, SetGlobalAttributesOption{ 42 Instrument: "ins_bb", 43 InstrumentVersion: "v1.1", 44 InstrumentPattern: "", 45 }) 46 SetGlobalAttributes(Attributes{ 47 NewAttribute("c", 3), 48 }, SetGlobalAttributesOption{ 49 Instrument: "ins_bb", 50 InstrumentVersion: "v1.1", 51 InstrumentPattern: "", 52 }) 53 SetGlobalAttributes(Attributes{ 54 NewAttribute("d", 4), 55 }, SetGlobalAttributesOption{ 56 Instrument: "", 57 InstrumentVersion: "v1.0", 58 InstrumentPattern: "ins.+", 59 }) 60 SetGlobalAttributes(Attributes{ 61 NewAttribute("e", 5), 62 }, SetGlobalAttributesOption{ 63 Instrument: "", 64 InstrumentVersion: "v1.0", 65 InstrumentPattern: "ins_b.+", 66 }) 67 SetGlobalAttributes(Attributes{ 68 NewAttribute("f", 6), 69 }, SetGlobalAttributesOption{ 70 Instrument: "", 71 InstrumentVersion: "v1.1", 72 InstrumentPattern: "ins_b.+", 73 }) 74 75 t.Assert(GetGlobalAttributes(GetGlobalAttributesOption{ 76 Instrument: "", 77 InstrumentVersion: "", 78 }), Attributes{ 79 NewAttribute("global", "gl"), 80 }) 81 t.Assert(GetGlobalAttributes(GetGlobalAttributesOption{ 82 Instrument: "ins_a", 83 InstrumentVersion: "", 84 }), Attributes{}) 85 t.Assert(GetGlobalAttributes(GetGlobalAttributesOption{ 86 Instrument: "ins_a", 87 InstrumentVersion: "1.1", 88 }), Attributes{}) 89 t.Assert(GetGlobalAttributes(GetGlobalAttributesOption{ 90 Instrument: "ins_bb", 91 InstrumentVersion: "v1.0", 92 }), Attributes{ 93 NewAttribute("d", 4), 94 NewAttribute("e", 5), 95 }) 96 t.Assert(GetGlobalAttributes(GetGlobalAttributesOption{ 97 Instrument: "ins_bb", 98 InstrumentVersion: "v1.1", 99 }), Attributes{ 100 NewAttribute("b", 2), 101 NewAttribute("c", 3), 102 NewAttribute("f", 6), 103 }) 104 t.Assert(GetGlobalAttributes(GetGlobalAttributesOption{ 105 Instrument: "ins_cc", 106 InstrumentVersion: "v1.1", 107 }), Attributes{}) 108 }) 109 }