From 8dd08efcded5291da679e00b2b902361ce6e8ee4 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 9 Oct 2023 17:51:27 -0700 Subject: [PATCH] rpcapi(stacks): Additional OpenTelemetry tracing metadata During PlanStackChanges and ApplyStackChanges we'll mark the trace span representing the planning or applying of a particular component instance as either OK or Error status depending on which of the two terminal hooks get called. We'll also emit a trace event for each resource instance we plan. Ideally these would have their own spans too but we're currently limited by the design of the terraform.Hook API which wasn't designed with tracing spans in mind, and so we'll just use point-in-time events for now and rely on manual time-based correlation to try to associate downstream spans with particular resource instance operations if needed. --- internal/rpcapi/stacks.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/rpcapi/stacks.go b/internal/rpcapi/stacks.go index df8ff6b64a..fa743cd15b 100644 --- a/internal/rpcapi/stacks.go +++ b/internal/rpcapi/stacks.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/go-slug/sourceaddrs" "github.com/hashicorp/go-slug/sourcebundle" "go.opentelemetry.io/otel/attribute" + otelCodes "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/trace" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" @@ -552,11 +553,13 @@ func stackChangeHooks(send func(*terraform1.StackChangeProgress) error, mainStac }, EndComponentInstancePlan: func(ctx context.Context, span any, ci stackaddrs.AbsComponentInstance) any { send(evtComponentInstanceStatus(ci, hooks.ComponentInstancePlanned)) + span.(trace.Span).SetStatus(otelCodes.Ok, "planning succeeded") span.(trace.Span).End() return nil }, ErrorComponentInstancePlan: func(ctx context.Context, span any, ci stackaddrs.AbsComponentInstance) any { send(evtComponentInstanceStatus(ci, hooks.ComponentInstanceErrored)) + span.(trace.Span).SetStatus(otelCodes.Error, "planning failed") span.(trace.Span).End() return nil }, @@ -572,11 +575,13 @@ func stackChangeHooks(send func(*terraform1.StackChangeProgress) error, mainStac }, EndComponentInstanceApply: func(ctx context.Context, span any, ci stackaddrs.AbsComponentInstance) any { send(evtComponentInstanceStatus(ci, hooks.ComponentInstanceApplied)) + span.(trace.Span).SetStatus(otelCodes.Ok, "applying succeeded") span.(trace.Span).End() return nil }, ErrorComponentInstanceApply: func(ctx context.Context, span any, ci stackaddrs.AbsComponentInstance) any { send(evtComponentInstanceStatus(ci, hooks.ComponentInstanceErrored)) + span.(trace.Span).SetStatus(otelCodes.Error, "applying failed") span.(trace.Span).End() return nil }, @@ -607,6 +612,11 @@ func stackChangeHooks(send func(*terraform1.StackChangeProgress) error, mainStac return span } + span.(trace.Span).AddEvent("planned resource instance", trace.WithAttributes( + attribute.String("component_instance", ric.Addr.Component.String()), + attribute.String("resource_instance", ric.Addr.Item.String()), + )) + moved := &terraform1.StackChangeProgress_ResourceInstancePlannedChange_Moved{} if !ric.Change.PrevRunAddr.Equal(ric.Change.Addr) { moved.PrevAddr = &terraform1.ResourceInstanceInStackAddr{