Skip to content

Commit 1409f23

Browse files
authored
Rollback capture with empty UUID (#71)
1 parent f37d6f7 commit 1409f23

12 files changed

+46
-60
lines changed

README.md

-12
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,6 @@ func main() {
6262
Properties: posthog.NewProperties().
6363
Set("$current_url", "https://example.com"),
6464
})
65-
66-
// Capture event with calculated uuid to deduplicate repeated events.
67-
// The library github.com/google/uuid is used
68-
key := myEvent.Id + myEvent.Project
69-
uid := uuid.NewSHA1(uuid.NameSpaceX500, []byte(key)).String()
70-
client.Enqueue(posthog.Capture{
71-
Uuid: uid,
72-
DistinctId: "test-user",
73-
Event: "$pageview",
74-
Properties: posthog.NewProperties().
75-
Set("$current_url", "https://example.com"),
76-
})
7765

7866
// Check if a feature flag is enabled
7967
isMyFlagEnabled, err := client.IsFeatureEnabled(

capture.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ type Capture struct {
99
// This field is exported for serialization purposes and shouldn't be set by
1010
// the application, its value is always overwritten by the library.
1111
Type string
12-
// You don't usually need to specify this field - Posthog will generate it automatically.
13-
// Use it only when necessary - for example, to prevent duplicate events.
14-
Uuid string
12+
1513
DistinctId string
1614
Event string
1715
Timestamp time.Time
@@ -46,7 +44,6 @@ func (msg Capture) Validate() error {
4644

4745
type CaptureInApi struct {
4846
Type string `json:"type"`
49-
Uuid string `json:"uuid"`
5047
Library string `json:"library"`
5148
LibraryVersion string `json:"library_version"`
5249
Timestamp time.Time `json:"timestamp"`
@@ -75,7 +72,6 @@ func (msg Capture) APIfy() APIMessage {
7572

7673
apified := CaptureInApi{
7774
Type: msg.Type,
78-
Uuid: msg.Uuid,
7975
Library: library,
8076
LibraryVersion: libraryVersion,
8177
Timestamp: msg.Timestamp,

config.go

+19
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package posthog
33
import (
44
"net/http"
55
"time"
6+
7+
"github.com/google/uuid"
68
)
79

810
// Instances of this type carry the different configuration options that may
@@ -77,6 +79,12 @@ type Config struct {
7779
// If not set the client will fallback to use a default retry policy.
7880
RetryAfter func(int) time.Duration
7981

82+
// A function called by the client to generate unique message identifiers.
83+
// The client uses a UUID generator if none is provided.
84+
// This field is not exported and only exposed internally to let unit tests
85+
// mock the id generation.
86+
uid func() string
87+
8088
// A function called by the client to get the current time, `time.Now` is
8189
// used by default.
8290
// This field is not exported and only exposed internally to let unit tests
@@ -165,6 +173,10 @@ func makeConfig(c Config) Config {
165173
c.RetryAfter = DefaultBacko().Duration
166174
}
167175

176+
if c.uid == nil {
177+
c.uid = uid
178+
}
179+
168180
if c.now == nil {
169181
c.now = time.Now
170182
}
@@ -175,3 +187,10 @@ func makeConfig(c Config) Config {
175187

176188
return c
177189
}
190+
191+
// This function returns a string representation of a UUID, it's the default
192+
// function used for generating unique IDs.
193+
func uid() string {
194+
new_uuid, _ := uuid.NewRandom()
195+
return new_uuid.String()
196+
}

fixtures/test-enqueue-capture-with-uuid.json

-22
This file was deleted.

fixtures/test-enqueue-capture.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
},
1616
"send_feature_flags": false,
1717
"timestamp": "2009-11-10T23:00:00Z",
18-
"type": "capture",
19-
"uuid": ""
18+
"type": "capture"
2019
}
2120
]
2221
}

fixtures/test-interval-capture.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
},
1616
"send_feature_flags": false,
1717
"timestamp": "2009-11-10T23:00:00Z",
18-
"type": "capture",
19-
"uuid": ""
18+
"type": "capture"
2019
}
2120
]
2221
}

fixtures/test-many-capture.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
},
1515
"send_feature_flags": false,
1616
"timestamp": "2009-11-10T23:00:00Z",
17-
"type": "capture",
18-
"uuid": ""
17+
"type": "capture"
1918
},
2019
{
2120
"distinct_id": "123456",
@@ -30,8 +29,7 @@
3029
},
3130
"send_feature_flags": false,
3231
"timestamp": "2009-11-10T23:00:00Z",
33-
"type": "capture",
34-
"uuid": ""
32+
"type": "capture"
3533
},
3634
{
3735
"distinct_id": "123456",
@@ -46,8 +44,7 @@
4644
},
4745
"send_feature_flags": false,
4846
"timestamp": "2009-11-10T23:00:00Z",
49-
"type": "capture",
50-
"uuid": ""
47+
"type": "capture"
5148
}
5249
]
5350
}

fixtures/test-merge-capture.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
},
1717
"send_feature_flags": false,
1818
"timestamp": "2015-07-10T23:00:00Z",
19-
"type": "capture",
20-
"uuid": ""
19+
"type": "capture"
2120
}
2221
]
2322
}

fixtures/test-timestamp-capture.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
},
1616
"send_feature_flags": false,
1717
"timestamp": "2015-07-10T23:00:00Z",
18-
"type": "capture",
19-
"uuid": ""
18+
"type": "capture"
2019
}
2120
]
2221
}

go.mod

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/posthog/posthog-go
22

33
go 1.18
44

5-
require github.com/urfave/cli v1.22.5
5+
require (
6+
github.com/google/uuid v1.3.0
7+
github.com/urfave/cli v1.22.5
8+
)
69

710
require (
811
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect

go.sum

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
22
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
33
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
4+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
5+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
46
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
57
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
68
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
@@ -10,4 +12,4 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
1012
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
1113
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
1214
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
13-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
15+
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

posthog_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func fixture(name string) string {
152152
return string(b)
153153
}
154154

155+
func mockId() string { return "I'm unique" }
156+
155157
func mockTime() time.Time {
156158
// time.Unix(0, 0) fails on Circle
157159
return time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
@@ -189,11 +191,11 @@ func ExampleCapture() {
189191
Endpoint: server.URL,
190192
BatchSize: 1,
191193
now: mockTime,
194+
uid: mockId,
192195
})
193196
defer client.Close()
194197

195198
client.Enqueue(Capture{
196-
Uuid: "00000000-0000-0000-0000-000000000000",
197199
Event: "Download",
198200
DistinctId: "123456",
199201
Properties: Properties{
@@ -223,8 +225,7 @@ func ExampleCapture() {
223225
// },
224226
// "send_feature_flags": false,
225227
// "timestamp": "2009-11-10T23:00:00Z",
226-
// "type": "capture",
227-
// "uuid": "00000000-0000-0000-0000-000000000000"
228+
// "type": "capture"
228229
// }
229230
// ]
230231
// }
@@ -245,6 +246,7 @@ func TestCaptureNoProperties(t *testing.T) {
245246
Endpoint: server.URL,
246247
BatchSize: 1,
247248
now: mockTime,
249+
uid: mockId,
248250
DefaultEventProperties: NewProperties().Set("service", "api"),
249251
})
250252
defer client.Close()
@@ -321,9 +323,8 @@ func TestEnqueue(t *testing.T) {
321323
},
322324

323325
"*capture": {
324-
strings.TrimSpace(fixture("test-enqueue-capture-with-uuid.json")),
326+
strings.TrimSpace(fixture("test-enqueue-capture.json")),
325327
&Capture{
326-
Uuid: "11111111-1111-1111-1111-111111111111",
327328
Event: "Download",
328329
DistinctId: "123456",
329330
Properties: Properties{
@@ -345,6 +346,7 @@ func TestEnqueue(t *testing.T) {
345346
Logger: t,
346347
BatchSize: 1,
347348
now: mockTime,
349+
uid: mockId,
348350
})
349351
defer client.Close()
350352

@@ -402,6 +404,7 @@ func TestCaptureWithInterval(t *testing.T) {
402404
Verbose: true,
403405
Logger: t,
404406
now: mockTime,
407+
uid: mockId,
405408
})
406409
defer client.Close()
407410

@@ -438,6 +441,7 @@ func TestCaptureWithTimestamp(t *testing.T) {
438441
Logger: t,
439442
BatchSize: 1,
440443
now: mockTime,
444+
uid: mockId,
441445
})
442446
defer client.Close()
443447

@@ -471,6 +475,7 @@ func TestCaptureWithDefaultProperties(t *testing.T) {
471475
Logger: t,
472476
BatchSize: 1,
473477
now: mockTime,
478+
uid: mockId,
474479
})
475480
defer client.Close()
476481

@@ -503,6 +508,7 @@ func TestCaptureMany(t *testing.T) {
503508
Logger: t,
504509
BatchSize: 3,
505510
now: mockTime,
511+
uid: mockId,
506512
})
507513
defer client.Close()
508514

@@ -1705,6 +1711,7 @@ func TestCaptureSendFlags(t *testing.T) {
17051711
Logger: t,
17061712
BatchSize: 1,
17071713
now: mockTime,
1714+
uid: mockId,
17081715

17091716
PersonalApiKey: "some very secret key",
17101717
})

0 commit comments

Comments
 (0)