Skip to content

Commit

Permalink
Add ability to set custom IP Address (closes #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeemster committed Sep 5, 2018
1 parent 17ca1c8 commit b555b59
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ where:
* `--sdjson` is a self-describing JSON of the standard form `{ "schema": "iglu:...", "data": { ... } }`
* `--schema` is a schema URI, most likely of the form `iglu:...`
* `--json` is a (non-self-describing) JSON, of the form `{ ... }`
* `--ipaddress` is optional. It defaults to an empty string

The idea here is that you can either send in a [**self-describing JSON**][sd-json], or pass in the constituent parts (i.e. a regular JSON plus a schema URI) and the Snowplow Tracking CLI will construct the final self-describing JSON for you.

Expand Down
13 changes: 11 additions & 2 deletions snowplowtrk.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func main() {
Name: "json, j",
Usage: "Non-SelfDescribing JSON, of the form { ... }",
},
cli.StringFlag{
Name: "ipaddress, ip",
Usage: "Track a custom IP Address (Optional)",
Value: "",
},
}

// Set CLI Action
Expand All @@ -97,6 +102,7 @@ func main() {
sdjson := c.String("sdjson")
schema := c.String("schema")
jsonData := c.String("json")
ipAddress := c.String("ipaddress")

// Check that collector domain exists
if collector == "" {
Expand All @@ -113,7 +119,7 @@ func main() {
trackerChan := make(chan int, 1)

// Send the event
tracker := initTracker(collector, appid, method, protocol, trackerChan)
tracker := initTracker(collector, appid, method, protocol, ipAddress, trackerChan)
statusCode := trackSelfDescribingEvent(tracker, trackerChan, sdj)

// Parse return code
Expand Down Expand Up @@ -162,7 +168,7 @@ func getSdJSON(sdjson string, schema string, jsonData string) (*gt.SelfDescribin

// initTracker creates a new Tracker ready for use
// by the application.
func initTracker(collector string, appid string, method string, protocol string, trackerChan chan int) *gt.Tracker {
func initTracker(collector string, appid string, method string, protocol string, ipAddress string, trackerChan chan int) *gt.Tracker {

// Create callback function
callback := func(s []gt.CallbackResult, f []gt.CallbackResult) {
Expand All @@ -185,6 +191,9 @@ func initTracker(collector string, appid string, method string, protocol string,
gt.OptionStorage(gt.InitStorageMemory()),
)
subject := gt.InitSubject()
if ipAddress != "" {
subject.SetIpAddress(ipAddress)
}
tracker := gt.InitTracker(
gt.RequireEmitter(emitter),
gt.OptionSubject(subject),
Expand Down
6 changes: 3 additions & 3 deletions snowplowtrk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestInitTracker(t *testing.T) {
assert := assert.New(t)
trackerChan := make(chan int, 1)

tracker := initTracker("com.acme", "myapp", "POST", "https", trackerChan)
tracker := initTracker("com.acme", "myapp", "POST", "https", "", trackerChan)
assert.NotNil(tracker)
assert.NotNil(tracker.Emitter)
assert.NotNil(tracker.Subject)
Expand All @@ -99,7 +99,7 @@ func TestTrackSelfDescribingEventGood(t *testing.T) {

// Setup Tracker
trackerChan := make(chan int, 1)
tracker := initTracker("com.acme", "myapp", "GET", "http", trackerChan)
tracker := initTracker("com.acme", "myapp", "GET", "http", "", trackerChan)
assert.NotNil(tracker)

// Make SDJ
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestTrackSelfDescribingEventBad(t *testing.T) {

// Setup Tracker
trackerChan := make(chan int, 1)
tracker := initTracker("com.acme", "myapp", "POST", "http", trackerChan)
tracker := initTracker("com.acme", "myapp", "POST", "http", "", trackerChan)
assert.NotNil(tracker)

// Make SDJ
Expand Down

0 comments on commit b555b59

Please sign in to comment.