Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tfprotov5/tf5server+tfprotov6/tf6server: Add downstream RPC request d…
- Loading branch information
Showing
20 changed files
with
1,967 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
```release-note:enhancement | ||
tfprotov5/tf5server: Added downstream RPC request duration and response diagnostics logging | ||
``` | ||
|
||
```release-note:enhancement | ||
tfprotov6/tf6server: Added downstream RPC request duration and response diagnostics logging | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package diag | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-go/internal/logging" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
) | ||
|
||
// Diagnostics is a collection of Diagnostic. | ||
type Diagnostics []*tfprotov5.Diagnostic | ||
|
||
// ErrorCount returns the number of error severity diagnostics. | ||
func (d Diagnostics) ErrorCount() int { | ||
var result int | ||
|
||
for _, diagnostic := range d { | ||
if diagnostic == nil { | ||
continue | ||
} | ||
|
||
if diagnostic.Severity != tfprotov5.DiagnosticSeverityError { | ||
continue | ||
} | ||
|
||
result++ | ||
} | ||
|
||
return result | ||
} | ||
|
||
// Log will log every diagnostic: | ||
// | ||
// - Error severity at ERROR level | ||
// - Warning severity at WARN level | ||
// - Invalid/Unknown severity at WARN level | ||
// | ||
func (d Diagnostics) Log(ctx context.Context) { | ||
for _, diagnostic := range d { | ||
if diagnostic == nil { | ||
continue | ||
} | ||
|
||
diagnosticFields := map[string]interface{}{ | ||
logging.KeyDiagnosticDetail: diagnostic.Detail, | ||
logging.KeyDiagnosticSeverity: diagnostic.Severity.String(), | ||
logging.KeyDiagnosticSummary: diagnostic.Summary, | ||
} | ||
|
||
if diagnostic.Attribute != nil { | ||
diagnosticFields[logging.KeyDiagnosticAttribute] = diagnostic.Attribute.String() | ||
} | ||
|
||
switch diagnostic.Severity { | ||
case tfprotov5.DiagnosticSeverityError: | ||
logging.ProtocolError(ctx, "Response contains error diagnostic", diagnosticFields) | ||
case tfprotov5.DiagnosticSeverityWarning: | ||
logging.ProtocolWarn(ctx, "Response contains warning diagnostic", diagnosticFields) | ||
default: | ||
logging.ProtocolWarn(ctx, "Response contains unknown diagnostic", diagnosticFields) | ||
} | ||
} | ||
} | ||
|
||
// WarningCount returns the number of warning severity diagnostics. | ||
func (d Diagnostics) WarningCount() int { | ||
var result int | ||
|
||
for _, diagnostic := range d { | ||
if diagnostic == nil { | ||
continue | ||
} | ||
|
||
if diagnostic.Severity != tfprotov5.DiagnosticSeverityWarning { | ||
continue | ||
} | ||
|
||
result++ | ||
} | ||
|
||
return result | ||
} |
Oops, something went wrong.