-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0283769
commit 865e9da
Showing
7 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package telemetry | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
var ( | ||
enable = true | ||
interval = time.Hour | ||
method = "POST" | ||
protocol = "https" | ||
url = "telemetry-g.snowplowanalytics.com" | ||
port = "443" | ||
applicationName = "stream-replicator" | ||
applicationVersion = "1.0.0" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package telemetry | ||
|
||
import ( | ||
"fmt" | ||
log "github.com/sirupsen/logrus" | ||
"net/http" | ||
"time" | ||
|
||
conf "github.com/snowplow-devops/stream-replicator/config" | ||
gt "github.com/snowplow/snowplow-golang-tracker/v2/tracker" | ||
"github.com/twinj/uuid" | ||
) | ||
|
||
// config holds the configuration for telemetry | ||
type config struct { | ||
enable bool | ||
interval time.Duration | ||
method string | ||
url string | ||
protocol string | ||
port string | ||
userProvidedID string | ||
applicationName string | ||
applicationVersion string | ||
appGeneratedID string | ||
} | ||
|
||
func newTelemetryWithConfig(cfg *conf.Config) *config { | ||
return &config{ | ||
enable: enable, | ||
interval: interval, | ||
method: method, | ||
protocol: protocol, | ||
url: url, | ||
port: port, | ||
userProvidedID: cfg.Data.UserProvidedID, | ||
applicationName: applicationName, | ||
applicationVersion: applicationVersion, | ||
appGeneratedID: uuid.NewV4().String(), | ||
} | ||
} | ||
|
||
func initTelemetry(telemetry *config) { | ||
storage := gt.InitStorageMemory() | ||
emitter := gt.InitEmitter( | ||
gt.RequireCollectorUri(fmt.Sprintf(`%s:%s`, telemetry.url, telemetry.port)), | ||
gt.OptionRequestType(telemetry.method), | ||
gt.OptionProtocol(telemetry.protocol), | ||
gt.OptionCallback(func(g []gt.CallbackResult, b []gt.CallbackResult) { | ||
if len(g) != 0 && g[0].Status != http.StatusOK { | ||
log.Printf(`Error sending good telemetry event: %d code`, g[0].Status) | ||
return | ||
} | ||
if len(b) != 0 && b[0].Status != http.StatusOK { | ||
log.Printf(`Error sending bad telemetry event: %d code`, b[0].Status) | ||
return | ||
} | ||
log.Println(`Telemetry event sent successfully`) | ||
}), | ||
gt.OptionStorage(storage), | ||
) | ||
|
||
tracker := gt.InitTracker( | ||
gt.RequireEmitter(emitter), | ||
gt.OptionNamespace("telemetry"), | ||
gt.OptionAppId(telemetry.applicationName), | ||
) | ||
|
||
ticker := time.NewTicker(telemetry.interval) | ||
|
||
go func() { | ||
makeAndTrackHeartbeat(telemetry, tracker) | ||
for { | ||
<-ticker.C | ||
makeAndTrackHeartbeat(telemetry, tracker) | ||
} | ||
}() | ||
} | ||
|
||
func makeAndTrackHeartbeat(telemetry *config, tracker *gt.Tracker) { | ||
event := makeHeartbeatEvent(*telemetry) | ||
|
||
tracker.TrackSelfDescribingEvent(gt.SelfDescribingEvent{ | ||
Event: event, | ||
Timestamp: nil, | ||
EventId: nil, | ||
TrueTimestamp: nil, | ||
Contexts: nil, | ||
Subject: nil, | ||
}) | ||
} | ||
|
||
// InitTelemetryWithCollector initialises telemetry | ||
func InitTelemetryWithCollector(cfg *conf.Config) { | ||
telemetry := newTelemetryWithConfig(cfg) | ||
initTelemetry(telemetry) | ||
} | ||
|
||
func makeHeartbeatEvent(service config) *gt.SelfDescribingJson { | ||
payload := gt.InitPayload() | ||
|
||
payload.Add(`userProvidedId`, &service.userProvidedID) | ||
payload.Add(`applicationName`, &service.applicationName) | ||
payload.Add(`applicationVersion`, &service.applicationVersion) | ||
payload.Add(`appGeneratedId`, &service.appGeneratedID) | ||
|
||
selfDescJSON := gt.InitSelfDescribingJson( | ||
`iglu:com.snowplowanalytics.oss/oss_context/jsonschema/1-0-1`, payload.Get()) | ||
return selfDescJSON | ||
} |