|
| 1 | +package datasources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/formancehq/go-libs/v3/logging" |
| 10 | + "github.com/formancehq/terraform-provider-cloud/pkg/tracing" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 12 | + "go.opentelemetry.io/otel/propagation" |
| 13 | + "go.opentelemetry.io/otel/trace" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + _ datasource.DataSource = &DatasourcesTracer{} |
| 18 | + _ datasource.DataSourceWithConfigure = &DatasourcesTracer{} |
| 19 | + _ datasource.DataSourceWithConfigValidators = &DatasourcesTracer{} |
| 20 | +) |
| 21 | + |
| 22 | +type DatasourcesTracer struct { |
| 23 | + tracer trace.Tracer |
| 24 | + logger logging.Logger |
| 25 | + underlyingValue any |
| 26 | +} |
| 27 | + |
| 28 | +func injectTraceContext(ctx context.Context, res any, funcName string) context.Context { |
| 29 | + headerCarrier := propagation.MapCarrier{} |
| 30 | + propagation.TraceContext{}.Inject(ctx, headerCarrier) |
| 31 | + |
| 32 | + for k, v := range headerCarrier { |
| 33 | + ctx = logging.ContextWithField(ctx, k, v) |
| 34 | + } |
| 35 | + |
| 36 | + name := reflect.TypeOf(res).Elem().Name() |
| 37 | + ctx = logging.ContextWithField(ctx, "resource", strings.ToLower(name)) |
| 38 | + ctx = logging.ContextWithField(ctx, "operation", strings.ToLower(funcName)) |
| 39 | + return ctx |
| 40 | +} |
| 41 | + |
| 42 | +var ( |
| 43 | + ErrConfigure = fmt.Errorf("error in configure") |
| 44 | + ErrRead = fmt.Errorf("error in read") |
| 45 | + ErrSchema = fmt.Errorf("error in schema") |
| 46 | + ErrMetadata = fmt.Errorf("error in metadata") |
| 47 | + ErrValidate = fmt.Errorf("error in validate") |
| 48 | +) |
| 49 | + |
| 50 | +// Configure implements datasource.DataSourceWithConfigure. |
| 51 | +func (d *DatasourcesTracer) Configure(ctx context.Context, req datasource.ConfigureRequest, res *datasource.ConfigureResponse) { |
| 52 | + ctx = logging.ContextWithLogger(ctx, d.logger) |
| 53 | + operation := "Configure" |
| 54 | + if v, ok := d.underlyingValue.(datasource.DataSourceWithConfigure); ok { |
| 55 | + _ = tracing.TraceError(ctx, d.tracer, operation, func(ctx context.Context) error { |
| 56 | + ctx = injectTraceContext(ctx, d.underlyingValue, operation) |
| 57 | + logging.FromContext(ctx).Debugf(operation + " called") |
| 58 | + defer logging.FromContext(ctx).Debugf(operation + " completed") |
| 59 | + v.Configure(ctx, req, res) |
| 60 | + if res.Diagnostics.HasError() { |
| 61 | + return ErrConfigure |
| 62 | + } |
| 63 | + return nil |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// ConfigValidators implements datasource.DataSourceWithConfigValidators. |
| 69 | +func (d *DatasourcesTracer) ConfigValidators(ctx context.Context) []datasource.ConfigValidator { |
| 70 | + ctx = logging.ContextWithLogger(ctx, d.logger) |
| 71 | + operation := "ConfigValidators" |
| 72 | + if v, ok := d.underlyingValue.(datasource.DataSourceWithConfigValidators); ok { |
| 73 | + ret, _ := tracing.Trace(ctx, d.tracer, operation, func(ctx context.Context) ([]datasource.ConfigValidator, error) { |
| 74 | + ctx = injectTraceContext(ctx, d.underlyingValue, operation) |
| 75 | + logging.FromContext(ctx).Debugf(operation + " called") |
| 76 | + defer logging.FromContext(ctx).Debugf(operation + " completed") |
| 77 | + return v.ConfigValidators(ctx), nil |
| 78 | + }) |
| 79 | + return ret |
| 80 | + } |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +// Metadata implements datasource.DataSourceWithConfigValidators. |
| 85 | +func (d *DatasourcesTracer) Metadata(ctx context.Context, req datasource.MetadataRequest, res *datasource.MetadataResponse) { |
| 86 | + ctx = logging.ContextWithLogger(ctx, d.logger) |
| 87 | + operation := "Metadata" |
| 88 | + if v, ok := d.underlyingValue.(datasource.DataSource); ok { |
| 89 | + _ = tracing.TraceError(ctx, d.tracer, operation, func(ctx context.Context) error { |
| 90 | + ctx = injectTraceContext(ctx, d.underlyingValue, operation) |
| 91 | + logging.FromContext(ctx).Debugf(operation + " called") |
| 92 | + defer logging.FromContext(ctx).Debugf(operation + " completed") |
| 93 | + v.Metadata(ctx, req, res) |
| 94 | + return nil |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Read implements datasource.DataSourceWithConfigValidators. |
| 100 | +func (d *DatasourcesTracer) Read(ctx context.Context, req datasource.ReadRequest, res *datasource.ReadResponse) { |
| 101 | + ctx = logging.ContextWithLogger(ctx, d.logger) |
| 102 | + operation := "Read" |
| 103 | + if v, ok := d.underlyingValue.(datasource.DataSource); ok { |
| 104 | + _ = tracing.TraceError(ctx, d.tracer, operation, func(ctx context.Context) error { |
| 105 | + ctx = injectTraceContext(ctx, d.underlyingValue, operation) |
| 106 | + logging.FromContext(ctx).Debugf(operation + " called") |
| 107 | + defer logging.FromContext(ctx).Debugf(operation + " completed") |
| 108 | + v.Read(ctx, req, res) |
| 109 | + if res.Diagnostics.HasError() { |
| 110 | + return ErrRead |
| 111 | + } |
| 112 | + return nil |
| 113 | + }) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// Schema implements datasource.DataSourceWithConfigValidators. |
| 118 | +func (d *DatasourcesTracer) Schema(ctx context.Context, req datasource.SchemaRequest, res *datasource.SchemaResponse) { |
| 119 | + ctx = logging.ContextWithLogger(ctx, d.logger) |
| 120 | + operation := "Schema" |
| 121 | + if v, ok := d.underlyingValue.(datasource.DataSource); ok { |
| 122 | + _ = tracing.TraceError(ctx, d.tracer, operation, func(ctx context.Context) error { |
| 123 | + ctx = injectTraceContext(ctx, d.underlyingValue, operation) |
| 124 | + logging.FromContext(ctx).Debugf(operation + " called") |
| 125 | + defer logging.FromContext(ctx).Debugf(operation + " completed") |
| 126 | + v.Schema(ctx, req, res) |
| 127 | + if res.Diagnostics.HasError() { |
| 128 | + return ErrSchema |
| 129 | + } |
| 130 | + return nil |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func NewDatasourcesTracer(tracer trace.Tracer, logger logging.Logger, res func() datasource.DataSource) *DatasourcesTracer { |
| 136 | + return &DatasourcesTracer{ |
| 137 | + tracer: tracer, |
| 138 | + logger: logger, |
| 139 | + underlyingValue: res(), |
| 140 | + } |
| 141 | +} |
0 commit comments