-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
77 changed files
with
671 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package metrics | ||
|
||
type Action string | ||
|
||
const ( | ||
Create Action = "Create" | ||
Read Action = "Read" | ||
Update Action = "Update" | ||
Delete Action = "Delete" | ||
Import Action = "Import" | ||
) | ||
|
||
// String returns the string representation of the action. | ||
func (a Action) String() string { | ||
return string(a) | ||
} |
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,49 @@ | ||
package metrics | ||
|
||
import ( | ||
"time" | ||
|
||
tat "github.com/FrangipaneTeam/terraform-analytic-tool/api" | ||
) | ||
|
||
// version that can be overwritten by a release process. | ||
var version = "dev" | ||
|
||
// token can be overwritten by a release process. | ||
var token = "dev" | ||
|
||
// target can be overwritten by a release process. | ||
var target = "https://localhost" | ||
|
||
// GlobalExecutionID is the execution ID of the current Terraform run. | ||
var GlobalExecutionID = "" | ||
|
||
func New(resourceName, organizationID string, action Action) func() { | ||
if everyThingIsOK() { | ||
start := time.Now() | ||
return func() { | ||
timeElapsed := time.Since(start) | ||
send( | ||
tat.AnalyticRequest{ | ||
TerraformRequest: &tat.TerraformRequest{ | ||
TerraformExecutionID: GlobalExecutionID, | ||
ClientVersion: "terraform-cloudavenue/" + version, | ||
ClientToken: token, | ||
}, | ||
ResourceName: resourceName, | ||
OrganizationID: organizationID, | ||
Action: action.String(), | ||
ExecutionTime: timeElapsed.Milliseconds(), | ||
}) | ||
} | ||
} | ||
return func() {} | ||
} | ||
|
||
// everyThingIsOK Check if all variables are set. | ||
func everyThingIsOK() bool { | ||
if version == "" || token == "" || target == "" { | ||
return false | ||
} | ||
return true | ||
} |
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,46 @@ | ||
package metrics | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"time" | ||
|
||
tat "github.com/FrangipaneTeam/terraform-analytic-tool/api" | ||
) | ||
|
||
// Send is a function to send an event | ||
// with a given configuration and client. | ||
// This not return an error because it's not critical. | ||
func send(event tat.AnalyticRequest) { | ||
// Serialize and pack event | ||
eventPkg, err := json.Marshal(event) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Context with 1 second timeout | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
|
||
// Compose request | ||
req, err := http.NewRequestWithContext(ctx, "POST", target+"/api/v1/send", bytes.NewReader(eventPkg)) | ||
if err != nil { | ||
return | ||
} | ||
// Set headers | ||
req.Header.Set("Content-Type", "application/json") | ||
// Add Header Authorization if token is set | ||
if token != "" { | ||
req.Header.Set("Authorization", token) | ||
} | ||
|
||
// Send request | ||
res, err := http.DefaultClient.Do(req) | ||
// Check error | ||
if err != nil { | ||
return | ||
} | ||
res.Body.Close() | ||
} |
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
Oops, something went wrong.