Skip to content

Commit 6e7640a

Browse files
author
Justin Hayes
committed
Make subsequent calls to withTags append the additional tags
1 parent f8563d7 commit 6e7640a

File tree

7 files changed

+142
-20
lines changed

7 files changed

+142
-20
lines changed

src/Helpers/TagNormalizer.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace JSHayes\LaravelDataDogStatsd\Helpers;
4+
5+
use Illuminate\Support\Collection;
6+
7+
class TagNormalizer
8+
{
9+
/**
10+
* Normalize the given tags to an array representation
11+
*
12+
* @param array|string|null $tags
13+
*
14+
* @return array|null
15+
*/
16+
public static function normalize($tags): ?array
17+
{
18+
if (is_string($tags)) {
19+
return (new Collection(explode(',', $tags)))->mapWithKeys(function ($tag) {
20+
[$key, $value] = array_pad(explode(':', $tag, 2), 2, null);
21+
return [$key => $value];
22+
})->toArray();
23+
}
24+
25+
return $tags;
26+
}
27+
}

src/StatBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace JSHayes\LaravelDataDogStatsd;
44

55
use DataDog\DogStatsd;
6+
use JSHayes\LaravelDataDogStatsd\Helpers\TagNormalizer;
67

78
class StatBuilder
89
{
@@ -32,7 +33,7 @@ public function __construct(DogStatsd $statsd, string $stat)
3233
*/
3334
public function withTags($tags): self
3435
{
35-
$this->tags = $tags;
36+
$this->tags = array_merge($this->tags ?? [], TagNormalizer::normalize($tags));
3637
return $this;
3738
}
3839

src/Testing/StatsCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Countable;
66
use Illuminate\Support\Collection;
77
use Illuminate\Contracts\Support\Arrayable;
8+
use JSHayes\LaravelDataDogStatsd\Helpers\TagNormalizer;
89

910
class StatsCollection implements Countable, Arrayable
1011
{
@@ -40,7 +41,7 @@ public function add(string $stat, $value, float $sampleRate = 1.0, $tags = null)
4041
'stat' => $stat,
4142
'value' => $value,
4243
'sample-rate' => $sampleRate,
43-
'tags' => $tags,
44+
'tags' => TagNormalizer::normalize($tags),
4445
]);
4546
return $this;
4647
}
@@ -93,7 +94,7 @@ public function filterByValue($value): StatsCollection
9394
*/
9495
public function filterByTags($tags): StatsCollection
9596
{
96-
return $this->filterBy('tags', $tags);
97+
return $this->filterBy('tags', TagNormalizer::normalize($tags));
9798
}
9899

99100
/**

tests/Unit/DataDogTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function it_delegates_the_fluent_timing_call_to_the_statsd_instance()
153153
$dataDog->setStatsdInstance($spy = Mockery::spy(DogStatsd::class));
154154
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->timing(10);
155155

156-
$spy->shouldHaveReceived('timing')->with('stat', 10, 0.5, 'tags');
156+
$spy->shouldHaveReceived('timing')->with('stat', 10, 0.5, ['tags' => null]);
157157
}
158158

159159
/**
@@ -165,7 +165,7 @@ public function it_delegates_the_fluent_microtiming_call_to_the_statsd_instance(
165165
$dataDog->setStatsdInstance($spy = Mockery::spy(DogStatsd::class));
166166
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->microtiming(10);
167167

168-
$spy->shouldHaveReceived('microtiming')->with('stat', 10, 0.5, 'tags');
168+
$spy->shouldHaveReceived('microtiming')->with('stat', 10, 0.5, ['tags' => null]);
169169
}
170170

171171
/**
@@ -177,7 +177,7 @@ public function it_delegates_the_fluent_gauge_call_to_the_statsd_instance()
177177
$dataDog->setStatsdInstance($spy = Mockery::spy(DogStatsd::class));
178178
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->gauge(10);
179179

180-
$spy->shouldHaveReceived('gauge')->with('stat', 10, 0.5, 'tags');
180+
$spy->shouldHaveReceived('gauge')->with('stat', 10, 0.5, ['tags' => null]);
181181
}
182182

183183
/**
@@ -189,7 +189,7 @@ public function it_delegates_the_fluent_histogram_call_to_the_statsd_instance()
189189
$dataDog->setStatsdInstance($spy = Mockery::spy(DogStatsd::class));
190190
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->histogram(10);
191191

192-
$spy->shouldHaveReceived('histogram')->with('stat', 10, 0.5, 'tags');
192+
$spy->shouldHaveReceived('histogram')->with('stat', 10, 0.5, ['tags' => null]);
193193
}
194194

195195
/**
@@ -201,7 +201,7 @@ public function it_delegates_the_fluent_distribution_call_to_the_statsd_instance
201201
$dataDog->setStatsdInstance($spy = Mockery::spy(DogStatsd::class));
202202
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->distribution(10);
203203

204-
$spy->shouldHaveReceived('distribution')->with('stat', 10, 0.5, 'tags');
204+
$spy->shouldHaveReceived('distribution')->with('stat', 10, 0.5, ['tags' => null]);
205205
}
206206

207207
/**
@@ -213,7 +213,7 @@ public function it_delegates_the_fluent_set_call_to_the_statsd_instance()
213213
$dataDog->setStatsdInstance($spy = Mockery::spy(DogStatsd::class));
214214
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->set(10);
215215

216-
$spy->shouldHaveReceived('set')->with('stat', 10, 0.5, 'tags');
216+
$spy->shouldHaveReceived('set')->with('stat', 10, 0.5, ['tags' => null]);
217217
}
218218

219219
/**
@@ -226,8 +226,8 @@ public function it_delegates_the_fluent_increment_call_to_the_statsd_instance()
226226
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->increment();
227227
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->increment(2);
228228

229-
$spy->shouldHaveReceived('increment')->with('stat', 0.5, 'tags', 1);
230-
$spy->shouldHaveReceived('increment')->with('stat', 0.5, 'tags', 2);
229+
$spy->shouldHaveReceived('increment')->with('stat', 0.5, ['tags' => null], 1);
230+
$spy->shouldHaveReceived('increment')->with('stat', 0.5, ['tags' => null], 2);
231231
}
232232

233233
/**
@@ -240,7 +240,7 @@ public function it_delegates_the_fluent_decrement_call_to_the_statsd_instance()
240240
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->decrement();
241241
$dataDog->stat('stat')->withSampleRate(0.5)->withTags('tags')->decrement(-2);
242242

243-
$spy->shouldHaveReceived('decrement')->with('stat', 0.5, 'tags', -1);
244-
$spy->shouldHaveReceived('decrement')->with('stat', 0.5, 'tags', -2);
243+
$spy->shouldHaveReceived('decrement')->with('stat', 0.5, ['tags' => null], -1);
244+
$spy->shouldHaveReceived('decrement')->with('stat', 0.5, ['tags' => null], -2);
245245
}
246246
}

tests/Unit/Facades/DataDogTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,28 @@ public function it_can_fluently_increment_a_stat()
102102

103103
DataDog::assertStatWasIncremented('stat')->withTags('tags')->withSampleRate(0.5);
104104
}
105+
106+
/**
107+
* @test
108+
*/
109+
public function it_can_fluently_append_tags()
110+
{
111+
DataDog::fake();
112+
113+
DataDog::stat('stat')
114+
->withTags('tag1')
115+
->withTags(['tag2' => null])
116+
->withTags('tag3,key4:tag4')
117+
->withTags(['key5' => 'tag5', 'key6' => 'tag6'])
118+
->increment();
119+
120+
DataDog::assertStatWasIncremented('stat')->withTags([
121+
'tag1' => null,
122+
'tag2' => null,
123+
'tag3' => null,
124+
'key4' => 'tag4',
125+
'key5' => 'tag5',
126+
'key6' => 'tag6',
127+
]);
128+
}
105129
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace JSHayes\LaravelDataDogStatsd\Tests\Unit\Helpers;
4+
5+
use JSHayes\LaravelDataDogStatsd\Tests\TestCase;
6+
use JSHayes\LaravelDataDogStatsd\Helpers\TagNormalizer;
7+
8+
class TagNormalizerTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
public function it_normalizes_string_tags_with_no_keys()
14+
{
15+
$this->assertSame(['tag1' => null], TagNormalizer::normalize('tag1'));
16+
$this->assertSame(['tag1' => null, 'tag2' => null], TagNormalizer::normalize('tag1,tag2'));
17+
}
18+
19+
/**
20+
* @test
21+
*/
22+
public function it_normalizes_string_tags_with_keys()
23+
{
24+
$this->assertSame(['key1' => 'tag1'], TagNormalizer::normalize('key1:tag1'));
25+
$this->assertSame(['key1' => 'tag1', 'key2' => 'tag2'], TagNormalizer::normalize('key1:tag1,key2:tag2'));
26+
}
27+
28+
/**
29+
* @test
30+
*/
31+
public function it_normalizes_string_tags()
32+
{
33+
$this->assertSame(['key1' => 'tag1', 'tag2' => null], TagNormalizer::normalize('key1:tag1,tag2'));
34+
}
35+
36+
/**
37+
* @test
38+
*/
39+
public function it_does_not_modify_array_tags()
40+
{
41+
$this->assertSame(['tag1' => null], TagNormalizer::normalize(['tag1' => null]));
42+
$this->assertSame(['key1' => 'tag1'], TagNormalizer::normalize(['key1' => 'tag1']));
43+
$this->assertSame(
44+
['key1' => 'tag1', 'tag2' => null],
45+
TagNormalizer::normalize(['key1' => 'tag1', 'tag2' => null])
46+
);
47+
}
48+
}

tests/Unit/Testing/StatsCollectionTest.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function adding_stats_will_add_them_to_the_collection()
1919

2020
$this->assertEquals([
2121
['stat' => 'stat1', 'value' => 'test', 'tags' => null, 'sample-rate' => 1.0],
22-
['stat' => 'stat2', 'value' => 'test', 'tags' => 'tags', 'sample-rate' => 1.0],
22+
['stat' => 'stat2', 'value' => 'test', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
2323
['stat' => 'stat3', 'value' => 'other', 'tags' => ['key' => 'value'], 'sample-rate' => 1.0],
2424
], $collection->toArray());
2525
}
@@ -36,7 +36,7 @@ public function adding_the_same_stat_multiple_times_will_add_them_to_the_collect
3636

3737
$this->assertEquals([
3838
['stat' => 'stat', 'value' => 'test', 'tags' => null, 'sample-rate' => 1.0],
39-
['stat' => 'stat', 'value' => 'test', 'tags' => 'tags', 'sample-rate' => 1.0],
39+
['stat' => 'stat', 'value' => 'test', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
4040
['stat' => 'stat', 'value' => 'other', 'tags' => ['key' => 'value'], 'sample-rate' => 1.0],
4141
], $collection->toArray());
4242
}
@@ -55,7 +55,7 @@ public function filtering_by_stat_will_remove_all_other_stats()
5555

5656
$this->assertEquals([
5757
['stat' => 'stat1', 'value' => 'test', 'tags' => null, 'sample-rate' => 1.0],
58-
['stat' => 'stat1', 'value' => 'test', 'tags' => 'tags', 'sample-rate' => 1.0],
58+
['stat' => 'stat1', 'value' => 'test', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
5959
], $collection->toArray());
6060
}
6161

@@ -73,7 +73,7 @@ public function filtering_by_value_will_remove_all_stats_with_different_values()
7373

7474
$this->assertEquals([
7575
['stat' => 'stat1', 'value' => 'test', 'tags' => null, 'sample-rate' => 1.0],
76-
['stat' => 'stat2', 'value' => 'test', 'tags' => 'tags', 'sample-rate' => 1.0],
76+
['stat' => 'stat2', 'value' => 'test', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
7777
], $collection->toArray());
7878
}
7979

@@ -110,8 +110,8 @@ public function filtering_by_tags_will_remove_all_stats_with_different_tags()
110110
$collection = $collection->filterByTags('tags');
111111

112112
$this->assertEquals([
113-
['stat' => 'stat2', 'value' => 'test', 'tags' => 'tags', 'sample-rate' => 1.0],
114-
['stat' => 'stat3', 'value' => 'another', 'tags' => 'tags', 'sample-rate' => 1.0],
113+
['stat' => 'stat2', 'value' => 'test', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
114+
['stat' => 'stat3', 'value' => 'another', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
115115
], $collection->toArray());
116116
}
117117

@@ -130,7 +130,28 @@ public function filtering_by_multiple_fields_will_return_the_correct_set()
130130
$collection = $collection->filterByStat('stat1')->filterByValue('test')->filterByTags('tags');
131131

132132
$this->assertEquals([
133-
['stat' => 'stat1', 'value' => 'test', 'tags' => 'tags', 'sample-rate' => 1.0],
133+
['stat' => 'stat1', 'value' => 'test', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
134134
], $collection->toArray());
135135
}
136+
137+
/**
138+
* @test
139+
*/
140+
public function filtering_by_tags_will_match_stats_that_have_the_same_normalized_tags()
141+
{
142+
$collection = new StatsCollection();
143+
$collection->add('stat1', 'test', 1.0, 'tags')
144+
->add('stat2', 'other', 1.0, ['key' => 'value'])
145+
->add('stat3', 'another', 1.0, ['tags' => null]);
146+
147+
$this->assertEquals([
148+
['stat' => 'stat1', 'value' => 'test', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
149+
['stat' => 'stat3', 'value' => 'another', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
150+
], $collection->filterByTags('tags')->toArray());
151+
152+
$this->assertEquals([
153+
['stat' => 'stat1', 'value' => 'test', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
154+
['stat' => 'stat3', 'value' => 'another', 'tags' => ['tags' => null], 'sample-rate' => 1.0],
155+
], $collection->filterByTags(['tags' => null])->toArray());
156+
}
136157
}

0 commit comments

Comments
 (0)