|
| 1 | +package flood |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/spf13/cobra" |
| 5 | + "hash/fnv" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/DataDog/datadog-go/v5/statsd" |
| 12 | +) |
| 13 | + |
| 14 | +type client struct { |
| 15 | + client *statsd.Client |
| 16 | + pointsPer10Seconds int |
| 17 | + sendAtStartOfBucket bool |
| 18 | +} |
| 19 | + |
| 20 | +func initClient(command *cobra.Command) (*client, error) { |
| 21 | + var options []statsd.Option |
| 22 | + |
| 23 | + tags := []string{} |
| 24 | + |
| 25 | + b, err := command.Flags().GetBool("client-side-aggregation") |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + if b { |
| 30 | + options = append(options, statsd.WithClientSideAggregation()) |
| 31 | + } else { |
| 32 | + options = append(options, statsd.WithoutClientSideAggregation()) |
| 33 | + } |
| 34 | + tags = append(tags, "client-side-aggregation:"+strconv.FormatBool(b)) |
| 35 | + |
| 36 | + b, err = command.Flags().GetBool("extended-client-side-aggregation") |
| 37 | + if err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + if b { |
| 41 | + options = append(options, statsd.WithExtendedClientSideAggregation()) |
| 42 | + } |
| 43 | + tags = append(tags, "extended-client-side-aggregation"+strconv.FormatBool(b)) |
| 44 | + |
| 45 | + b, err = command.Flags().GetBool("channel-mode") |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + if b { |
| 50 | + options = append(options, statsd.WithChannelMode()) |
| 51 | + } |
| 52 | + tags = append(tags, "channel-mode:"+strconv.FormatBool(b)) |
| 53 | + |
| 54 | + i, err := command.Flags().GetInt("channel-mode-buffer-size") |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + options = append(options, statsd.WithChannelModeBufferSize(i)) |
| 59 | + tags = append(tags, "channel-mode-buffer-size:"+strconv.Itoa(i)) |
| 60 | + |
| 61 | + i, err = command.Flags().GetInt("sender-queue-size") |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + options = append(options, statsd.WithSenderQueueSize(i)) |
| 66 | + tags = append(tags, "sender-queue-size:"+strconv.Itoa(i)) |
| 67 | + |
| 68 | + d, err := command.Flags().GetDuration("buffer-flush-interval") |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + options = append(options, statsd.WithBufferFlushInterval(d)) |
| 73 | + tags = append(tags, "buffer-flush-interval:"+d.String()) |
| 74 | + |
| 75 | + d, err = command.Flags().GetDuration("write-timeout") |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + options = append(options, statsd.WithWriteTimeout(d)) |
| 80 | + tags = append(tags, "write-timeout:"+d.String()) |
| 81 | + |
| 82 | + pointsPer10Seconds, err := command.Flags().GetInt("points-per-10seconds") |
| 83 | + if err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + tags = append(tags, "points-per-10seconds:"+strconv.Itoa(pointsPer10Seconds)) |
| 87 | + |
| 88 | + sendAtStart, err := command.Flags().GetBool("send-at-start-of-bucket") |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + tags = append(tags, "send-at-start-of-bucket:"+strconv.FormatBool(sendAtStart)) |
| 93 | + |
| 94 | + address, err := command.Flags().GetString("address") |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + tags = append(tags, "address:"+address) |
| 99 | + |
| 100 | + transport := "udp" |
| 101 | + if strings.HasPrefix(address, statsd.UnixAddressPrefix) { |
| 102 | + transport = "unix" |
| 103 | + } |
| 104 | + tags = append(tags, "transport:"+transport) |
| 105 | + |
| 106 | + telemetryAddress, err := command.Flags().GetString("telemetry-address") |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + if telemetryAddress == "" { |
| 111 | + telemetryAddress = address |
| 112 | + } |
| 113 | + tags = append(tags, "telemetry-address:"+telemetryAddress) |
| 114 | + options = append(options, statsd.WithTelemetryAddr(telemetryAddress)) |
| 115 | + |
| 116 | + telemetryTransport := "udp" |
| 117 | + if strings.HasPrefix(telemetryAddress, statsd.UnixAddressPrefix) { |
| 118 | + telemetryTransport = "unix" |
| 119 | + } |
| 120 | + tags = append(tags, "telemetry-transport:"+telemetryTransport) |
| 121 | + |
| 122 | + t, err := command.Flags().GetStringSlice("tags") |
| 123 | + tags = append(tags, t...) |
| 124 | + h := hash(tags) |
| 125 | + tags = append(tags, "client-hash:"+strconv.Itoa(int(h))) |
| 126 | + |
| 127 | + options = append(options, statsd.WithTags(tags)) |
| 128 | + |
| 129 | + log.Printf("Tags: %v - Hash: %x", tags, h) |
| 130 | + |
| 131 | + options = append(options, statsd.WithOriginDetection()) |
| 132 | + |
| 133 | + c, err := statsd.New(address, options...) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + |
| 138 | + return &client{ |
| 139 | + client: c, |
| 140 | + pointsPer10Seconds: pointsPer10Seconds, |
| 141 | + sendAtStartOfBucket: sendAtStart, |
| 142 | + }, nil |
| 143 | +} |
| 144 | + |
| 145 | +func hash(s []string) uint32 { |
| 146 | + h := fnv.New32a() |
| 147 | + for _, e := range s { |
| 148 | + h.Write([]byte(e)) |
| 149 | + } |
| 150 | + return h.Sum32() |
| 151 | +} |
| 152 | + |
| 153 | +func Flood(command *cobra.Command, args []string) { |
| 154 | + c, err := initClient(command) |
| 155 | + if err != nil { |
| 156 | + log.Fatal(err) |
| 157 | + } |
| 158 | + log.Printf("Sending %d points per 10 seconds", c.pointsPer10Seconds) |
| 159 | + |
| 160 | + for { |
| 161 | + t1 := time.Now() |
| 162 | + |
| 163 | + for sent := 0; sent < c.pointsPer10Seconds; sent++ { |
| 164 | + err := c.client.Incr("flood.dogstatsd.count", []string{}, 1) |
| 165 | + if err != nil { |
| 166 | + log.Printf("Error: %v", err) |
| 167 | + } |
| 168 | + if !c.sendAtStartOfBucket { |
| 169 | + time.Sleep(time.Duration(8) * time.Second / time.Duration(c.pointsPer10Seconds)) |
| 170 | + } |
| 171 | + } |
| 172 | + err := c.client.Count("flood.dogstatsd.expected", int64(c.pointsPer10Seconds), []string{}, 1) |
| 173 | + if err != nil { |
| 174 | + log.Printf("Error: %v", err) |
| 175 | + } |
| 176 | + |
| 177 | + t2 := time.Now() |
| 178 | + |
| 179 | + duration := t2.Sub(t1) |
| 180 | + s := time.Duration(10)*time.Second - duration |
| 181 | + if s > 0 { |
| 182 | + // Sleep until the next bucket |
| 183 | + log.Printf("Sleeping for %f seconds", s.Seconds()) |
| 184 | + time.Sleep(s) |
| 185 | + } else { |
| 186 | + log.Printf("We're %f seconds behind", s.Seconds()*-1) |
| 187 | + } |
| 188 | + } |
| 189 | + c.client.Close() |
| 190 | +} |
0 commit comments