github.com/pinpoint-apm/pinpoint-go-agent@v1.4.1-0.20240110120318-a50c2eb18c8c/config_test.go (about) 1 package pinpoint 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "math" 6 "os" 7 "testing" 8 ) 9 10 func TestNewConfig_DefaultValue(t *testing.T) { 11 type args struct { 12 opts []ConfigOption 13 } 14 15 opts := []ConfigOption{ 16 WithAppName("TestApp"), 17 } 18 19 tests := []struct { 20 name string 21 args args 22 }{ 23 {"1", args{opts}}, 24 } 25 for _, tt := range tests { 26 t.Run(tt.name, func(t *testing.T) { 27 c, _ := NewConfig(tt.args.opts...) 28 assert.Equal(t, "TestApp", c.String(CfgAppName), CfgAppName) 29 assert.Equal(t, ServiceTypeGoApp, c.Int(CfgAppType), CfgAppType) 30 assert.Empty(t, c.String(CfgAgentID), CfgAgentID) 31 assert.Empty(t, c.String(CfgAgentName), CfgAgentName) 32 assert.Equal(t, "localhost", c.String(CfgCollectorHost), CfgCollectorHost) 33 assert.Equal(t, 9991, c.Int(CfgCollectorAgentPort), CfgCollectorAgentPort) 34 assert.Equal(t, 9993, c.Int(CfgCollectorSpanPort), CfgCollectorSpanPort) 35 assert.Equal(t, 9992, c.Int(CfgCollectorStatPort), CfgCollectorStatPort) 36 assert.Equal(t, "info", c.String(CfgLogLevel), CfgLogLevel) 37 assert.Equal(t, "stderr", c.String(CfgLogOutput), CfgLogOutput) 38 assert.Equal(t, 10, c.Int(CfgLogMaxSize), CfgLogMaxSize) 39 assert.Equal(t, samplingTypeCounter, c.String(CfgSamplingType), CfgSamplingType) 40 assert.Equal(t, 1, c.Int(CfgSamplingCounterRate), CfgSamplingCounterRate) 41 assert.Equal(t, float64(100), c.Float(CfgSamplingPercentRate), CfgSamplingPercentRate) 42 assert.Equal(t, 0, c.Int(CfgSamplingNewThroughput), CfgSamplingNewThroughput) 43 assert.Equal(t, 0, c.Int(CfgSamplingContinueThroughput), CfgSamplingContinueThroughput) 44 assert.Equal(t, defaultQueueSize, c.Int(CfgSpanQueueSize), CfgSpanQueueSize) 45 assert.Equal(t, defaultEventDepth, c.Int(CfgSpanMaxCallStackDepth), CfgSpanMaxCallStackDepth) 46 assert.Equal(t, defaultEventSequence, c.Int(CfgSpanMaxCallStackSequence), CfgSpanMaxCallStackSequence) 47 assert.Equal(t, 5000, c.Int(CfgStatCollectInterval), CfgStatCollectInterval) 48 assert.Equal(t, 6, c.Int(CfgStatBatchCount), CfgStatBatchCount) 49 assert.Equal(t, false, c.Bool(CfgIsContainerEnv), CfgIsContainerEnv) 50 assert.Empty(t, c.String(CfgConfigFile), CfgConfigFile) 51 assert.Empty(t, c.String(CfgActiveProfile), CfgActiveProfile) 52 assert.Equal(t, true, c.Bool(CfgSQLTraceBindValue), CfgSQLTraceBindValue) 53 assert.Equal(t, 1024, c.Int(CfgSQLMaxBindValueSize), CfgSQLMaxBindValueSize) 54 assert.Equal(t, true, c.Bool(CfgSQLTraceCommit), CfgSQLTraceCommit) 55 assert.Equal(t, true, c.Bool(CfgSQLTraceRollback), CfgSQLTraceRollback) 56 assert.Equal(t, false, c.Bool(CfgSQLTraceQueryStat), CfgSQLTraceQueryStat) 57 assert.Equal(t, true, c.Bool(CfgEnable), CfgEnable) 58 assert.Equal(t, false, c.Bool(CfgHttpUrlStatEnable), CfgHttpUrlStatEnable) 59 assert.Equal(t, 1024, c.Int(CfgHttpUrlStatLimitSize), CfgHttpUrlStatLimitSize) 60 assert.Equal(t, false, c.Bool(CfgErrorTraceCallStack), CfgErrorTraceCallStack) 61 assert.Equal(t, 32, c.Int(CfgErrorCallStackDepth), CfgErrorCallStackDepth) 62 }) 63 } 64 } 65 66 func TestNewConfig_WithFunc(t *testing.T) { 67 type args struct { 68 opts []ConfigOption 69 } 70 71 opts := []ConfigOption{ 72 WithAppName("TestApp"), 73 WithAppType(1234), 74 WithAgentId("TestAgent"), 75 WithAgentName("TestAgentName"), 76 WithCollectorHost("func.collector.host"), 77 WithCollectorAgentPort(7777), 78 WithCollectorSpanPort(8888), 79 WithCollectorStatPort(9999), 80 WithLogLevel("error"), 81 WithLogOutput("stdout"), 82 WithLogMaxSize(100), 83 WithSamplingType("percent"), 84 WithSamplingPercentRate(90), 85 WithSamplingCounterRate(200), 86 WithSamplingNewThroughput(20), 87 WithSamplingContinueThroughput(30), 88 WithSpanQueueSize(2048), 89 WithSpanMaxCallStackDepth(100), 90 WithSpanMaxCallStackSequence(1000), 91 WithStatCollectInterval(10000), 92 WithStatBatchCount(3), 93 WithIsContainerEnv(true), 94 WithSQLTraceBindValue(false), 95 WithSQLMaxBindValueSize(512), 96 WithSQLTraceCommit(false), 97 WithSQLTraceRollback(false), 98 WithSQLTraceQueryStat(true), 99 WithEnable(false), 100 WithHttpUrlStatEnable(true), 101 WithHttpUrlStatLimitSize(2048), 102 WithErrorTraceCallStack(true), 103 WithErrorCallStackDepth(64), 104 } 105 106 tests := []struct { 107 name string 108 args args 109 }{ 110 {"1", args{opts}}, 111 } 112 for _, tt := range tests { 113 t.Run(tt.name, func(t *testing.T) { 114 c, _ := NewConfig(tt.args.opts...) 115 assert.Equal(t, "TestApp", c.String(CfgAppName), CfgAppName) 116 assert.Equal(t, 1234, c.Int(CfgAppType), CfgAppType) 117 assert.Equal(t, "TestAgent", c.String(CfgAgentID), CfgAgentID) 118 assert.Equal(t, "TestAgentName", c.String(CfgAgentName), CfgAgentName) 119 assert.Equal(t, "func.collector.host", c.String(CfgCollectorHost), CfgCollectorHost) 120 assert.Equal(t, 7777, c.Int(CfgCollectorAgentPort), CfgCollectorAgentPort) 121 assert.Equal(t, 8888, c.Int(CfgCollectorSpanPort), CfgCollectorSpanPort) 122 assert.Equal(t, 9999, c.Int(CfgCollectorStatPort), CfgCollectorStatPort) 123 assert.Equal(t, "error", c.String(CfgLogLevel), CfgLogLevel) 124 assert.Equal(t, "stdout", c.String(CfgLogOutput), CfgLogOutput) 125 assert.Equal(t, 100, c.Int(CfgLogMaxSize), CfgLogMaxSize) 126 assert.Equal(t, "percent", c.String(CfgSamplingType), CfgSamplingType) 127 assert.Equal(t, 200, c.Int(CfgSamplingCounterRate), CfgSamplingCounterRate) 128 assert.Equal(t, float64(90), c.Float(CfgSamplingPercentRate), CfgSamplingPercentRate) 129 assert.Equal(t, 20, c.Int(CfgSamplingNewThroughput), CfgSamplingNewThroughput) 130 assert.Equal(t, 30, c.Int(CfgSamplingContinueThroughput), CfgSamplingContinueThroughput) 131 assert.Equal(t, 2048, c.Int(CfgSpanQueueSize), CfgSpanQueueSize) 132 assert.Equal(t, 100, c.Int(CfgSpanMaxCallStackDepth), CfgSpanMaxCallStackDepth) 133 assert.Equal(t, 1000, c.Int(CfgSpanMaxCallStackSequence), CfgSpanMaxCallStackSequence) 134 assert.Equal(t, 10000, c.Int(CfgStatCollectInterval), CfgStatCollectInterval) 135 assert.Equal(t, 3, c.Int(CfgStatBatchCount), CfgStatBatchCount) 136 assert.Equal(t, true, c.Bool(CfgIsContainerEnv), CfgIsContainerEnv) 137 assert.Equal(t, false, c.Bool(CfgSQLTraceBindValue), CfgSQLTraceBindValue) 138 assert.Equal(t, 512, c.Int(CfgSQLMaxBindValueSize), CfgSQLMaxBindValueSize) 139 assert.Equal(t, false, c.Bool(CfgSQLTraceCommit), CfgSQLTraceCommit) 140 assert.Equal(t, false, c.Bool(CfgSQLTraceRollback), CfgSQLTraceRollback) 141 assert.Equal(t, true, c.Bool(CfgSQLTraceQueryStat), CfgSQLTraceQueryStat) 142 assert.Equal(t, false, c.Bool(CfgEnable), CfgEnable) 143 assert.Equal(t, true, c.Bool(CfgHttpUrlStatEnable), CfgHttpUrlStatEnable) 144 assert.Equal(t, 2048, c.Int(CfgHttpUrlStatLimitSize), CfgHttpUrlStatLimitSize) 145 assert.Equal(t, true, c.Bool(CfgErrorTraceCallStack), CfgErrorTraceCallStack) 146 assert.Equal(t, 64, c.Int(CfgErrorCallStackDepth), CfgErrorCallStackDepth) 147 }) 148 } 149 } 150 151 func TestNewConfig_AppNameMissing(t *testing.T) { 152 type args struct { 153 opts []ConfigOption 154 } 155 156 opts := []ConfigOption{ 157 WithAgentId("TestAgent"), 158 } 159 160 tests := []struct { 161 name string 162 args args 163 }{ 164 {"1", args{opts}}, 165 } 166 for _, tt := range tests { 167 t.Run(tt.name, func(t *testing.T) { 168 c, err := NewConfig(tt.args.opts...) 169 err = c.checkNameAndID() 170 assert.Error(t, err, "error") 171 }) 172 } 173 } 174 175 func TestNewConfig_GenerateAgentId(t *testing.T) { 176 type args struct { 177 opts []ConfigOption 178 } 179 180 opts := []ConfigOption{ 181 WithAppName("TestApp"), 182 } 183 184 tests := []struct { 185 name string 186 args args 187 }{ 188 {"1", args{opts}}, 189 } 190 for _, tt := range tests { 191 t.Run(tt.name, func(t *testing.T) { 192 c, _ := NewConfig(tt.args.opts...) 193 c.checkNameAndID() 194 assert.Equal(t, c.String(CfgAppName), "TestApp", CfgAppName) 195 assert.NotNil(t, c.String(CfgAgentID), CfgAgentID) 196 }) 197 } 198 } 199 200 func TestNewConfig_ConfigFileYaml(t *testing.T) { 201 type args struct { 202 opts []ConfigOption 203 } 204 205 opts := []ConfigOption{ 206 WithAppName("TestApp"), 207 WithAgentId("TestAgent"), 208 WithConfigFile("example/pinpoint-config.yaml"), 209 } 210 211 tests := []struct { 212 name string 213 args args 214 }{ 215 {"1", args{opts}}, 216 } 217 for _, tt := range tests { 218 t.Run(tt.name, func(t *testing.T) { 219 c, _ := NewConfig(tt.args.opts...) 220 assert.Equal(t, "MyAppName", c.String(CfgAppName), CfgAppName) 221 assert.Equal(t, 1900, c.Int(CfgAppType), CfgAppType) 222 assert.Equal(t, "MyAgentID", c.String(CfgAgentID), CfgAgentID) 223 assert.Equal(t, "my.collector.host", c.String(CfgCollectorHost), CfgCollectorHost) 224 assert.Equal(t, 9000, c.Int(CfgCollectorAgentPort), CfgCollectorAgentPort) 225 assert.Equal(t, 9001, c.Int(CfgCollectorSpanPort), CfgCollectorSpanPort) 226 assert.Equal(t, 9002, c.Int(CfgCollectorStatPort), CfgCollectorStatPort) 227 assert.Equal(t, "debug", c.String(CfgLogLevel), CfgLogLevel) 228 assert.Equal(t, "PERCENT", c.String(CfgSamplingType), CfgSamplingType) 229 assert.Equal(t, 20, c.Int(CfgSamplingCounterRate), CfgSamplingCounterRate) 230 assert.Equal(t, 0.1, c.Float(CfgSamplingPercentRate), CfgSamplingPercentRate) 231 assert.Equal(t, 50, c.Int(CfgSamplingNewThroughput), CfgSamplingNewThroughput) 232 assert.Equal(t, 60, c.Int(CfgSamplingContinueThroughput), CfgSamplingContinueThroughput) 233 assert.Equal(t, 512, c.Int(CfgSpanQueueSize), CfgSpanQueueSize) 234 assert.Equal(t, 32, c.Int(CfgSpanMaxCallStackDepth), CfgSpanMaxCallStackDepth) 235 assert.Equal(t, 512, c.Int(CfgSpanMaxCallStackSequence), CfgSpanMaxCallStackSequence) 236 assert.Equal(t, 7000, c.Int(CfgStatCollectInterval), CfgStatCollectInterval) 237 assert.Equal(t, 10, c.Int(CfgStatBatchCount), CfgStatBatchCount) 238 assert.Equal(t, true, c.Bool(CfgIsContainerEnv), CfgIsContainerEnv) 239 assert.Equal(t, false, c.Bool(CfgSQLTraceBindValue), CfgSQLTraceBindValue) 240 assert.Equal(t, 512, c.Int(CfgSQLMaxBindValueSize), CfgSQLMaxBindValueSize) 241 assert.Equal(t, false, c.Bool(CfgSQLTraceCommit), CfgSQLTraceCommit) 242 assert.Equal(t, false, c.Bool(CfgSQLTraceRollback), CfgSQLTraceRollback) 243 assert.Equal(t, true, c.Bool(CfgSQLTraceQueryStat), CfgSQLTraceQueryStat) 244 assert.Equal(t, true, c.Bool(CfgHttpUrlStatEnable), CfgHttpUrlStatEnable) 245 assert.Equal(t, 1234, c.Int(CfgHttpUrlStatLimitSize), CfgHttpUrlStatLimitSize) 246 assert.Equal(t, true, c.Bool(CfgErrorTraceCallStack), CfgErrorTraceCallStack) 247 assert.Equal(t, 20, c.Int(CfgErrorCallStackDepth), CfgErrorCallStackDepth) 248 }) 249 } 250 } 251 252 func TestNewConfig_ConfigFileJson(t *testing.T) { 253 type args struct { 254 opts []ConfigOption 255 } 256 257 opts := []ConfigOption{ 258 WithAppName("TestApp"), 259 WithAgentId("TestAgent"), 260 WithConfigFile("example/pinpoint-config.json"), 261 } 262 263 tests := []struct { 264 name string 265 args args 266 }{ 267 {"1", args{opts}}, 268 } 269 for _, tt := range tests { 270 t.Run(tt.name, func(t *testing.T) { 271 c, _ := NewConfig(tt.args.opts...) 272 assert.Equal(t, "JsonAppName", c.String(CfgAppName), CfgAppName) 273 assert.Equal(t, 1901, c.Int(CfgAppType), CfgAppType) 274 assert.Equal(t, "JsonAgentID", c.String(CfgAgentID), CfgAgentID) 275 assert.Equal(t, "real.collector.host", c.String(CfgCollectorHost), CfgCollectorHost) 276 assert.Equal(t, 9000, c.Int(CfgCollectorAgentPort), CfgCollectorAgentPort) 277 assert.Equal(t, 9001, c.Int(CfgCollectorSpanPort), CfgCollectorSpanPort) 278 assert.Equal(t, 9002, c.Int(CfgCollectorStatPort), CfgCollectorStatPort) 279 assert.Equal(t, "debug", c.String(CfgLogLevel), CfgLogLevel) 280 assert.Equal(t, "percent", c.String(CfgSamplingType), CfgSamplingType) 281 assert.Equal(t, 20, c.Int(CfgSamplingCounterRate), CfgSamplingCounterRate) 282 assert.Equal(t, 5.5, c.Float(CfgSamplingPercentRate), CfgSamplingPercentRate) 283 assert.Equal(t, 50, c.Int(CfgSamplingNewThroughput), CfgSamplingNewThroughput) 284 assert.Equal(t, 60, c.Int(CfgSamplingContinueThroughput), CfgSamplingContinueThroughput) 285 assert.Equal(t, 1024, c.Int(CfgSpanQueueSize), CfgSpanQueueSize) 286 assert.Equal(t, 10, c.Int(CfgSpanMaxCallStackDepth), CfgSpanMaxCallStackDepth) 287 assert.Equal(t, 50, c.Int(CfgSpanMaxCallStackSequence), CfgSpanMaxCallStackSequence) 288 assert.Equal(t, 7000, c.Int(CfgStatCollectInterval), CfgStatCollectInterval) 289 assert.Equal(t, 10, c.Int(CfgStatBatchCount), CfgStatBatchCount) 290 assert.Equal(t, true, c.Bool(CfgIsContainerEnv), CfgIsContainerEnv) 291 assert.Equal(t, false, c.Bool(CfgSQLTraceBindValue), CfgSQLTraceBindValue) 292 assert.Equal(t, 256, c.Int(CfgSQLMaxBindValueSize), CfgSQLMaxBindValueSize) 293 assert.Equal(t, false, c.Bool(CfgSQLTraceCommit), CfgSQLTraceCommit) 294 assert.Equal(t, false, c.Bool(CfgSQLTraceRollback), CfgSQLTraceRollback) 295 assert.Equal(t, true, c.Bool(CfgSQLTraceQueryStat), CfgSQLTraceQueryStat) 296 assert.Equal(t, true, c.Bool(CfgHttpUrlStatEnable), CfgHttpUrlStatEnable) 297 assert.Equal(t, 10, c.Int(CfgHttpUrlStatLimitSize), CfgHttpUrlStatLimitSize) 298 assert.Equal(t, true, c.Bool(CfgErrorTraceCallStack), CfgErrorTraceCallStack) 299 assert.Equal(t, 30, c.Int(CfgErrorCallStackDepth), CfgErrorCallStackDepth) 300 }) 301 } 302 } 303 304 func TestNewConfig_ConfigFileProp(t *testing.T) { 305 type args struct { 306 opts []ConfigOption 307 } 308 309 opts := []ConfigOption{ 310 WithAppName("TestApp"), 311 WithAgentId("TestAgent"), 312 WithConfigFile("example/pinpoint-config.prop"), 313 } 314 315 tests := []struct { 316 name string 317 args args 318 }{ 319 {"1", args{opts}}, 320 } 321 for _, tt := range tests { 322 t.Run(tt.name, func(t *testing.T) { 323 c, _ := NewConfig(tt.args.opts...) 324 assert.Equal(t, "PropAppName", c.String(CfgAppName), CfgAppName) 325 assert.Equal(t, 1902, c.Int(CfgAppType), CfgAppType) 326 assert.Equal(t, "PropAgentID", c.String(CfgAgentID), CfgAgentID) 327 assert.Equal(t, "real.collector.host", c.String(CfgCollectorHost), CfgCollectorHost) 328 assert.Equal(t, 7000, c.Int(CfgCollectorAgentPort), CfgCollectorAgentPort) 329 assert.Equal(t, 7001, c.Int(CfgCollectorSpanPort), CfgCollectorSpanPort) 330 assert.Equal(t, 7002, c.Int(CfgCollectorStatPort), CfgCollectorStatPort) 331 assert.Equal(t, "debug", c.String(CfgLogLevel), CfgLogLevel) 332 assert.Equal(t, "percent", c.String(CfgSamplingType), CfgSamplingType) 333 assert.Equal(t, 20, c.Int(CfgSamplingCounterRate), CfgSamplingCounterRate) 334 assert.Equal(t, 5.5, c.Float(CfgSamplingPercentRate), CfgSamplingPercentRate) 335 assert.Equal(t, 50, c.Int(CfgSamplingNewThroughput), CfgSamplingNewThroughput) 336 assert.Equal(t, 60, c.Int(CfgSamplingContinueThroughput), CfgSamplingContinueThroughput) 337 assert.Equal(t, 1024, c.Int(CfgSpanQueueSize), CfgSpanQueueSize) 338 assert.Equal(t, 2, c.Int(CfgSpanMaxCallStackDepth), CfgSpanMaxCallStackDepth) 339 assert.Equal(t, 4, c.Int(CfgSpanMaxCallStackSequence), CfgSpanMaxCallStackSequence) 340 assert.Equal(t, 7000, c.Int(CfgStatCollectInterval), CfgStatCollectInterval) 341 assert.Equal(t, 10, c.Int(CfgStatBatchCount), CfgStatBatchCount) 342 assert.Equal(t, true, c.Bool(CfgIsContainerEnv), CfgIsContainerEnv) 343 assert.Equal(t, false, c.Bool(CfgSQLTraceBindValue), CfgSQLTraceBindValue) 344 assert.Equal(t, 128, c.Int(CfgSQLMaxBindValueSize), CfgSQLMaxBindValueSize) 345 assert.Equal(t, false, c.Bool(CfgSQLTraceCommit), CfgSQLTraceCommit) 346 assert.Equal(t, false, c.Bool(CfgSQLTraceRollback), CfgSQLTraceRollback) 347 assert.Equal(t, true, c.Bool(CfgSQLTraceQueryStat), CfgSQLTraceQueryStat) 348 assert.Equal(t, true, c.Bool(CfgHttpUrlStatEnable), CfgHttpUrlStatEnable) 349 assert.Equal(t, 10240, c.Int(CfgHttpUrlStatLimitSize), CfgHttpUrlStatLimitSize) 350 assert.Equal(t, true, c.Bool(CfgErrorTraceCallStack), CfgErrorTraceCallStack) 351 assert.Equal(t, 40, c.Int(CfgErrorCallStackDepth), CfgErrorCallStackDepth) 352 }) 353 } 354 } 355 356 func TestNewConfig_ConfigFileProfile(t *testing.T) { 357 type args struct { 358 opts []ConfigOption 359 } 360 361 opts := []ConfigOption{ 362 WithAppName("TestApp"), 363 WithAgentId("TestAgent"), 364 WithConfigFile("example/test-config.yaml"), 365 WithActiveProfile("real"), 366 } 367 368 tests := []struct { 369 name string 370 args args 371 }{ 372 {"1", args{opts}}, 373 } 374 375 oldArgs := os.Args 376 defer func() { os.Args = oldArgs }() 377 378 os.Args = []string{ 379 "pinpoint_go_agent", 380 "--pinpoint-configfile=example/pinpoint-config.yaml", 381 } 382 os.Setenv("PINPOINT_GO_ACTIVEPROFILE", "dev") 383 384 for _, tt := range tests { 385 t.Run(tt.name, func(t *testing.T) { 386 c, _ := NewConfig(tt.args.opts...) 387 assert.Equal(t, "MyAppName", c.String(CfgAppName), CfgAppName) 388 assert.Equal(t, "MyAgentID", c.String(CfgAgentID), CfgAgentID) 389 assert.Equal(t, "dev.collector.host", c.String(CfgCollectorHost), CfgCollectorHost) 390 assert.Equal(t, "COUNTER", c.String(CfgSamplingType), CfgSamplingType) 391 assert.Equal(t, 1, c.Int(CfgSamplingCounterRate), CfgSamplingCounterRate) 392 assert.Equal(t, 0.1, c.Float(CfgSamplingPercentRate), CfgSamplingPercentRate) 393 assert.Equal(t, 50, c.Int(CfgSamplingNewThroughput), CfgSamplingNewThroughput) 394 assert.Equal(t, 60, c.Int(CfgSamplingContinueThroughput), CfgSamplingContinueThroughput) 395 assert.Equal(t, 7000, c.Int(CfgStatCollectInterval), CfgStatCollectInterval) 396 assert.Equal(t, 10, c.Int(CfgStatBatchCount), CfgStatBatchCount) 397 assert.Equal(t, true, c.Bool(CfgIsContainerEnv), CfgIsContainerEnv) 398 }) 399 } 400 } 401 402 func TestNewConfig_EnvVarArg(t *testing.T) { 403 type args struct { 404 opts []ConfigOption 405 } 406 407 opts := []ConfigOption{ 408 WithAppName("TestApp"), 409 WithAgentId("TestAgent"), 410 WithConfigFile("example/test.yaml"), 411 } 412 413 tests := []struct { 414 name string 415 args args 416 }{ 417 {"1", args{opts}}, 418 } 419 420 os.Setenv("PINPOINT_GO_APPLICATIONNAME", "EnvVarArgTest") 421 os.Setenv("PINPOINT_GO_APPLICATIONTYPE", "2000") 422 os.Setenv("PINPOINT_GO_AGENTID", "envagentid") 423 os.Setenv("PINPOINT_GO_AGENTNAME", "envagentname") 424 os.Setenv("PINPOINT_GO_COLLECTOR_HOST", "env.collector.host") 425 os.Setenv("PINPOINT_GO_COLLECTOR_AGENTPORT", "8000") 426 os.Setenv("PINPOINT_GO_COLLECTOR_SPANPORT", "8100") 427 os.Setenv("PINPOINT_GO_COLLECTOR_STATPORT", "8200") 428 os.Setenv("PINPOINT_GO_SAMPLING_TYPE", "Percent") 429 os.Setenv("PINPOINT_GO_SAMPLING_PERCENTRATE", "120") 430 os.Setenv("PINPOINT_GO_SAMPLING_COUNTERRATE", "100") 431 os.Setenv("PINPOINT_GO_SAMPLING_NEWTHROUGHPUT", "100") 432 os.Setenv("PINPOINT_GO_SAMPLING_CONTINUETHROUGHPUT", "200") 433 os.Setenv("PINPOINT_GO_SPAN_QUEUESIZE", "1000") 434 os.Setenv("PINPOINT_GO_SPAN_MAXCALLSTACKDEPTH", "128") 435 os.Setenv("PINPOINT_GO_SPAN_MAXCALLSTACKSEQUENCE", "2000") 436 os.Setenv("PINPOINT_GO_STAT_COLLECTINTERVAL", "3000") 437 os.Setenv("PINPOINT_GO_STAT_BATCHCOUNT", "11") 438 os.Setenv("PINPOINT_GO_LOG_LEVEL", "trace") 439 os.Setenv("PINPOINT_GO_LOG_OUTPUT", "stdout") 440 os.Setenv("PINPOINT_GO_LOG_MAXSIZE", "50") 441 os.Setenv("PINPOINT_GO_ISCONTAINERENV", "false") 442 os.Setenv("PINPOINT_GO_SQL_TRACEBINDVALUE", "true") 443 os.Setenv("PINPOINT_GO_SQL_MAXBINDVALUESIZE", "100") 444 os.Setenv("PINPOINT_GO_SQL_TRACECOMMIT", "false") 445 os.Setenv("PINPOINT_GO_SQL_TRACEROLLBACK", "false") 446 os.Setenv("PINPOINT_GO_SQL_TRACEQUERYSTAT", "true") 447 os.Setenv("PINPOINT_GO_CONFIGFILE", "example/pinpoint-config.yaml") 448 os.Setenv("PINPOINT_GO_ENABLE", "false") 449 os.Setenv("PINPOINT_GO_HTTP_URLSTAT_ENABLE", "true") 450 os.Setenv("PINPOINT_GO_HTTP_URLSTAT_LIMITSIZE", "100") 451 os.Setenv("PINPOINT_GO_ERROR_TRACECALLSTACK", "true") 452 os.Setenv("PINPOINT_GO_ERROR_CALLSTACKDEPTH", "50") 453 454 for _, tt := range tests { 455 t.Run(tt.name, func(t *testing.T) { 456 c, _ := NewConfig(tt.args.opts...) 457 assert.Equal(t, "EnvVarArgTest", c.String(CfgAppName), CfgAppName) 458 assert.Equal(t, 2000, c.Int(CfgAppType), CfgAppType) 459 assert.Equal(t, "envagentid", c.String(CfgAgentID), CfgAgentID) 460 assert.Equal(t, "envagentname", c.String(CfgAgentName), CfgAgentName) 461 assert.Equal(t, "env.collector.host", c.String(CfgCollectorHost), CfgCollectorHost) 462 assert.Equal(t, 8000, c.Int(CfgCollectorAgentPort), CfgCollectorAgentPort) 463 assert.Equal(t, 8100, c.Int(CfgCollectorSpanPort), CfgCollectorSpanPort) 464 assert.Equal(t, 8200, c.Int(CfgCollectorStatPort), CfgCollectorStatPort) 465 assert.Equal(t, "trace", c.String(CfgLogLevel), CfgLogLevel) 466 assert.Equal(t, "stdout", c.String(CfgLogOutput), CfgLogOutput) 467 assert.Equal(t, 50, c.Int(CfgLogMaxSize), CfgLogMaxSize) 468 assert.Equal(t, "Percent", c.String(CfgSamplingType), CfgSamplingType) 469 assert.Equal(t, 100, c.Int(CfgSamplingCounterRate), CfgSamplingCounterRate) 470 assert.Equal(t, float64(120), c.Float(CfgSamplingPercentRate), CfgSamplingPercentRate) 471 assert.Equal(t, 100, c.Int(CfgSamplingNewThroughput), CfgSamplingNewThroughput) 472 assert.Equal(t, 200, c.Int(CfgSamplingContinueThroughput), CfgSamplingContinueThroughput) 473 assert.Equal(t, 1000, c.Int(CfgSpanQueueSize), CfgSpanQueueSize) 474 assert.Equal(t, 128, c.Int(CfgSpanMaxCallStackDepth), CfgSpanMaxCallStackDepth) 475 assert.Equal(t, 2000, c.Int(CfgSpanMaxCallStackSequence), CfgSpanMaxCallStackSequence) 476 assert.Equal(t, 3000, c.Int(CfgStatCollectInterval), CfgStatCollectInterval) 477 assert.Equal(t, 11, c.Int(CfgStatBatchCount), CfgStatBatchCount) 478 assert.Equal(t, false, c.Bool(CfgIsContainerEnv), CfgIsContainerEnv) 479 assert.Equal(t, true, c.Bool(CfgSQLTraceBindValue), CfgSQLTraceBindValue) 480 assert.Equal(t, 100, c.Int(CfgSQLMaxBindValueSize), CfgSQLMaxBindValueSize) 481 assert.Equal(t, false, c.Bool(CfgSQLTraceCommit), CfgSQLTraceCommit) 482 assert.Equal(t, false, c.Bool(CfgSQLTraceRollback), CfgSQLTraceRollback) 483 assert.Equal(t, true, c.Bool(CfgSQLTraceQueryStat), CfgSQLTraceQueryStat) 484 assert.Equal(t, "example/pinpoint-config.yaml", c.String(CfgConfigFile), CfgConfigFile) 485 assert.Equal(t, "dev", c.String(CfgActiveProfile), CfgActiveProfile) 486 assert.Equal(t, false, c.Bool(CfgEnable), CfgEnable) 487 assert.Equal(t, true, c.Bool(CfgHttpUrlStatEnable), CfgHttpUrlStatEnable) 488 assert.Equal(t, 100, c.Int(CfgHttpUrlStatLimitSize), CfgHttpUrlStatLimitSize) 489 assert.Equal(t, true, c.Bool(CfgErrorTraceCallStack), CfgErrorTraceCallStack) 490 assert.Equal(t, 50, c.Int(CfgErrorCallStackDepth), CfgErrorCallStackDepth) 491 }) 492 } 493 } 494 495 func TestNewConfig_CmdLineArg(t *testing.T) { 496 type args struct { 497 opts []ConfigOption 498 } 499 500 opts := []ConfigOption{ 501 WithAppName("TestApp"), 502 WithAgentId("TestAgent"), 503 WithConfigFile("example/test-config.yaml"), 504 } 505 506 tests := []struct { 507 name string 508 args args 509 }{ 510 {"1", args{opts}}, 511 } 512 513 oldArgs := os.Args 514 defer func() { os.Args = oldArgs }() 515 516 os.Args = []string{ 517 "pinpoint_go_agent", 518 "--app-arg1=1", 519 "--app-arg2=2", 520 "--pinpoint-applicationname=CmdLineArgTest", 521 "--pinpoint-applicationtype=2100", 522 "--pinpoint-agentid=cmdAgentID", 523 "--pinpoint-agentname=cmdAgentName", 524 "-app-arg3", 525 "--pinpoint-collector-host=cmd.collector.host", 526 "--pinpoint-collector-agentport=7000", 527 "--pinpoint-collector-spanport=7100", 528 "--pinpoint-collector-statport=7200", 529 "--pinpoint-sampling-type=percent", 530 "--pinpoint-sampling-percentrate=0.0001", 531 "--pinpoint-sampling-counterrate=10", 532 "--pinpoint-sampling-newthroughput=500", 533 "--pinpoint-sampling-continuethroughput=600", 534 "--pinpoint-span-queuesize=10", 535 "--pinpoint-span-maxcallstackdepth=-1", 536 "--pinpoint-span-maxcallstacksequence=-1", 537 "--pinpoint-stat-collectinterval=6000", 538 "--pinpoint-stat-batchcount=5", 539 "--pinpoint-log-level=error", 540 "--pinpoint-log-output=stdout", 541 "--pinpoint-log-maxsize=20", 542 "-app-arg4", 543 "--pinpoint-iscontainerenv=true", 544 "--pinpoint-sql-tracebindvalue=false", 545 "--pinpoint-sql-maxbindvaluesize=500", 546 "--pinpoint-sql-tracecommit=true", 547 "--pinpoint-sql-tracerollback=false", 548 "--pinpoint-sql-tracequerystat=true", 549 "--pinpoint-configfile=example/pinpoint-config.yaml", 550 "--pinpoint-activeprofile=real", 551 "--pinpoint-enable=false", 552 "--pinpoint-http-urlstat-enable=true", 553 "--pinpoint-http-urlstat-limitsize=200", 554 "--app-arg5=5", 555 "--pinpoint-error-tracecallstack=true", 556 "--pinpoint-error-callstackdepth=100", 557 } 558 559 for _, tt := range tests { 560 t.Run(tt.name, func(t *testing.T) { 561 c, _ := NewConfig(tt.args.opts...) 562 563 assert.Equal(t, "CmdLineArgTest", c.String(CfgAppName), CfgAppName) 564 assert.Equal(t, 2100, c.Int(CfgAppType), CfgAppType) 565 assert.Equal(t, "cmdAgentID", c.String(CfgAgentID), CfgAgentID) 566 assert.Equal(t, "cmdAgentName", c.String(CfgAgentName), CfgAgentName) 567 assert.Equal(t, "cmd.collector.host", c.String(CfgCollectorHost), CfgCollectorHost) 568 assert.Equal(t, 7000, c.Int(CfgCollectorAgentPort), CfgCollectorAgentPort) 569 assert.Equal(t, 7100, c.Int(CfgCollectorSpanPort), CfgCollectorSpanPort) 570 assert.Equal(t, 7200, c.Int(CfgCollectorStatPort), CfgCollectorStatPort) 571 assert.Equal(t, "error", c.String(CfgLogLevel), CfgLogLevel) 572 assert.Equal(t, "stdout", c.String(CfgLogOutput), CfgLogOutput) 573 assert.Equal(t, 20, c.Int(CfgLogMaxSize), CfgLogMaxSize) 574 assert.Equal(t, "percent", c.String(CfgSamplingType), CfgSamplingType) 575 assert.Equal(t, 10, c.Int(CfgSamplingCounterRate), CfgSamplingCounterRate) 576 assert.Equal(t, 0.0001, c.Float(CfgSamplingPercentRate), CfgSamplingPercentRate) 577 assert.Equal(t, 500, c.Int(CfgSamplingNewThroughput), CfgSamplingNewThroughput) 578 assert.Equal(t, 600, c.Int(CfgSamplingContinueThroughput), CfgSamplingContinueThroughput) 579 assert.Equal(t, 10, c.Int(CfgSpanQueueSize), CfgSpanQueueSize) 580 assert.Equal(t, math.MaxInt32, c.Int(CfgSpanMaxCallStackDepth), CfgSpanMaxCallStackDepth) 581 assert.Equal(t, math.MaxInt32, c.Int(CfgSpanMaxCallStackSequence), CfgSpanMaxCallStackSequence) 582 assert.Equal(t, 6000, c.Int(CfgStatCollectInterval), CfgStatCollectInterval) 583 assert.Equal(t, 5, c.Int(CfgStatBatchCount), CfgStatBatchCount) 584 assert.Equal(t, true, c.Bool(CfgIsContainerEnv), CfgIsContainerEnv) 585 assert.Equal(t, false, c.Bool(CfgSQLTraceBindValue), CfgSQLTraceBindValue) 586 assert.Equal(t, 500, c.Int(CfgSQLMaxBindValueSize), CfgSQLMaxBindValueSize) 587 assert.Equal(t, true, c.Bool(CfgSQLTraceCommit), CfgSQLTraceCommit) 588 assert.Equal(t, false, c.Bool(CfgSQLTraceRollback), CfgSQLTraceRollback) 589 assert.Equal(t, true, c.Bool(CfgSQLTraceQueryStat), CfgSQLTraceQueryStat) 590 assert.Equal(t, "example/pinpoint-config.yaml", c.String(CfgConfigFile), CfgConfigFile) 591 assert.Equal(t, "real", c.String(CfgActiveProfile), CfgActiveProfile) 592 assert.Equal(t, false, c.Bool(CfgEnable), CfgEnable) 593 assert.Equal(t, true, c.Bool(CfgHttpUrlStatEnable), CfgHttpUrlStatEnable) 594 assert.Equal(t, 200, c.Int(CfgHttpUrlStatLimitSize), CfgHttpUrlStatLimitSize) 595 assert.Equal(t, true, c.Bool(CfgErrorTraceCallStack), CfgErrorTraceCallStack) 596 assert.Equal(t, 100, c.Int(CfgErrorCallStackDepth), CfgErrorCallStackDepth) 597 }) 598 } 599 }