Skip to content

Commit bce8baf

Browse files
authored
chore: add essential around mutations (#2189)
1 parent 1c83745 commit bce8baf

File tree

5 files changed

+120
-15
lines changed

5 files changed

+120
-15
lines changed

taco/cmd/taco/commands/unit.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"text/tabwriter"
1414
"time"
1515

16+
"github.com/diggerhq/digger/opentaco/internal/analytics"
1617
"github.com/diggerhq/digger/opentaco/pkg/sdk"
1718
"github.com/google/uuid"
1819
"github.com/spf13/cobra"
@@ -50,16 +51,20 @@ var unitCreateCmd = &cobra.Command{
5051
Short: "Create a new unit",
5152
Args: cobra.ExactArgs(1),
5253
RunE: func(cmd *cobra.Command, args []string) error {
54+
analytics.SendEssential("taco_unit_create_started")
55+
5356
client := newAuthedClient()
5457
unitID := args[0]
5558

5659
printVerbose("Creating unit: %s", unitID)
5760

5861
resp, err := client.CreateUnit(context.Background(), unitID)
5962
if err != nil {
63+
analytics.SendEssential("taco_unit_create_failed")
6064
return fmt.Errorf("failed to create unit: %w", err)
6165
}
6266

67+
analytics.SendEssential("taco_unit_create_completed")
6368
fmt.Printf("Unit created: %s\n", resp.ID)
6469
return nil
6570
},
@@ -219,20 +224,27 @@ var unitPullCmd = &cobra.Command{
219224
Short: "Download unit data",
220225
Args: cobra.RangeArgs(1, 2),
221226
RunE: func(cmd *cobra.Command, args []string) error {
227+
analytics.SendEssential("taco_unit_pull_started")
228+
222229
client := newAuthedClient()
223230
unitID := args[0]
224231
printVerbose("Downloading unit: %s", unitID)
225232
data, err := client.DownloadUnit(context.Background(), unitID)
226-
if err != nil { return fmt.Errorf("failed to download unit: %w", err) }
233+
if err != nil {
234+
analytics.SendEssential("taco_unit_pull_failed")
235+
return fmt.Errorf("failed to download unit: %w", err)
236+
}
227237
if len(args) > 1 {
228238
outputFile := args[1]
229239
if err := os.WriteFile(outputFile, data, 0o644); err != nil {
240+
analytics.SendEssential("taco_unit_pull_failed")
230241
return fmt.Errorf("failed to write file: %w", err)
231242
}
232243
fmt.Printf("Unit downloaded to: %s\n", outputFile)
233244
} else {
234245
fmt.Print(string(data))
235246
}
247+
analytics.SendEssential("taco_unit_pull_completed")
236248
return nil
237249
},
238250
}
@@ -242,16 +254,23 @@ var unitPushCmd = &cobra.Command{
242254
Short: "Upload unit data",
243255
Args: cobra.ExactArgs(2),
244256
RunE: func(cmd *cobra.Command, args []string) error {
257+
analytics.SendEssential("taco_unit_push_started")
258+
245259
client := newAuthedClient()
246260
unitID := args[0]
247261
inputFile := args[1]
248262
printVerbose("Uploading unit: %s from %s", unitID, inputFile)
249263
data, err := os.ReadFile(inputFile)
250-
if err != nil { return fmt.Errorf("failed to read file: %w", err) }
264+
if err != nil {
265+
analytics.SendEssential("taco_unit_push_failed")
266+
return fmt.Errorf("failed to read file: %w", err)
267+
}
251268
lockID := getLockID(unitID)
252269
if err := client.UploadUnit(context.Background(), unitID, data, lockID); err != nil {
270+
analytics.SendEssential("taco_unit_push_failed")
253271
return fmt.Errorf("failed to upload unit: %w", err)
254272
}
273+
analytics.SendEssential("taco_unit_push_completed")
255274
fmt.Printf("Unit uploaded: %s\n", unitID)
256275
return nil
257276
},

taco/internal/backend/handler.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/diggerhq/digger/opentaco/internal/analytics"
1112
"github.com/diggerhq/digger/opentaco/internal/storage"
1213
"github.com/diggerhq/digger/opentaco/internal/deps"
1314
"github.com/google/uuid"
@@ -27,10 +28,13 @@ func NewHandler(store storage.UnitStore) *Handler {
2728

2829
// GetState handles GET requests for state retrieval
2930
func (h *Handler) GetState(c echo.Context) error {
31+
analytics.SendEssential("terraform_plan_started")
32+
3033
id := extractID(c)
3134

3235
data, err := h.store.Download(c.Request().Context(), id)
3336
if err != nil {
37+
analytics.SendEssential("terraform_plan_failed")
3438
if err == storage.ErrNotFound {
3539
return c.NoContent(http.StatusNotFound)
3640
}
@@ -39,11 +43,14 @@ func (h *Handler) GetState(c echo.Context) error {
3943
})
4044
}
4145

46+
analytics.SendEssential("terraform_plan_completed")
4247
return c.Blob(http.StatusOK, "application/json", data)
4348
}
4449

4550
// UpdateState handles POST/PUT requests for state updates
4651
func (h *Handler) UpdateState(c echo.Context) error {
52+
analytics.SendEssential("terraform_apply_started")
53+
4754
id := extractID(c)
4855

4956
// Check if state exists, create if not
@@ -52,6 +59,7 @@ func (h *Handler) UpdateState(c echo.Context) error {
5259
// Create state
5360
_, err = h.store.Create(c.Request().Context(), id)
5461
if err != nil && err != storage.ErrAlreadyExists {
62+
analytics.SendEssential("terraform_apply_failed")
5563
return c.JSON(http.StatusInternalServerError, map[string]string{
5664
"error": "Failed to create state",
5765
})
@@ -61,6 +69,7 @@ func (h *Handler) UpdateState(c echo.Context) error {
6169
// Read state data
6270
data, err := io.ReadAll(c.Request().Body)
6371
if err != nil {
72+
analytics.SendEssential("terraform_apply_failed")
6473
return c.JSON(http.StatusBadRequest, map[string]string{
6574
"error": "Failed to read request body",
6675
})
@@ -78,6 +87,7 @@ func (h *Handler) UpdateState(c echo.Context) error {
7887
// Upload state
7988
err = h.store.Upload(c.Request().Context(), id, data, lockID)
8089
if err != nil {
90+
analytics.SendEssential("terraform_apply_failed")
8191
if err == storage.ErrLockConflict {
8292
// Get current lock for details
8393
lock, _ := h.store.GetLock(c.Request().Context(), id)
@@ -96,6 +106,7 @@ func (h *Handler) UpdateState(c echo.Context) error {
96106
// Fire-and-forget graph update (best effort; never block/tank the write)
97107
go deps.UpdateGraphOnWrite(contextWithBackground(c), h.store, id, data)
98108

109+
analytics.SendEssential("terraform_apply_completed")
99110
return c.NoContent(http.StatusOK)
100111
}
101112

taco/internal/unit/handler.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,27 @@ type CreateUnitResponse struct {
4343
func (h *Handler) CreateUnit(c echo.Context) error {
4444
var req CreateUnitRequest
4545
if err := c.Bind(&req); err != nil {
46-
analytics.SendDefault("unit_create_failed_invalid_request")
46+
analytics.SendEssential("unit_create_failed_invalid_request")
4747
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Invalid request body"})
4848
}
4949

5050
if err := domain.ValidateUnitID(req.ID); err != nil {
51-
analytics.SendDefault("unit_create_failed_invalid_id")
51+
analytics.SendEssential("unit_create_failed_invalid_id")
5252
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
5353
}
5454
id := domain.NormalizeUnitID(req.ID)
5555

5656
metadata, err := h.store.Create(c.Request().Context(), id)
5757
if err != nil {
5858
if err == storage.ErrAlreadyExists {
59-
analytics.SendDefault("unit_create_failed_already_exists")
59+
analytics.SendEssential("unit_create_failed_already_exists")
6060
return c.JSON(http.StatusConflict, map[string]string{"error": "Unit already exists"})
6161
}
62-
analytics.SendDefault("unit_create_failed_storage_error")
62+
analytics.SendEssential("unit_create_failed_storage_error")
6363
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to create unit"})
6464
}
6565

66-
analytics.SendDefault("unit_created")
66+
analytics.SendEssential("unit_created")
6767
return c.JSON(http.StatusCreated, CreateUnitResponse{ID: metadata.ID, Created: metadata.Updated})
6868
}
6969

@@ -146,33 +146,43 @@ func (h *Handler) DeleteUnit(c echo.Context) error {
146146
}
147147

148148
func (h *Handler) DownloadUnit(c echo.Context) error {
149+
analytics.SendEssential("taco_unit_pull_started")
150+
149151
encodedID := c.Param("id")
150152
id := domain.DecodeUnitID(encodedID)
151153
if err := domain.ValidateUnitID(id); err != nil {
154+
analytics.SendEssential("taco_unit_pull_failed")
152155
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
153156
}
154157
data, err := h.store.Download(c.Request().Context(), id)
155158
if err != nil {
159+
analytics.SendEssential("taco_unit_pull_failed")
156160
if err == storage.ErrNotFound {
157161
return c.JSON(http.StatusNotFound, map[string]string{"error": "Unit not found"})
158162
}
159163
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "Failed to download unit"})
160164
}
165+
analytics.SendEssential("taco_unit_pull_completed")
161166
return c.Blob(http.StatusOK, "application/json", data)
162167
}
163168

164169
func (h *Handler) UploadUnit(c echo.Context) error {
170+
analytics.SendEssential("taco_unit_push_started")
171+
165172
encodedID := c.Param("id")
166173
id := domain.DecodeUnitID(encodedID)
167174
if err := domain.ValidateUnitID(id); err != nil {
175+
analytics.SendEssential("taco_unit_push_failed")
168176
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
169177
}
170178
data, err := io.ReadAll(c.Request().Body)
171179
if err != nil {
180+
analytics.SendEssential("taco_unit_push_failed")
172181
return c.JSON(http.StatusBadRequest, map[string]string{"error": "Failed to read request body"})
173182
}
174183
lockID := c.QueryParam("if_locked_by")
175184
if err := h.store.Upload(c.Request().Context(), id, data, lockID); err != nil {
185+
analytics.SendEssential("taco_unit_push_failed")
176186
if err == storage.ErrNotFound {
177187
return c.JSON(http.StatusNotFound, map[string]string{"error": "Unit not found"})
178188
}
@@ -183,6 +193,7 @@ func (h *Handler) UploadUnit(c echo.Context) error {
183193
}
184194
// Best-effort dependency graph update
185195
go deps.UpdateGraphOnWrite(c.Request().Context(), h.store, id, data)
196+
analytics.SendEssential("taco_unit_push_completed")
186197
return c.JSON(http.StatusOK, map[string]string{"message": "Unit uploaded successfully"})
187198
}
188199

taco/providers/terraform/opentaco/resources/dependency_resource.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/sha256"
66
"strings"
77

8+
"github.com/diggerhq/digger/opentaco/internal/analytics"
89
"github.com/hashicorp/terraform-plugin-framework/path"
910
"github.com/hashicorp/terraform-plugin-framework/resource"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -79,10 +80,15 @@ func (r *dependencyResource) Schema(_ context.Context, _ resource.SchemaRequest,
7980
}
8081

8182
func (r *dependencyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
83+
analytics.SendEssential("terraform_apply_started")
84+
8285
var plan dependencyModel
8386
diags := req.Plan.Get(ctx, &plan)
8487
resp.Diagnostics.Append(diags...)
85-
if resp.Diagnostics.HasError() { return }
88+
if resp.Diagnostics.HasError() {
89+
analytics.SendEssential("terraform_apply_failed")
90+
return
91+
}
8692

8793
// Default to_input to from_output if not set
8894
toInput := plan.ToInput.ValueString()
@@ -107,29 +113,48 @@ func (r *dependencyResource) Create(ctx context.Context, req resource.CreateRequ
107113

108114
diags = resp.State.Set(ctx, plan)
109115
resp.Diagnostics.Append(diags...)
116+
if !resp.Diagnostics.HasError() {
117+
analytics.SendEssential("terraform_apply_completed")
118+
}
110119
}
111120

112121
func (r *dependencyResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
122+
analytics.SendEssential("terraform_plan_started")
123+
113124
// No remote calls; state is source of truth (service may have edited it via state surgery)
114125
var state dependencyModel
115126
diags := req.State.Get(ctx, &state)
116127
resp.Diagnostics.Append(diags...)
117-
if resp.Diagnostics.HasError() { return }
128+
if resp.Diagnostics.HasError() {
129+
analytics.SendEssential("terraform_plan_failed")
130+
return
131+
}
118132
diags = resp.State.Set(ctx, &state)
119133
resp.Diagnostics.Append(diags...)
134+
if !resp.Diagnostics.HasError() {
135+
analytics.SendEssential("terraform_plan_completed")
136+
}
120137
}
121138

122139
func (r *dependencyResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
140+
analytics.SendEssential("terraform_apply_started")
141+
123142
// Carry over computed fields from current state to avoid Unknowns
124143
var state dependencyModel
125144
diags := req.State.Get(ctx, &state)
126145
resp.Diagnostics.Append(diags...)
127-
if resp.Diagnostics.HasError() { return }
146+
if resp.Diagnostics.HasError() {
147+
analytics.SendEssential("terraform_apply_failed")
148+
return
149+
}
128150

129151
var plan dependencyModel
130152
diags = req.Plan.Get(ctx, &plan)
131153
resp.Diagnostics.Append(diags...)
132-
if resp.Diagnostics.HasError() { return }
154+
if resp.Diagnostics.HasError() {
155+
analytics.SendEssential("terraform_apply_failed")
156+
return
157+
}
133158

134159
if plan.ToInput.ValueString() == "" {
135160
plan.ToInput = plan.FromOutput
@@ -147,10 +172,15 @@ func (r *dependencyResource) Update(ctx context.Context, req resource.UpdateRequ
147172

148173
diags = resp.State.Set(ctx, plan)
149174
resp.Diagnostics.Append(diags...)
175+
if !resp.Diagnostics.HasError() {
176+
analytics.SendEssential("terraform_apply_completed")
177+
}
150178
}
151179

152180
func (r *dependencyResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
181+
analytics.SendEssential("terraform_apply_started")
153182
// Nothing to do; remove from state
183+
analytics.SendEssential("terraform_apply_completed")
154184
}
155185

156186
func (r *dependencyResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {

0 commit comments

Comments
 (0)