Skip to content

Commit 68dd7cd

Browse files
Publish analysis utils
PiperOrigin-RevId: 883189301
1 parent 05db8d8 commit 68dd7cd

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

analysis/util/alertwindow.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Package alertwindow provides utility functions for getting the alert time window.
2+
package alertwindow
3+
4+
import (
5+
"context"
6+
"fmt"
7+
"os"
8+
"time"
9+
10+
cdenv "github.com/GoogleCloudPlatform/cloud-deploy-samples/packages/cdenv"
11+
cdapi "google.golang.org/api/clouddeploy/v1"
12+
)
13+
14+
// AlertTimeWindow holds the rollout start time and the end time to look for alerts (which is now).
15+
type AlertTimeWindow struct {
16+
// StartTime is the start time of the alert window in RFC3339 format.
17+
StartTime time.Time
18+
// EndTime is the end time of the alert window in RFC3339 format.
19+
EndTime time.Time
20+
}
21+
22+
// TimeWindow returns the start time of the rollout as a string representing the number of
23+
// milliseconds since the Unix epoch (this is the time format expected by Datadog).
24+
func TimeWindow(ctx context.Context) (*AlertTimeWindow, error) {
25+
// Construct the rollout resource name.
26+
projectID := os.Getenv(cdenv.ProjectIDEnvKey)
27+
location := os.Getenv(cdenv.LocationEnvKey)
28+
pipelineID := os.Getenv(cdenv.PipelineEnvKey)
29+
releaseID := os.Getenv(cdenv.ReleaseEnvKey)
30+
rolloutID := os.Getenv(cdenv.RolloutEnvKey)
31+
32+
rolloutName := fmt.Sprintf("projects/%s/locations/%s/deliveryPipelines/%s/releases/%s/rollouts/%s",
33+
projectID, location, pipelineID, releaseID, rolloutID)
34+
35+
cdService, err := cdapi.NewService(ctx)
36+
if err != nil {
37+
return nil, fmt.Errorf("unable to create Cloud Deploy API service: %v", err)
38+
}
39+
40+
rollout, err := cdService.Projects.Locations.DeliveryPipelines.Releases.Rollouts.Get(rolloutName).Do()
41+
if err != nil {
42+
return nil, fmt.Errorf("unable to get rollout from Cloud Deploy API: %v", err)
43+
}
44+
45+
parsedStartTime, err := time.Parse(time.RFC3339, rollout.DeployStartTime)
46+
if err != nil {
47+
return nil, fmt.Errorf("unable to convert start time to RFC3339 format in order to validate time")
48+
}
49+
50+
endTime := time.Now().Format(time.RFC3339)
51+
parsedEndTime, err := time.Parse(time.RFC3339, endTime)
52+
if err != nil {
53+
return nil, fmt.Errorf("unable to convert start time to RFC3339 format in order to validate time")
54+
}
55+
if parsedStartTime.After(parsedEndTime) {
56+
return nil, fmt.Errorf("start time is after end time")
57+
}
58+
59+
return &AlertTimeWindow{StartTime: parsedStartTime, EndTime: parsedEndTime}, nil
60+
}

analysis/util/gcs.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Package gcs provides utilities for uploading analysis results to GCS.
2+
package gcs
3+
4+
import (
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"os"
9+
10+
"cloud.google.com/go/storage"
11+
cdenv "github.com/GoogleCloudPlatform/cloud-deploy-samples/packages/cdenv"
12+
"github.com/GoogleCloudPlatform/cloud-deploy-samples/packages/gcs"
13+
)
14+
15+
// AnalysisMetadata represents the Analysis result metadata that will be uploaded to GCS.
16+
type AnalysisMetadata struct {
17+
// Metadata contains metadata associated with the analysis result.
18+
Metadata map[string]string `json:"metadata,omitempty"`
19+
}
20+
21+
// UploadResult uploads the result to GCS.
22+
func UploadResult(ctx context.Context, analysisMetadata *AnalysisMetadata, client *storage.Client) error {
23+
data, err := json.Marshal(analysisMetadata)
24+
if err != nil {
25+
return fmt.Errorf("failed to marshal analysis metadata: %v, error: %w", analysisMetadata, err)
26+
}
27+
// Get the GCS URI where the results file should be uploaded. The full path is in the format of
28+
// {outputPath}/{gcs.ResultObjectSuffix}.
29+
outputPath := os.Getenv(cdenv.OutputGCSEnvKey)
30+
uri := fmt.Sprintf("%s/%s", outputPath, gcs.ResultObjectSuffix)
31+
return gcs.Upload(ctx, client, uri, &gcs.UploadContent{Data: data})
32+
}

go.mod

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ require (
3232
cloud.google.com/go/iam v1.5.2 // indirect
3333
cloud.google.com/go/longrunning v0.6.7 // indirect
3434
cloud.google.com/go/pubsub v1.49.0 // indirect
35-
github.com/DataDog/datadog-api-client-go/v2 v2.55.0 // indirect
36-
github.com/DataDog/zstd v1.5.2 // indirect
3735
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0 // indirect
3836
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.51.0 // indirect
3937
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.51.0 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ cloud.google.com/go/storage v1.55.0/go.mod h1:ztSmTTwzsdXe5syLVS0YsbFxXuvEmEyZj7
3030
cloud.google.com/go/trace v1.11.6 h1:2O2zjPzqPYAHrn3OKl029qlqG6W8ZdYaOWRyr8NgMT4=
3131
cloud.google.com/go/trace v1.11.6/go.mod h1:GA855OeDEBiBMzcckLPE2kDunIpC72N+Pq8WFieFjnI=
3232
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
33-
github.com/DataDog/datadog-api-client-go/v2 v2.55.0 h1:wRwBJJpNRoyBJXdKtInfHXeXIo+nTn5DSIayaovRYVY=
34-
github.com/DataDog/datadog-api-client-go/v2 v2.55.0/go.mod h1:d3tOEgUd2kfsr9uuHQdY+nXrWp4uikgTgVCPdKNK30U=
35-
github.com/DataDog/zstd v1.5.2 h1:vUG4lAyuPCXO0TLbXvPv7EB7cNK1QV/luu55UHLrrn8=
36-
github.com/DataDog/zstd v1.5.2/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
3733
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0 h1:ErKg/3iS1AKcTkf3yixlZ54f9U1rljCkQyEXWUnIUxc=
3834
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.27.0/go.mod h1:yAZHSGnqScoU556rBOVkwLze6WP5N+U11RHuWaGVxwY=
3935
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.51.0 h1:fYE9p3esPxA/C0rQ0AHhP0drtPXDRhaWiwg1DPqO7IU=

0 commit comments

Comments
 (0)