Skip to content
Snippets Groups Projects
Commit 09e9fcf2 authored by Jamie Tanna's avatar Jamie Tanna
Browse files

chore(telemetry): instrument pre-flight checks

As part of #621.
parent adf93452
No related branches found
No related tags found
Loading
......@@ -8,11 +8,15 @@ import (
"sync"
"dmd.tanna.dev/internal/metadata/db"
"dmd.tanna.dev/internal/tracing"
"dmd.tanna.dev/metadata"
)
func preFlightChecks(ctx context.Context, sqlDB *sql.DB) {
sync.OnceFunc(func() {
ctx, span := tracer.Start(ctx, "Performing pre-flight checks")
defer span.End()
if sqlDB == nil {
logger.Debug("Skipping preFlightChecks because provided *sql.DB was <nil>")
return
......@@ -40,6 +44,9 @@ func preFlightChecks(ctx context.Context, sqlDB *sql.DB) {
}
func getDBVersion(ctx context.Context, sqlDB *sql.DB) string {
ctx, span := tracer.Start(ctx, "Determing version of `dmd` used to create the database")
defer span.End()
q := db.New(sqlDB)
data, err := q.RetrieveDMDVersion(ctx)
if errors.Is(err, sql.ErrNoRows) {
......@@ -49,18 +56,26 @@ func getDBVersion(ctx context.Context, sqlDB *sql.DB) string {
return ""
}
span.SetAttributes(tracing.AttributeKeyDMDDatabaseMetadataVersion.String(data.Value))
return data.Value
}
func isDatabaseFinalised(ctx context.Context, sqlDB *sql.DB) bool {
ctx, span := tracer.Start(ctx, "Determing if the database is finalised")
defer span.End()
q := db.New(sqlDB)
_, err := q.RetrieveFinalisedAt(ctx)
if errors.Is(err, sql.ErrNoRows) {
span.SetAttributes(tracing.AttributeKeyDMDDatabaseMetadataFinalised.Bool(false))
return false
} else if err != nil {
logger.Error(fmt.Sprintf("Failed to look up `finalised_at` in the metadata table: %v", err))
return false
}
span.SetAttributes(tracing.AttributeKeyDMDDatabaseMetadataFinalised.Bool(true))
return true
}
......@@ -20,13 +20,17 @@ const (
AttributeKeyCobraSubcommand attribute.Key = "cobra.subcommand"
AttributeKeyCobraCommandPath attribute.Key = "cobra.commandpath"
AttributeKeyDMDDependenciesCount attribute.Key = "dmd.dependencies.count"
AttributeKeyDMDDependencyUpdatesCount attribute.Key = "dmd.dependencyUpdates.count"
AttributeKeyDMDAdvisories attribute.Key = "dmd.advisories"
AttributeKeyDMDDatasource attribute.Key = "dmd.datasource"
AttributeKeyDMDCustomAdvisoryName attribute.Key = "dmd.customAdvisories.name"
AttributeKeyDMDPolicyName attribute.Key = "dmd.policy.name"
AttributeKeyDMDSBOMFormat attribute.Key = "dmd.sbom.format"
AttributeKeyDMDDependenciesCount attribute.Key = "dmd.dependencies.count"
AttributeKeyDMDDependencyUpdatesCount attribute.Key = "dmd.dependencyUpdates.count"
AttributeKeyDMDAdvisories attribute.Key = "dmd.advisories"
AttributeKeyDMDDatasource attribute.Key = "dmd.datasource"
AttributeKeyDMDCustomAdvisoryName attribute.Key = "dmd.customAdvisories.name"
AttributeKeyDMDPolicyName attribute.Key = "dmd.policy.name"
AttributeKeyDMDSBOMFormat attribute.Key = "dmd.sbom.format"
AttributeKeyDMDCLIVersion attribute.Key = "dmd.cli.version"
AttributeKeyDMDDatabaseMetadataCompatibleSinceVersion attribute.Key = "dmd.dbmeta.compatibleSince.version"
AttributeKeyDMDDatabaseMetadataVersion attribute.Key = "dmd.dbmeta.dmd.version"
AttributeKeyDMDDatabaseMetadataFinalised attribute.Key = "dmd.dbmeta.finalised"
AttributeKeyEndoflifedateProduct attribute.Key = "endoflifedate.product"
AttributeKeyEndoflifedateCycle attribute.Key = "endoflifedate.cycle"
......
......@@ -7,9 +7,14 @@ import (
"fmt"
"dmd.tanna.dev/internal/metadata/db"
"dmd.tanna.dev/internal/tracing"
"github.com/hashicorp/go-version"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
var tracer = otel.Tracer("dmd.tanna.dev/metadata")
// CompatibleSince defines the Compatible Since version, which is used by the `compatible_since` field in `metadata`. See https://dmd.tanna.dev/concepts/compatible-since/ for more details
const CompatibleSince = "v0.100.0"
......@@ -21,6 +26,11 @@ var ErrNoCompatibleSince = errors.New("metadata: no `compatible_since` was found
// An error is returned, which is either `ErrNoCompatibleSince`, to indicate that the `compatible_since` field could not be found in the database, or an error when retrieving or parsing the resulting `compatible_since`
// Otherwise, `compatible` is whether you should be able to safely use the versions together, and `compatibleSince` is the `compatible_since` value for reference
func IsCompatible(ctx context.Context, sqlDB *sql.DB, currentVersion string) (compatible bool, compatibleSince string, err error) {
ctx, span := tracer.Start(ctx, "Determing if the database is compatible with the version of the `dmd` CLI", trace.WithAttributes(
tracing.AttributeKeyDMDCLIVersion.String(currentVersion),
))
defer span.End()
q := db.New(sqlDB)
c, err := q.RetrieveCompatibleSince(ctx)
......@@ -30,6 +40,8 @@ func IsCompatible(ctx context.Context, sqlDB *sql.DB, currentVersion string) (co
return false, "", fmt.Errorf("failed to retrieve the `compatible_since` from the database: %v", err)
}
span.SetAttributes(tracing.AttributeKeyDMDDatabaseMetadataCompatibleSinceVersion.String(c.Value))
cliVer, err := version.NewVersion(currentVersion)
if err != nil {
return false, "", fmt.Errorf("failed to parse CLI version provided, %v, as a version: %v", currentVersion, err)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment