Skip to content

Commit 0fbca79

Browse files
committed
feat: log trace id in every resource / datasource
1 parent fc82b3e commit 0fbca79

File tree

103 files changed

+465
-44
lines changed

Some content is hidden

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

103 files changed

+465
-44
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func (r *barResource) Create(ctx context.Context, req resource.CreateRequest, re
184184
if resp.Diagnostics.HasError() {
185185
return
186186
}
187+
ctx = core.InitProviderContext(ctx)
187188
projectId := model.ProjectId.ValueString()
188189
region := model.Region.ValueString() // not needed for global APIs
189190
ctx = tflog.SetField(ctx, "project_id", projectId)
@@ -202,6 +203,7 @@ func (r *barResource) Create(ctx context.Context, req resource.CreateRequest, re
202203
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating bar", fmt.Sprintf("Calling API: %v", err))
203204
return
204205
}
206+
ctx = core.LogResponse(ctx)
205207

206208
// only in case the create bar API call is asynchronous (Make sure to include *ALL* fields which are part of the
207209
// internal terraform resource id! And please include the comment below in your code):
@@ -244,6 +246,7 @@ func (r *barResource) Read(ctx context.Context, req resource.ReadRequest, resp *
244246
if resp.Diagnostics.HasError() {
245247
return
246248
}
249+
ctx = core.InitProviderContext(ctx)
247250
projectId := model.ProjectId.ValueString()
248251
region := r.providerData.GetRegionWithOverride(model.Region)
249252
barId := model.BarId.ValueString()
@@ -256,6 +259,7 @@ func (r *barResource) Read(ctx context.Context, req resource.ReadRequest, resp *
256259
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading bar", fmt.Sprintf("Calling API: %v", err))
257260
return
258261
}
262+
ctx = core.LogResponse(ctx)
259263

260264
// Map response body to schema
261265
err = mapFields(barResp, &model)
@@ -285,6 +289,7 @@ func (r *barResource) Delete(ctx context.Context, req resource.DeleteRequest, re
285289
if resp.Diagnostics.HasError() {
286290
return
287291
}
292+
ctx = core.InitProviderContext(ctx)
288293
projectId := model.ProjectId.ValueString()
289294
region := model.Region.ValueString()
290295
barId := model.BarId.ValueString()
@@ -297,6 +302,7 @@ func (r *barResource) Delete(ctx context.Context, req resource.DeleteRequest, re
297302
if err != nil {
298303
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting bar", fmt.Sprintf("Calling API: %v", err))
299304
}
305+
ctx = core.LogResponse(ctx)
300306

301307
// only in case the bar delete API endpoint is asynchronous: use a wait handler to wait for the delete operation to complete
302308
_, err = wait.DeleteBarWaitHandler(ctx, r.client, projectId, region, barId).WaitWithContext(ctx)

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.17.3
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.17.3 h1:GsZGmRRc/3GJLmCUnsZswirr5wfLRrwavbnL/renOqg=
156-
github.com/stackitcloud/stackit-sdk-go/core v0.17.3/go.mod h1:HBCXJGPgdRulplDzhrmwC+Dak9B/x0nzNtmOpu+1Ahg=
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ func LogAndAddError(ctx context.Context, diags *diag.Diagnostics, summary, detai
112112

113113
// LogAndAddWarning Logs the warning and adds it to the diags
114114
func LogAndAddWarning(ctx context.Context, diags *diag.Diagnostics, summary, detail string) {
115+
if traceId := runtime.GetTraceId(ctx); traceId != "" {
116+
detail = fmt.Sprintf("%s\nTrace ID: %q", detail, traceId)
117+
}
115118
tflog.Warn(ctx, fmt.Sprintf("%s | %s", summary, detail))
116119
diags.AddWarning(summary, detail)
117120
}
@@ -140,6 +143,10 @@ func InitProviderContext(ctx context.Context) context.Context {
140143

141144
func LogResponse(ctx context.Context) context.Context {
142145
// Logs the trace-id of the request
143-
ctx = tflog.SetField(ctx, "x-trace-id", runtime.GetTraceId(ctx))
146+
traceId := runtime.GetTraceId(ctx)
147+
ctx = tflog.SetField(ctx, "x-trace-id", traceId)
148+
tflog.Debug(ctx, "response data", map[string]interface{}{
149+
"x-trace-id": traceId,
150+
})
144151
return ctx
145152
}

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

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

155+
ctx = core.InitProviderContext(ctx)
155156
ctx = r.annotateLogger(ctx, &model)
156157

157158
if err := r.checkDuplicate(ctx, model); err != nil {
@@ -170,6 +171,7 @@ func (r *roleAssignmentResource) Create(ctx context.Context, req resource.Create
170171
core.LogAndAddError(ctx, &resp.Diagnostics, fmt.Sprintf("Error creating %s role assignment", r.apiName), fmt.Sprintf("Calling API: %v", err))
171172
return
172173
}
174+
ctx = core.LogResponse(ctx)
173175

174176
// Map response body to schema
175177
err = mapMembersResponse(createResp, &model)
@@ -194,13 +196,15 @@ func (r *roleAssignmentResource) Read(ctx context.Context, req resource.ReadRequ
194196
return
195197
}
196198

199+
ctx = core.InitProviderContext(ctx)
197200
ctx = r.annotateLogger(ctx, &model)
198201

199202
listResp, err := r.authorizationClient.ListMembers(ctx, r.apiName, model.ResourceId.ValueString()).Subject(model.Subject.ValueString()).Execute()
200203
if err != nil {
201204
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading authorizations", fmt.Sprintf("Calling API: %v", err))
202205
return
203206
}
207+
ctx = core.LogResponse(ctx)
204208

205209
// Map response body to schema
206210
err = mapListMembersResponse(listResp, &model)
@@ -232,6 +236,7 @@ func (r *roleAssignmentResource) Delete(ctx context.Context, req resource.Delete
232236
return
233237
}
234238

239+
ctx = core.InitProviderContext(ctx)
235240
ctx = r.annotateLogger(ctx, &model)
236241

237242
payload := authorization.RemoveMembersPayload{
@@ -240,6 +245,7 @@ func (r *roleAssignmentResource) Delete(ctx context.Context, req resource.Delete
240245
*authorization.NewMember(model.Role.ValueString(), model.Subject.ValueString()),
241246
},
242247
}
248+
ctx = core.LogResponse(ctx)
243249

244250
// Delete existing project role assignment
245251
_, err := r.authorizationClient.RemoveMembers(ctx, model.ResourceId.ValueString()).RemoveMembersPayload(payload).Execute()

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

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

129+
ctx = core.InitProviderContext(ctx)
129130
projectId := model.ProjectId.ValueString()
130131
ctx = tflog.SetField(ctx, "project_id", projectId)
131132
distributionId := model.DistributionId.ValueString()
@@ -145,6 +146,7 @@ func (r *customDomainDataSource) Read(ctx context.Context, req datasource.ReadRe
145146
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN custom domain", fmt.Sprintf("Calling API: %v", err))
146147
return
147148
}
149+
ctx = core.LogResponse(ctx)
148150

149151
// Call the new data source mapping function
150152
err = mapCustomDomainDataSourceFields(customDomainResp, &model, projectId, distributionId)

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ func (r *customDomainResource) Create(ctx context.Context, req resource.CreateRe
186186
if resp.Diagnostics.HasError() {
187187
return
188188
}
189+
ctx = core.InitProviderContext(ctx)
189190
projectId := model.ProjectId.ValueString()
190191
ctx = tflog.SetField(ctx, "project_id", projectId)
191192
distributionId := model.DistributionId.ValueString()
@@ -207,6 +208,8 @@ func (r *customDomainResource) Create(ctx context.Context, req resource.CreateRe
207208
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN custom domain", fmt.Sprintf("Calling API: %v", err))
208209
return
209210
}
211+
ctx = core.LogResponse(ctx)
212+
210213
_, err = wait.CreateCDNCustomDomainWaitHandler(ctx, r.client, projectId, distributionId, name).SetTimeout(5 * time.Minute).WaitWithContext(ctx)
211214
if err != nil {
212215
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN custom domain", fmt.Sprintf("Waiting for create: %v", err))
@@ -240,6 +243,7 @@ func (r *customDomainResource) Read(ctx context.Context, req resource.ReadReques
240243
return
241244
}
242245

246+
ctx = core.InitProviderContext(ctx)
243247
projectId := model.ProjectId.ValueString()
244248
ctx = tflog.SetField(ctx, "project_id", projectId)
245249
distributionId := model.DistributionId.ValueString()
@@ -248,7 +252,6 @@ func (r *customDomainResource) Read(ctx context.Context, req resource.ReadReques
248252
ctx = tflog.SetField(ctx, "name", name)
249253

250254
customDomainResp, err := r.client.GetCustomDomain(ctx, projectId, distributionId, name).Execute()
251-
252255
if err != nil {
253256
var oapiErr *oapierror.GenericOpenAPIError
254257
// n.b. err is caught here if of type *oapierror.GenericOpenAPIError, which the stackit SDK client returns
@@ -261,6 +264,8 @@ func (r *customDomainResource) Read(ctx context.Context, req resource.ReadReques
261264
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN custom domain", fmt.Sprintf("Calling API: %v", err))
262265
return
263266
}
267+
ctx = core.LogResponse(ctx)
268+
264269
err = mapCustomDomainResourceFields(customDomainResp, &model)
265270
if err != nil {
266271
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN custom domain", fmt.Sprintf("Processing API payload: %v", err))
@@ -283,6 +288,7 @@ func (r *customDomainResource) Update(ctx context.Context, req resource.UpdateRe
283288
return
284289
}
285290

291+
ctx = core.InitProviderContext(ctx)
286292
projectId := model.ProjectId.ValueString()
287293
ctx = tflog.SetField(ctx, "project_id", projectId)
288294
distributionId := model.DistributionId.ValueString()
@@ -305,6 +311,7 @@ func (r *customDomainResource) Update(ctx context.Context, req resource.UpdateRe
305311
core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating CDN custom domain certificate", fmt.Sprintf("Calling API: %v", err))
306312
return
307313
}
314+
ctx = core.LogResponse(ctx)
308315

309316
_, err = wait.CreateCDNCustomDomainWaitHandler(ctx, r.client, projectId, distributionId, name).SetTimeout(5 * time.Minute).WaitWithContext(ctx)
310317
if err != nil {
@@ -338,6 +345,7 @@ func (r *customDomainResource) Delete(ctx context.Context, req resource.DeleteRe
338345
return
339346
}
340347

348+
ctx = core.InitProviderContext(ctx)
341349
projectId := model.ProjectId.ValueString()
342350
ctx = tflog.SetField(ctx, "project_id", projectId)
343351
distributionId := model.DistributionId.ValueString()
@@ -349,6 +357,7 @@ func (r *customDomainResource) Delete(ctx context.Context, req resource.DeleteRe
349357
if err != nil {
350358
core.LogAndAddError(ctx, &resp.Diagnostics, "Delete CDN custom domain", fmt.Sprintf("Delete custom domain: %v", err))
351359
}
360+
ctx = core.LogResponse(ctx)
352361
_, err = wait.DeleteCDNCustomDomainWaitHandler(ctx, r.client, projectId, distributionId, name).WaitWithContext(ctx)
353362
if err != nil {
354363
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ func (r *distributionDataSource) Read(ctx context.Context, req datasource.ReadRe
184184
return
185185
}
186186

187+
ctx = core.InitProviderContext(ctx)
187188
projectId := model.ProjectId.ValueString()
188189
distributionId := model.DistributionId.ValueString()
189190
distributionResp, err := r.client.GetDistributionExecute(ctx, projectId, distributionId)
@@ -199,6 +200,7 @@ func (r *distributionDataSource) Read(ctx context.Context, req datasource.ReadRe
199200
resp.State.RemoveResource(ctx)
200201
return
201202
}
203+
ctx = core.LogResponse(ctx)
202204
err = mapFields(ctx, distributionResp.Distribution, &model)
203205
if err != nil {
204206
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ func (r *distributionResource) Create(ctx context.Context, req resource.CreateRe
334334
if resp.Diagnostics.HasError() {
335335
return
336336
}
337+
ctx = core.InitProviderContext(ctx)
337338
projectId := model.ProjectId.ValueString()
338339
ctx = tflog.SetField(ctx, "project_id", projectId)
339340

@@ -348,6 +349,7 @@ func (r *distributionResource) Create(ctx context.Context, req resource.CreateRe
348349
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN distribution", fmt.Sprintf("Calling API: %v", err))
349350
return
350351
}
352+
ctx = core.LogResponse(ctx)
351353
waitResp, err := wait.CreateDistributionPoolWaitHandler(ctx, r.client, projectId, *createResp.Distribution.Id).SetTimeout(5 * time.Minute).WaitWithContext(ctx)
352354
if err != nil {
353355
core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating CDN distribution", fmt.Sprintf("Waiting for create: %v", err))
@@ -376,6 +378,7 @@ func (r *distributionResource) Read(ctx context.Context, req resource.ReadReques
376378
return
377379
}
378380

381+
ctx = core.InitProviderContext(ctx)
379382
projectId := model.ProjectId.ValueString()
380383
distributionId := model.DistributionId.ValueString()
381384
ctx = tflog.SetField(ctx, "project_id", projectId)
@@ -394,6 +397,7 @@ func (r *distributionResource) Read(ctx context.Context, req resource.ReadReques
394397
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN distribution", fmt.Sprintf("Calling API: %v", err))
395398
return
396399
}
400+
ctx = core.LogResponse(ctx)
397401
err = mapFields(ctx, cdnResp.Distribution, &model)
398402
if err != nil {
399403
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading CDN ditribution", fmt.Sprintf("Processing API payload: %v", err))

stackit/internal/services/dns/recordset/datasource.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ func (d *recordSetDataSource) Read(ctx context.Context, req datasource.ReadReque
136136
if resp.Diagnostics.HasError() {
137137
return
138138
}
139+
ctx = core.InitProviderContext(ctx)
139140
projectId := model.ProjectId.ValueString()
140141
zoneId := model.ZoneId.ValueString()
141142
recordSetId := model.RecordSetId.ValueString()
@@ -157,6 +158,7 @@ func (d *recordSetDataSource) Read(ctx context.Context, req datasource.ReadReque
157158
resp.State.RemoveResource(ctx)
158159
return
159160
}
161+
ctx = core.LogResponse(ctx)
160162
if recordSetResp != nil && recordSetResp.Rrset.State != nil && *recordSetResp.Rrset.State == dns.RECORDSETSTATE_DELETE_SUCCEEDED {
161163
resp.State.RemoveResource(ctx)
162164
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading record set", "Record set was deleted successfully")

0 commit comments

Comments
 (0)