Skip to content

Commit

Permalink
chore: add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod authored and David MICHENEAU committed Sep 7, 2023
1 parent 6c54571 commit 1f19a37
Show file tree
Hide file tree
Showing 77 changed files with 671 additions and 53 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ jobs:
# GitHub sets the GITHUB_TOKEN secret automatically.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
METRICS_TARGET: ${{ secrets.METRICS_TARGET }}
METRICS_TOKEN: ${{ secrets.METRICS_TOKEN }}
highest-version-tag:
needs: [goreleaser]
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ builds:
flags:
- -trimpath
ldflags:
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics.version={{.Version}} -X github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics.target={{ .Env.METRICS_TARGET }} -X github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics.token={{ .Env.METRICS_TOKEN }}"'
goos:
- freebsd
- windows
Expand Down
16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
module github.com/orange-cloudavenue/terraform-provider-cloudavenue

go 1.20
go 1.21.0

require (
github.com/FrangipaneTeam/terraform-analytic-tool v0.0.11
github.com/FrangipaneTeam/terraform-plugin-framework-planmodifiers v1.3.3
github.com/FrangipaneTeam/terraform-plugin-framework-superschema v1.5.3
github.com/FrangipaneTeam/terraform-plugin-framework-supertypes v0.1.0
Expand All @@ -15,13 +16,14 @@ require (
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.18.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/iancoleman/strcase v0.3.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.28.0
github.com/iancoleman/strcase v0.3.0
github.com/orange-cloudavenue/cloudavenue-sdk-go v0.1.2
github.com/rs/zerolog v1.30.0
github.com/vmware/go-vcloud-director/v2 v2.21.0
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df
golang.org/x/net v0.15.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand All @@ -30,12 +32,19 @@ require (
github.com/Masterminds/sprig/v3 v3.2.3 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/ajg/form v1.5.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-chi/chi v4.0.2+incompatible // indirect
github.com/go-chi/render v1.0.3 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand All @@ -57,6 +66,8 @@ require (
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/influxdata/influxdb-client-go/v2 v2.12.3 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -69,6 +80,7 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/peterhellberg/link v1.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
Expand Down
90 changes: 90 additions & 0 deletions go.sum

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions internal/metrics/action.go
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)
}
49 changes: 49 additions & 0 deletions internal/metrics/metrics.go
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
}
46 changes: 46 additions & 0 deletions internal/metrics/send.go
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()
}
3 changes: 3 additions & 0 deletions internal/provider/alb/pool_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/edgegw"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/org"
)
Expand Down Expand Up @@ -78,6 +79,8 @@ func (d *albPoolDataSource) Configure(ctx context.Context, req datasource.Config
}

func (d *albPoolDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_alb_pool", d.client.GetOrgName(), metrics.Read)()

var (
data *albPoolModel
diags diag.Diagnostics
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/alb/pool_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/edgegw"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/org"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/pkg/utils"
Expand Down Expand Up @@ -91,6 +92,8 @@ func (r *albPoolResource) Configure(ctx context.Context, req resource.ConfigureR

// Create creates the resource and sets the initial Terraform state.
func (r *albPoolResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
defer metrics.New("cloudavenue_alb_pool", r.client.GetOrgName(), metrics.Create)()

// Retrieve values from plan
var (
plan *albPoolModel
Expand Down Expand Up @@ -139,6 +142,8 @@ func (r *albPoolResource) Create(ctx context.Context, req resource.CreateRequest

// Read refreshes the Terraform state with the latest data.
func (r *albPoolResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
defer metrics.New("cloudavenue_alb_pool", r.client.GetOrgName(), metrics.Read)()

var (
state *albPoolModel
diags diag.Diagnostics
Expand Down Expand Up @@ -221,6 +226,8 @@ func (r *albPoolResource) Read(ctx context.Context, req resource.ReadRequest, re

// Update updates the resource and sets the updated Terraform state on success.
func (r *albPoolResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
defer metrics.New("cloudavenue_alb_pool", r.client.GetOrgName(), metrics.Update)()

var plan *albPoolModel

// Get current state
Expand Down Expand Up @@ -271,6 +278,8 @@ func (r *albPoolResource) Update(ctx context.Context, req resource.UpdateRequest

// Delete deletes the resource and removes the Terraform state on success.
func (r *albPoolResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
defer metrics.New("cloudavenue_alb_pool", r.client.GetOrgName(), metrics.Delete)()

var state *albPoolModel

// Get current state
Expand Down Expand Up @@ -309,6 +318,8 @@ func (r *albPoolResource) Delete(ctx context.Context, req resource.DeleteRequest
}

func (r *albPoolResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
defer metrics.New("cloudavenue_alb_pool", r.client.GetOrgName(), metrics.Import)()

idParts := strings.Split(req.ID, ".")

if len(idParts) != 2 {
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/catalog/acl_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/adminorg"
)

Expand Down Expand Up @@ -65,6 +66,8 @@ func (d *aclDataSource) Configure(ctx context.Context, req datasource.ConfigureR
}

func (d *aclDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_catalog_acl", d.client.GetOrgName(), metrics.Read)()

config := &ACLModel{}

// Read Terraform configuration data into the model
Expand Down
12 changes: 11 additions & 1 deletion internal/provider/catalog/acl_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/adminorg"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/pkg/uuid"
)
Expand Down Expand Up @@ -76,6 +77,8 @@ func (r *aclResource) Configure(ctx context.Context, req resource.ConfigureReque

// Create creates the resource and sets the initial Terraform state.
func (r *aclResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
defer metrics.New("cloudavenue_catalog_acl", r.client.GetOrgName(), metrics.Create)()

plan := &ACLModel{}

// Retrieve values from plan
Expand Down Expand Up @@ -111,6 +114,8 @@ func (r *aclResource) Create(ctx context.Context, req resource.CreateRequest, re

// Read refreshes the Terraform state with the latest data.
func (r *aclResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
defer metrics.New("cloudavenue_catalog_acl", r.client.GetOrgName(), metrics.Read)()

state := &ACLModel{}

// Get current state
Expand Down Expand Up @@ -145,6 +150,8 @@ func (r *aclResource) Read(ctx context.Context, req resource.ReadRequest, resp *

// Update updates the resource and sets the updated Terraform state on success.
func (r *aclResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
defer metrics.New("cloudavenue_catalog_acl", r.client.GetOrgName(), metrics.Update)()

var (
plan = &ACLModel{}
state = &ACLModel{}
Expand Down Expand Up @@ -184,6 +191,8 @@ func (r *aclResource) Update(ctx context.Context, req resource.UpdateRequest, re

// Delete deletes the resource and removes the Terraform state on success.
func (r *aclResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
defer metrics.New("cloudavenue_catalog_acl", r.client.GetOrgName(), metrics.Delete)()

state := &ACLModel{}

// Get current state
Expand Down Expand Up @@ -214,8 +223,9 @@ func (r *aclResource) Delete(ctx context.Context, req resource.DeleteRequest, re
}

func (r *aclResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// import format is catalogIDOrName
defer metrics.New("cloudavenue_catalog_acl", r.client.GetOrgName(), metrics.Import)()

// import format is catalogIDOrName
var d diag.Diagnostics

r.adminOrg, d = adminorg.Init(r.client)
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/catalog/catalog_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/client"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/metrics"
"github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/provider/common/adminorg"
)

Expand Down Expand Up @@ -77,6 +78,8 @@ func (d *catalogDataSource) Configure(ctx context.Context, req datasource.Config
}

func (d *catalogDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
defer metrics.New("data.cloudavenue_catalog", d.client.GetOrgName(), metrics.Read)()

state := &catalogDataSourceModel{}

// Read Terraform configuration data into the model
Expand Down
Loading

0 comments on commit 1f19a37

Please sign in to comment.