|
| 1 | +// Copyright 2020 New Relic Corporation. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package nrredis |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "net" |
| 9 | + "testing" |
| 10 | + |
| 11 | + redis "github.com/go-redis/redis/v8" |
| 12 | + "github.com/newrelic/go-agent/v3/internal" |
| 13 | + "github.com/newrelic/go-agent/v3/internal/integrationsupport" |
| 14 | + newrelic "github.com/newrelic/go-agent/v3/newrelic" |
| 15 | +) |
| 16 | + |
| 17 | +func emptyDialer(context.Context, string, string) (net.Conn, error) { |
| 18 | + return &net.TCPConn{}, nil |
| 19 | +} |
| 20 | + |
| 21 | +func TestPing(t *testing.T) { |
| 22 | + opts := &redis.Options{ |
| 23 | + Dialer: emptyDialer, |
| 24 | + Addr: "myhost:myport", |
| 25 | + } |
| 26 | + client := redis.NewClient(opts) |
| 27 | + |
| 28 | + app := integrationsupport.NewTestApp(nil, nil) |
| 29 | + txn := app.StartTransaction("txnName") |
| 30 | + ctx := newrelic.NewContext(context.Background(), txn) |
| 31 | + |
| 32 | + client.AddHook(NewHook(nil)) |
| 33 | + client.WithContext(ctx).Ping(ctx) |
| 34 | + txn.End() |
| 35 | + |
| 36 | + app.ExpectMetrics(t, []internal.WantMetric{ |
| 37 | + {Name: "OtherTransaction/Go/txnName", Forced: nil}, |
| 38 | + {Name: "OtherTransactionTotalTime/Go/txnName", Forced: nil}, |
| 39 | + {Name: "OtherTransaction/all", Forced: nil}, |
| 40 | + {Name: "OtherTransactionTotalTime", Forced: nil}, |
| 41 | + {Name: "Datastore/all", Forced: nil}, |
| 42 | + {Name: "Datastore/allOther", Forced: nil}, |
| 43 | + {Name: "Datastore/Redis/all", Forced: nil}, |
| 44 | + {Name: "Datastore/Redis/allOther", Forced: nil}, |
| 45 | + {Name: "Datastore/operation/Redis/ping", Forced: nil}, |
| 46 | + {Name: "Datastore/operation/Redis/ping", Scope: "OtherTransaction/Go/txnName", Forced: nil}, |
| 47 | + }) |
| 48 | +} |
| 49 | + |
| 50 | +func TestPingWithOptionsAndAddress(t *testing.T) { |
| 51 | + opts := &redis.Options{ |
| 52 | + Dialer: emptyDialer, |
| 53 | + Addr: "myhost:myport", |
| 54 | + } |
| 55 | + client := redis.NewClient(opts) |
| 56 | + |
| 57 | + app := integrationsupport.NewTestApp(nil, nil) |
| 58 | + txn := app.StartTransaction("txnName") |
| 59 | + ctx := newrelic.NewContext(context.Background(), txn) |
| 60 | + |
| 61 | + client.AddHook(NewHook(opts)) |
| 62 | + client.WithContext(ctx).Ping(ctx) |
| 63 | + txn.End() |
| 64 | + |
| 65 | + app.ExpectMetrics(t, []internal.WantMetric{ |
| 66 | + {Name: "OtherTransaction/Go/txnName", Forced: nil}, |
| 67 | + {Name: "OtherTransactionTotalTime/Go/txnName", Forced: nil}, |
| 68 | + {Name: "OtherTransaction/all", Forced: nil}, |
| 69 | + {Name: "OtherTransactionTotalTime", Forced: nil}, |
| 70 | + {Name: "Datastore/all", Forced: nil}, |
| 71 | + {Name: "Datastore/allOther", Forced: nil}, |
| 72 | + {Name: "Datastore/Redis/all", Forced: nil}, |
| 73 | + {Name: "Datastore/Redis/allOther", Forced: nil}, |
| 74 | + {Name: "Datastore/instance/Redis/myhost/myport", Forced: nil}, |
| 75 | + {Name: "Datastore/operation/Redis/ping", Forced: nil}, |
| 76 | + {Name: "Datastore/operation/Redis/ping", Scope: "OtherTransaction/Go/txnName", Forced: nil}, |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func TestPipelineOperation(t *testing.T) { |
| 81 | + // As of Jan 16, 2020, it is impossible to test pipeline operations using |
| 82 | + // a &net.TCPConn{}, so we will have to make do with this. |
| 83 | + if op := pipelineOperation(nil); op != "pipeline:" { |
| 84 | + t.Error(op) |
| 85 | + } |
| 86 | + ctx := context.Background() |
| 87 | + cmds := []redis.Cmder{redis.NewCmd(ctx, "GET"), redis.NewCmd(ctx, "SET")} |
| 88 | + if op := pipelineOperation(cmds); op != "pipeline:get,set" { |
| 89 | + t.Error(op) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestNewHookAddress(t *testing.T) { |
| 94 | + testcases := []struct { |
| 95 | + network string |
| 96 | + address string |
| 97 | + expHost string |
| 98 | + expPort string |
| 99 | + }{ |
| 100 | + // examples from net.Dial https://godoc.org/net#Dial |
| 101 | + { |
| 102 | + network: "tcp", |
| 103 | + address: "golang.org:http", |
| 104 | + expHost: "golang.org", |
| 105 | + expPort: "http", |
| 106 | + }, |
| 107 | + { |
| 108 | + network: "", // tcp is assumed if missing |
| 109 | + address: "golang.org:http", |
| 110 | + expHost: "golang.org", |
| 111 | + expPort: "http", |
| 112 | + }, |
| 113 | + { |
| 114 | + network: "tcp", |
| 115 | + address: "192.0.2.1:http", |
| 116 | + expHost: "192.0.2.1", |
| 117 | + expPort: "http", |
| 118 | + }, |
| 119 | + { |
| 120 | + network: "tcp", |
| 121 | + address: "198.51.100.1:80", |
| 122 | + expHost: "198.51.100.1", |
| 123 | + expPort: "80", |
| 124 | + }, |
| 125 | + { |
| 126 | + network: "tcp", |
| 127 | + address: ":80", |
| 128 | + expHost: "localhost", |
| 129 | + expPort: "80", |
| 130 | + }, |
| 131 | + { |
| 132 | + network: "tcp", |
| 133 | + address: "0.0.0.0:80", |
| 134 | + expHost: "0.0.0.0", |
| 135 | + expPort: "80", |
| 136 | + }, |
| 137 | + { |
| 138 | + network: "tcp", |
| 139 | + address: "[::]:80", |
| 140 | + expHost: "::", |
| 141 | + expPort: "80", |
| 142 | + }, |
| 143 | + { |
| 144 | + network: "unix", |
| 145 | + address: "path/to/socket", |
| 146 | + expHost: "localhost", |
| 147 | + expPort: "path/to/socket", |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + for _, tc := range testcases { |
| 152 | + t.Run(tc.network+","+tc.address, func(t *testing.T) { |
| 153 | + hk := NewHook(&redis.Options{ |
| 154 | + Network: tc.network, |
| 155 | + Addr: tc.address, |
| 156 | + }).(hook) |
| 157 | + |
| 158 | + if hk.segment.Host != tc.expHost { |
| 159 | + t.Errorf("incorrect host: expect=%s actual=%s", |
| 160 | + tc.expHost, hk.segment.Host) |
| 161 | + } |
| 162 | + if hk.segment.PortPathOrID != tc.expPort { |
| 163 | + t.Errorf("incorrect port: expect=%s actual=%s", |
| 164 | + tc.expPort, hk.segment.PortPathOrID) |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | +} |
0 commit comments