|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "net"
|
| 7 | + "strings" |
7 | 8 | "testing"
|
8 | 9 |
|
9 | 10 | "go.opentelemetry.io/otel/attribute"
|
@@ -222,6 +223,169 @@ func TestTracingHook_ProcessPipelineHook(t *testing.T) {
|
222 | 223 | }
|
223 | 224 | }
|
224 | 225 |
|
| 226 | +func TestTracingHook_ProcessHook_LongCommand(t *testing.T) { |
| 227 | + imsb := tracetest.NewInMemoryExporter() |
| 228 | + provider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(imsb)) |
| 229 | + hook := newTracingHook( |
| 230 | + "redis://localhost:6379", |
| 231 | + WithTracerProvider(provider), |
| 232 | + ) |
| 233 | + longValue := strings.Repeat("a", 102400) |
| 234 | + |
| 235 | + tests := []struct { |
| 236 | + name string |
| 237 | + cmd redis.Cmder |
| 238 | + expected string |
| 239 | + }{ |
| 240 | + { |
| 241 | + name: "short command", |
| 242 | + cmd: redis.NewCmd(context.Background(), "SET", "key", "value"), |
| 243 | + expected: "SET key value", |
| 244 | + }, |
| 245 | + { |
| 246 | + name: "set command with long key", |
| 247 | + cmd: redis.NewCmd(context.Background(), "SET", longValue, "value"), |
| 248 | + expected: "SET " + longValue + " value", |
| 249 | + }, |
| 250 | + { |
| 251 | + name: "set command with long value", |
| 252 | + cmd: redis.NewCmd(context.Background(), "SET", "key", longValue), |
| 253 | + expected: "SET key " + longValue, |
| 254 | + }, |
| 255 | + { |
| 256 | + name: "set command with long key and value", |
| 257 | + cmd: redis.NewCmd(context.Background(), "SET", longValue, longValue), |
| 258 | + expected: "SET " + longValue + " " + longValue, |
| 259 | + }, |
| 260 | + { |
| 261 | + name: "short command with many arguments", |
| 262 | + cmd: redis.NewCmd(context.Background(), "MSET", "key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4", "key5", "value5"), |
| 263 | + expected: "MSET key1 value1 key2 value2 key3 value3 key4 value4 key5 value5", |
| 264 | + }, |
| 265 | + { |
| 266 | + name: "long command", |
| 267 | + cmd: redis.NewCmd(context.Background(), longValue, "key", "value"), |
| 268 | + expected: longValue + " key value", |
| 269 | + }, |
| 270 | + } |
| 271 | + |
| 272 | + for _, tt := range tests { |
| 273 | + t.Run(tt.name, func(t *testing.T) { |
| 274 | + defer imsb.Reset() |
| 275 | + |
| 276 | + processHook := hook.ProcessHook(func(ctx context.Context, cmd redis.Cmder) error { |
| 277 | + return nil |
| 278 | + }) |
| 279 | + |
| 280 | + if err := processHook(context.Background(), tt.cmd); err != nil { |
| 281 | + t.Fatal(err) |
| 282 | + } |
| 283 | + |
| 284 | + assertEqual(t, 1, len(imsb.GetSpans())) |
| 285 | + |
| 286 | + spanData := imsb.GetSpans()[0] |
| 287 | + |
| 288 | + var dbStatement string |
| 289 | + for _, attr := range spanData.Attributes { |
| 290 | + if attr.Key == semconv.DBStatementKey { |
| 291 | + dbStatement = attr.Value.AsString() |
| 292 | + break |
| 293 | + } |
| 294 | + } |
| 295 | + |
| 296 | + if dbStatement != tt.expected { |
| 297 | + t.Errorf("Expected DB statement: %q\nGot: %q", tt.expected, dbStatement) |
| 298 | + } |
| 299 | + }) |
| 300 | + } |
| 301 | +} |
| 302 | + |
| 303 | +func TestTracingHook_ProcessPipelineHook_LongCommands(t *testing.T) { |
| 304 | + imsb := tracetest.NewInMemoryExporter() |
| 305 | + provider := sdktrace.NewTracerProvider(sdktrace.WithSyncer(imsb)) |
| 306 | + hook := newTracingHook( |
| 307 | + "redis://localhost:6379", |
| 308 | + WithTracerProvider(provider), |
| 309 | + ) |
| 310 | + |
| 311 | + tests := []struct { |
| 312 | + name string |
| 313 | + cmds []redis.Cmder |
| 314 | + expected string |
| 315 | + }{ |
| 316 | + { |
| 317 | + name: "multiple short commands", |
| 318 | + cmds: []redis.Cmder{ |
| 319 | + redis.NewCmd(context.Background(), "SET", "key1", "value1"), |
| 320 | + redis.NewCmd(context.Background(), "SET", "key2", "value2"), |
| 321 | + }, |
| 322 | + expected: "SET key1 value1\nSET key2 value2", |
| 323 | + }, |
| 324 | + { |
| 325 | + name: "multiple short commands with long key", |
| 326 | + cmds: []redis.Cmder{ |
| 327 | + redis.NewCmd(context.Background(), "SET", strings.Repeat("a", 102400), "value1"), |
| 328 | + redis.NewCmd(context.Background(), "SET", strings.Repeat("b", 102400), "value2"), |
| 329 | + }, |
| 330 | + expected: "SET " + strings.Repeat("a", 102400) + " value1\nSET " + strings.Repeat("b", 102400) + " value2", |
| 331 | + }, |
| 332 | + { |
| 333 | + name: "multiple short commands with long value", |
| 334 | + cmds: []redis.Cmder{ |
| 335 | + redis.NewCmd(context.Background(), "SET", "key1", strings.Repeat("a", 102400)), |
| 336 | + redis.NewCmd(context.Background(), "SET", "key2", strings.Repeat("b", 102400)), |
| 337 | + }, |
| 338 | + expected: "SET key1 " + strings.Repeat("a", 102400) + "\nSET key2 " + strings.Repeat("b", 102400), |
| 339 | + }, |
| 340 | + { |
| 341 | + name: "multiple short commands with long key and value", |
| 342 | + cmds: []redis.Cmder{ |
| 343 | + redis.NewCmd(context.Background(), "SET", strings.Repeat("a", 102400), strings.Repeat("b", 102400)), |
| 344 | + redis.NewCmd(context.Background(), "SET", strings.Repeat("c", 102400), strings.Repeat("d", 102400)), |
| 345 | + }, |
| 346 | + expected: "SET " + strings.Repeat("a", 102400) + " " + strings.Repeat("b", 102400) + "\nSET " + strings.Repeat("c", 102400) + " " + strings.Repeat("d", 102400), |
| 347 | + }, |
| 348 | + { |
| 349 | + name: "multiple long commands", |
| 350 | + cmds: []redis.Cmder{ |
| 351 | + redis.NewCmd(context.Background(), strings.Repeat("a", 102400), "key1", "value1"), |
| 352 | + redis.NewCmd(context.Background(), strings.Repeat("a", 102400), "key2", "value2"), |
| 353 | + }, |
| 354 | + expected: strings.Repeat("a", 102400) + " key1 value1\n" + strings.Repeat("a", 102400) + " key2 value2", |
| 355 | + }, |
| 356 | + } |
| 357 | + |
| 358 | + for _, tt := range tests { |
| 359 | + t.Run(tt.name, func(t *testing.T) { |
| 360 | + defer imsb.Reset() |
| 361 | + |
| 362 | + processHook := hook.ProcessPipelineHook(func(ctx context.Context, cmds []redis.Cmder) error { |
| 363 | + return nil |
| 364 | + }) |
| 365 | + |
| 366 | + if err := processHook(context.Background(), tt.cmds); err != nil { |
| 367 | + t.Fatal(err) |
| 368 | + } |
| 369 | + |
| 370 | + assertEqual(t, 1, len(imsb.GetSpans())) |
| 371 | + |
| 372 | + spanData := imsb.GetSpans()[0] |
| 373 | + |
| 374 | + var dbStatement string |
| 375 | + for _, attr := range spanData.Attributes { |
| 376 | + if attr.Key == semconv.DBStatementKey { |
| 377 | + dbStatement = attr.Value.AsString() |
| 378 | + break |
| 379 | + } |
| 380 | + } |
| 381 | + |
| 382 | + if dbStatement != tt.expected { |
| 383 | + t.Errorf("Expected DB statement:\n%q\nGot:\n%q", tt.expected, dbStatement) |
| 384 | + } |
| 385 | + }) |
| 386 | + } |
| 387 | +} |
| 388 | + |
225 | 389 | func assertEqual(t *testing.T, expected, actual interface{}) {
|
226 | 390 | t.Helper()
|
227 | 391 | if expected != actual {
|
|
0 commit comments