diff --git a/cmd/dmd/cmd/preflightchecks.go b/cmd/dmd/cmd/preflightchecks.go index 838501ae6b561c9ea102427afe31efd8d507d4e4..8c1daf8428536569f10daacb9553079042ed1fde 100644 --- a/cmd/dmd/cmd/preflightchecks.go +++ b/cmd/dmd/cmd/preflightchecks.go @@ -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 } diff --git a/internal/tracing/attributes.go b/internal/tracing/attributes.go index 9775822784a47ef29de99a54768ba144ce8e4968..430895bf97b9cccc72694ab8e5e71eed02cb1f4c 100644 --- a/internal/tracing/attributes.go +++ b/internal/tracing/attributes.go @@ -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" diff --git a/metadata/compatible_since.go b/metadata/compatible_since.go index 93f3d744f4a29eaca5f1f265a5e4d9a0253846f5..674c4c50c020d3ea3ad83da3df66da41f79cc1e0 100644 --- a/metadata/compatible_since.go +++ b/metadata/compatible_since.go @@ -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)