Skip to content

Commit 24b7387

Browse files
authored
feat: add logging for trace id (#1061)
relates to STACKITTPR-290
1 parent d1e12fc commit 24b7387

File tree

140 files changed

+1597
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+1597
-7
lines changed

.github/docs/contribution-guide/resource.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ func (r *barResource) Create(ctx context.Context, req resource.CreateRequest, re
184184
if resp.Diagnostics.HasError() {
185185
return
186186
}
187+
188+
ctx = core.InitProviderContext(ctx)
189+
187190
projectId := model.ProjectId.ValueString()
188191
region := model.Region.ValueString() // not needed for global APIs
189192
ctx = tflog.SetField(ctx, "project_id", projectId)
@@ -203,6 +206,8 @@ func (r *barResource) Create(ctx context.Context, req resource.CreateRequest, re
203206
return
204207
}
205208

209+
ctx = core.LogResponse(ctx)
210+
206211
// only in case the create bar API call is asynchronous (Make sure to include *ALL* fields which are part of the
207212
// internal terraform resource id! And please include the comment below in your code):
208213
// Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
@@ -244,6 +249,9 @@ func (r *barResource) Read(ctx context.Context, req resource.ReadRequest, resp *
244249
if resp.Diagnostics.HasError() {
245250
return
246251
}
252+
253+
ctx = core.InitProviderContext(ctx)
254+
247255
projectId := model.ProjectId.ValueString()
248256
region := r.providerData.GetRegionWithOverride(model.Region)
249257
barId := model.BarId.ValueString()
@@ -257,6 +265,8 @@ func (r *barResource) Read(ctx context.Context, req resource.ReadRequest, resp *
257265
return
258266
}
259267

268+
ctx = core.LogResponse(ctx)
269+
260270
// Map response body to schema
261271
err = mapFields(barResp, &model)
262272
if err != nil {
@@ -285,6 +295,9 @@ func (r *barResource) Delete(ctx context.Context, req resource.DeleteRequest, re
285295
if resp.Diagnostics.HasError() {
286296
return
287297
}
298+
299+
ctx = core.InitProviderContext(ctx)
300+
288301
projectId := model.ProjectId.ValueString()
289302
region := model.Region.ValueString()
290303
barId := model.BarId.ValueString()
@@ -298,6 +311,8 @@ func (r *barResource) Delete(ctx context.Context, req resource.DeleteRequest, re
298311
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting bar", fmt.Sprintf("Calling API: %v", err))
299312
}
300313

314+
ctx = core.LogResponse(ctx)
315+
301316
// only in case the bar delete API endpoint is asynchronous: use a wait handler to wait for the delete operation to complete
302317
_, err = wait.DeleteBarWaitHandler(ctx, r.client, projectId, region, barId).WaitWithContext(ctx)
303318
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/hashicorp/terraform-plugin-go v0.29.0
1212
github.com/hashicorp/terraform-plugin-log v0.10.0
1313
github.com/hashicorp/terraform-plugin-testing v1.13.3
14-
github.com/stackitcloud/stackit-sdk-go/core v0.19.0
14+
github.com/stackitcloud/stackit-sdk-go/core v0.20.0
1515
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.6.0
1616
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.1
1717
github.com/stackitcloud/stackit-sdk-go/services/git v0.8.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN
152152
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
153153
github.com/skeema/knownhosts v1.3.1 h1:X2osQ+RAjK76shCbvhHHHVl3ZlgDm8apHEHFqRjnBY8=
154154
github.com/skeema/knownhosts v1.3.1/go.mod h1:r7KTdC8l4uxWRyK2TpQZ/1o5HaSzh06ePQNxPwTcfiY=
155-
github.com/stackitcloud/stackit-sdk-go/core v0.19.0 h1:dtJcs6/TTCzzb2RKI7HJugDrbCkaFEDmn1pOeFe8qnI=
156-
github.com/stackitcloud/stackit-sdk-go/core v0.19.0/go.mod h1:fqto7M82ynGhEnpZU6VkQKYWYoFG5goC076JWXTUPRQ=
155+
github.com/stackitcloud/stackit-sdk-go/core v0.20.0 h1:4rrUk6uT1g4nOn5/g1uXukP07Tux/o5xbMz/f/qE1rY=
156+
github.com/stackitcloud/stackit-sdk-go/core v0.20.0/go.mod h1:fqto7M82ynGhEnpZU6VkQKYWYoFG5goC076JWXTUPRQ=
157157
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.9.0 h1:7ZKd3b+E/R4TEVShLTXxx5FrsuDuJBOyuVOuKTMa4mo=
158158
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.9.0/go.mod h1:/FoXa6hF77Gv8brrvLBCKa5ie1Xy9xn39yfHwaln9Tw=
159159
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.6.0 h1:Q+qIdejeMsYMkbtVoI9BpGlKGdSVFRBhH/zj44SP8TM=

stackit/internal/core/core.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/stackitcloud/stackit-sdk-go/core/runtime"
1011

1112
"github.com/hashicorp/terraform-plugin-framework/diag"
1213
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -102,12 +103,20 @@ func DiagsToError(diags diag.Diagnostics) error {
102103

103104
// LogAndAddError Logs the error and adds it to the diags
104105
func LogAndAddError(ctx context.Context, diags *diag.Diagnostics, summary, detail string) {
106+
if traceId := runtime.GetTraceId(ctx); traceId != "" {
107+
detail = fmt.Sprintf("%s\nTrace ID: %q", detail, traceId)
108+
}
109+
105110
tflog.Error(ctx, fmt.Sprintf("%s | %s", summary, detail))
106111
diags.AddError(summary, detail)
107112
}
108113

109114
// LogAndAddWarning Logs the warning and adds it to the diags
110115
func LogAndAddWarning(ctx context.Context, diags *diag.Diagnostics, summary, detail string) {
116+
if traceId := runtime.GetTraceId(ctx); traceId != "" {
117+
detail = fmt.Sprintf("%s\nTrace ID: %q", detail, traceId)
118+
}
119+
111120
tflog.Warn(ctx, fmt.Sprintf("%s | %s", summary, detail))
112121
diags.AddWarning(summary, detail)
113122
}
@@ -125,3 +134,22 @@ func LogAndAddErrorBeta(ctx context.Context, diags *diag.Diagnostics, name strin
125134
tflog.Error(ctx, fmt.Sprintf("%s | %s", errTitle, errContent))
126135
diags.AddError(errTitle, errContent)
127136
}
137+
138+
// InitProviderContext extends the context to capture the http response
139+
func InitProviderContext(ctx context.Context) context.Context {
140+
// Capture http response to get trace-id
141+
var httpResp *http.Response
142+
return runtime.WithCaptureHTTPResponse(ctx, &httpResp)
143+
}
144+
145+
// LogResponse logs the trace-id of the last request
146+
func LogResponse(ctx context.Context) context.Context {
147+
// Logs the trace-id of the request
148+
traceId := runtime.GetTraceId(ctx)
149+
ctx = tflog.SetField(ctx, "x-trace-id", traceId)
150+
151+
tflog.Info(ctx, "response data", map[string]interface{}{
152+
"x-trace-id": traceId,
153+
})
154+
return ctx
155+
}

stackit/internal/services/authorization/roleassignments/resource.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ func (r *roleAssignmentResource) Create(ctx context.Context, req resource.Create
152152
return
153153
}
154154

155+
ctx = core.InitProviderContext(ctx)
156+
155157
ctx = r.annotateLogger(ctx, &model)
156158

157159
if err := r.checkDuplicate(ctx, model); err != nil {
@@ -171,6 +173,8 @@ func (r *roleAssignmentResource) Create(ctx context.Context, req resource.Create
171173
return
172174
}
173175

176+
ctx = core.LogResponse(ctx)
177+
174178
// Map response body to schema
175179
err = mapMembersResponse(createResp, &model)
176180
if err != nil {
@@ -194,6 +198,8 @@ func (r *roleAssignmentResource) Read(ctx context.Context, req resource.ReadRequ
194198
return
195199
}
196200

201+
ctx = core.InitProviderContext(ctx)
202+
197203
ctx = r.annotateLogger(ctx, &model)
198204

199205
listResp, err := r.authorizationClient.ListMembers(ctx, r.apiName, model.ResourceId.ValueString()).Subject(model.Subject.ValueString()).Execute()
@@ -202,6 +208,8 @@ func (r *roleAssignmentResource) Read(ctx context.Context, req resource.ReadRequ
202208
return
203209
}
204210

211+
ctx = core.LogResponse(ctx)
212+
205213
// Map response body to schema
206214
err = mapListMembersResponse(listResp, &model)
207215
if err != nil {
@@ -232,6 +240,8 @@ func (r *roleAssignmentResource) Delete(ctx context.Context, req resource.Delete
232240
return
233241
}
234242

243+
ctx = core.InitProviderContext(ctx)
244+
235245
ctx = r.annotateLogger(ctx, &model)
236246

237247
payload := authorization.RemoveMembersPayload{
@@ -247,6 +257,8 @@ func (r *roleAssignmentResource) Delete(ctx context.Context, req resource.Delete
247257
core.LogAndAddError(ctx, &resp.Diagnostics, fmt.Sprintf("Error deleting %s role assignment", r.apiName), fmt.Sprintf("Calling API: %v", err))
248258
}
249259

260+
ctx = core.LogResponse(ctx)
261+
250262
tflog.Info(ctx, fmt.Sprintf("%s role assignment deleted", r.apiName))
251263
}
252264

stackit/internal/services/cdn/customdomain/datasource.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ func (r *customDomainDataSource) Read(ctx context.Context, req datasource.ReadRe
126126
return
127127
}
128128

129+
ctx = core.InitProviderContext(ctx)
130+
129131
projectId := model.ProjectId.ValueString()
130132
ctx = tflog.SetField(ctx, "project_id", projectId)
131133
distributionId := model.DistributionId.ValueString()
@@ -146,6 +148,8 @@ func (r *customDomainDataSource) Read(ctx context.Context, req datasource.ReadRe
146148
return
147149
}
148150

151+
ctx = core.LogResponse(ctx)
152+
149153
// Call the new data source mapping function
150154
err = mapCustomDomainDataSourceFields(customDomainResp, &model, projectId, distributionId)
151155
if err != nil {

stackit/internal/services/cdn/customdomain/resource.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ func (r *customDomainResource) Create(ctx context.Context, req resource.CreateRe
186186
if resp.Diagnostics.HasError() {
187187
return
188188
}
189+
190+
ctx = core.InitProviderContext(ctx)
191+
189192
projectId := model.ProjectId.ValueString()
190193
ctx = tflog.SetField(ctx, "project_id", projectId)
191194
distributionId := model.DistributionId.ValueString()
@@ -207,6 +210,9 @@ func (r *customDomainResource) Create(ctx context.Context, req resource.CreateRe
207210
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN custom domain", fmt.Sprintf("Calling API: %v", err))
208211
return
209212
}
213+
214+
ctx = core.LogResponse(ctx)
215+
210216
_, err = wait.CreateCDNCustomDomainWaitHandler(ctx, r.client, projectId, distributionId, name).SetTimeout(5 * time.Minute).WaitWithContext(ctx)
211217
if err != nil {
212218
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN custom domain", fmt.Sprintf("Waiting for create: %v", err))
@@ -240,6 +246,8 @@ func (r *customDomainResource) Read(ctx context.Context, req resource.ReadReques
240246
return
241247
}
242248

249+
ctx = core.InitProviderContext(ctx)
250+
243251
projectId := model.ProjectId.ValueString()
244252
ctx = tflog.SetField(ctx, "project_id", projectId)
245253
distributionId := model.DistributionId.ValueString()
@@ -248,7 +256,6 @@ func (r *customDomainResource) Read(ctx context.Context, req resource.ReadReques
248256
ctx = tflog.SetField(ctx, "name", name)
249257

250258
customDomainResp, err := r.client.GetCustomDomain(ctx, projectId, distributionId, name).Execute()
251-
252259
if err != nil {
253260
var oapiErr *oapierror.GenericOpenAPIError
254261
// n.b. err is caught here if of type *oapierror.GenericOpenAPIError, which the stackit SDK client returns
@@ -261,6 +268,9 @@ func (r *customDomainResource) Read(ctx context.Context, req resource.ReadReques
261268
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN custom domain", fmt.Sprintf("Calling API: %v", err))
262269
return
263270
}
271+
272+
ctx = core.LogResponse(ctx)
273+
264274
err = mapCustomDomainResourceFields(customDomainResp, &model)
265275
if err != nil {
266276
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN custom domain", fmt.Sprintf("Processing API payload: %v", err))
@@ -283,6 +293,8 @@ func (r *customDomainResource) Update(ctx context.Context, req resource.UpdateRe
283293
return
284294
}
285295

296+
ctx = core.InitProviderContext(ctx)
297+
286298
projectId := model.ProjectId.ValueString()
287299
ctx = tflog.SetField(ctx, "project_id", projectId)
288300
distributionId := model.DistributionId.ValueString()
@@ -306,6 +318,8 @@ func (r *customDomainResource) Update(ctx context.Context, req resource.UpdateRe
306318
return
307319
}
308320

321+
ctx = core.LogResponse(ctx)
322+
309323
_, err = wait.CreateCDNCustomDomainWaitHandler(ctx, r.client, projectId, distributionId, name).SetTimeout(5 * time.Minute).WaitWithContext(ctx)
310324
if err != nil {
311325
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating CDN custom domain certificate", fmt.Sprintf("Waiting for update: %v", err))
@@ -338,6 +352,8 @@ func (r *customDomainResource) Delete(ctx context.Context, req resource.DeleteRe
338352
return
339353
}
340354

355+
ctx = core.InitProviderContext(ctx)
356+
341357
projectId := model.ProjectId.ValueString()
342358
ctx = tflog.SetField(ctx, "project_id", projectId)
343359
distributionId := model.DistributionId.ValueString()
@@ -349,6 +365,9 @@ func (r *customDomainResource) Delete(ctx context.Context, req resource.DeleteRe
349365
if err != nil {
350366
core.LogAndAddError(ctx, &resp.Diagnostics, "Delete CDN custom domain", fmt.Sprintf("Delete custom domain: %v", err))
351367
}
368+
369+
ctx = core.LogResponse(ctx)
370+
352371
_, err = wait.DeleteCDNCustomDomainWaitHandler(ctx, r.client, projectId, distributionId, name).WaitWithContext(ctx)
353372
if err != nil {
354373
core.LogAndAddError(ctx, &resp.Diagnostics, "Delete CDN custom domain", fmt.Sprintf("Waiting for deletion: %v", err))

stackit/internal/services/cdn/distribution/datasource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ func (r *distributionDataSource) Read(ctx context.Context, req datasource.ReadRe
184184
return
185185
}
186186

187+
ctx = core.InitProviderContext(ctx)
188+
187189
projectId := model.ProjectId.ValueString()
188190
distributionId := model.DistributionId.ValueString()
189191
distributionResp, err := r.client.GetDistributionExecute(ctx, projectId, distributionId)
@@ -199,6 +201,9 @@ func (r *distributionDataSource) Read(ctx context.Context, req datasource.ReadRe
199201
resp.State.RemoveResource(ctx)
200202
return
201203
}
204+
205+
ctx = core.LogResponse(ctx)
206+
202207
err = mapFields(ctx, distributionResp.Distribution, &model)
203208
if err != nil {
204209
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN distribution", fmt.Sprintf("Error processing API response: %v", err))

stackit/internal/services/cdn/distribution/resource.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ func (r *distributionResource) Create(ctx context.Context, req resource.CreateRe
334334
if resp.Diagnostics.HasError() {
335335
return
336336
}
337+
338+
ctx = core.InitProviderContext(ctx)
339+
337340
projectId := model.ProjectId.ValueString()
338341
ctx = tflog.SetField(ctx, "project_id", projectId)
339342

@@ -348,6 +351,9 @@ func (r *distributionResource) Create(ctx context.Context, req resource.CreateRe
348351
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN distribution", fmt.Sprintf("Calling API: %v", err))
349352
return
350353
}
354+
355+
ctx = core.LogResponse(ctx)
356+
351357
waitResp, err := wait.CreateDistributionPoolWaitHandler(ctx, r.client, projectId, *createResp.Distribution.Id).SetTimeout(5 * time.Minute).WaitWithContext(ctx)
352358
if err != nil {
353359
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN distribution", fmt.Sprintf("Waiting for create: %v", err))
@@ -376,6 +382,8 @@ func (r *distributionResource) Read(ctx context.Context, req resource.ReadReques
376382
return
377383
}
378384

385+
ctx = core.InitProviderContext(ctx)
386+
379387
projectId := model.ProjectId.ValueString()
380388
distributionId := model.DistributionId.ValueString()
381389
ctx = tflog.SetField(ctx, "project_id", projectId)
@@ -394,6 +402,9 @@ func (r *distributionResource) Read(ctx context.Context, req resource.ReadReques
394402
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN distribution", fmt.Sprintf("Calling API: %v", err))
395403
return
396404
}
405+
406+
ctx = core.LogResponse(ctx)
407+
397408
err = mapFields(ctx, cdnResp.Distribution, &model)
398409
if err != nil {
399410
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN ditribution", fmt.Sprintf("Processing API payload: %v", err))
@@ -416,6 +427,8 @@ func (r *distributionResource) Update(ctx context.Context, req resource.UpdateRe
416427
return
417428
}
418429

430+
ctx = core.InitProviderContext(ctx)
431+
419432
projectId := model.ProjectId.ValueString()
420433
distributionId := model.DistributionId.ValueString()
421434
ctx = tflog.SetField(ctx, "project_id", projectId)
@@ -516,6 +529,8 @@ func (r *distributionResource) Update(ctx context.Context, req resource.UpdateRe
516529
return
517530
}
518531

532+
ctx = core.LogResponse(ctx)
533+
519534
waitResp, err := wait.UpdateDistributionWaitHandler(ctx, r.client, projectId, distributionId).WaitWithContext(ctx)
520535
if err != nil {
521536
core.LogAndAddError(ctx, &resp.Diagnostics, "Update CDN distribution", fmt.Sprintf("Waiting for update: %v", err))
@@ -543,6 +558,9 @@ func (r *distributionResource) Delete(ctx context.Context, req resource.DeleteRe
543558
if resp.Diagnostics.HasError() {
544559
return
545560
}
561+
562+
ctx = core.LogResponse(ctx)
563+
546564
projectId := model.ProjectId.ValueString()
547565
distributionId := model.DistributionId.ValueString()
548566
ctx = tflog.SetField(ctx, "project_id", projectId)
@@ -552,6 +570,9 @@ func (r *distributionResource) Delete(ctx context.Context, req resource.DeleteRe
552570
if err != nil {
553571
core.LogAndAddError(ctx, &resp.Diagnostics, "Delete CDN distribution", fmt.Sprintf("Delete distribution: %v", err))
554572
}
573+
574+
ctx = core.LogResponse(ctx)
575+
555576
_, err = wait.DeleteDistributionWaitHandler(ctx, r.client, projectId, distributionId).WaitWithContext(ctx)
556577
if err != nil {
557578
core.LogAndAddError(ctx, &resp.Diagnostics, "Delete CDN distribution", fmt.Sprintf("Waiting for deletion: %v", err))

0 commit comments

Comments
 (0)