Skip to content

Commit

Permalink
internal/civisibility: starts the work of requesting backend for sett…
Browse files Browse the repository at this point in the history
…ings and early flake detection data at CI Visibility initialization.
  • Loading branch information
tonyredondo committed Sep 26, 2024
1 parent ac73f9b commit 50e8be3
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
19 changes: 14 additions & 5 deletions internal/civisibility/integrations/civisibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package integrations

import (
"fmt"
"os"
"os/signal"
"regexp"
Expand Down Expand Up @@ -61,27 +62,31 @@ func internalCiVisibilityInitialization(tracerInitializer func([]tracer.StartOpt
// Avoid sampling rate warning (in CI Visibility mode we send all data)
_ = os.Setenv("DD_TRACE_SAMPLE_RATE", "1")

// Preload the CodeOwner file
_ = utils.GetCodeOwners()

// Preload all CI, Git, and CodeOwners tags.
ciTags := utils.GetCITags()
_ = utils.GetCIMetrics()

// Check if DD_SERVICE has been set; otherwise default to the repo name (from the spec).
var opts []tracer.StartOption
if v := os.Getenv("DD_SERVICE"); v == "" {
serviceName := os.Getenv("DD_SERVICE")
if serviceName == "" {
if repoURL, ok := ciTags[constants.GitRepositoryURL]; ok {
// regex to sanitize the repository url to be used as a service name
repoRegex := regexp.MustCompile(`(?m)/([a-zA-Z0-9\-_.]*)$`)
matches := repoRegex.FindStringSubmatch(repoURL)
if len(matches) > 1 {
repoURL = strings.TrimSuffix(matches[1], ".git")
}
opts = append(opts, tracer.WithService(repoURL))
serviceName = repoURL
opts = append(opts, tracer.WithService(serviceName))
}
}

wChn := initializeBackendRequests(serviceName)

// Preload the CodeOwner file
_ = utils.GetCodeOwners()

// Initialize the tracer
tracerInitializer(opts)

Expand All @@ -93,6 +98,10 @@ func internalCiVisibilityInitialization(tracerInitializer func([]tracer.StartOpt
ExitCiVisibility()
os.Exit(1)
}()

<-wChn
fmt.Println(*ciVisibilitySettings)
fmt.Println(*ciVisibilityEfdData)
})
}

Expand Down
54 changes: 54 additions & 0 deletions internal/civisibility/integrations/civisibility_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2024 Datadog, Inc.

package integrations

import (
"gopkg.in/DataDog/dd-trace-go.v1/internal/civisibility/utils/net"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
)

var (
// ciVisibilityRapidClient contains the http rapid client to do CI Visibility querys and upload to the rapid backend
ciVisibilityClient net.Client

// ciVisibilitySettings contains the CI Visibility settings for this session
ciVisibilitySettings *net.SettingsResponseData

// ciVisibilityEfdData contains the CI Visibility Early Flake Detection data for this session
ciVisibilityEfdData *net.EfdResponseData
)

func initializeBackendRequests(serviceName string) chan struct{} {
// Create the CI Visibility client
ciVisibilityClient = net.NewClientWithServiceName(serviceName)

// channel to wait for the settings request and (if required) the early flake detection request
settingsAndEfdChannel := make(chan struct{})
go func() {
// Get the CI Visibility settings payload for this test session
var err error
ciVisibilitySettings, err = ciVisibilityClient.GetSettings()
if err != nil {
log.Error("error getting CI visibility settings: %v", err)
ciVisibilitySettings = &net.SettingsResponseData{}
}

// if early flake detection is enabled then we run the early flake detection request
if ciVisibilitySettings.EarlyFlakeDetection.Enabled {
ciVisibilityEfdData, err = ciVisibilityClient.GetEarlyFlakeDetectionData()
if err != nil {
log.Error("error getting CI visibility early flake detection data: %v", err)
ciVisibilityEfdData = &net.EfdResponseData{}
}
} else {
ciVisibilityEfdData = &net.EfdResponseData{}
}

settingsAndEfdChannel <- struct{}{}
}()

return settingsAndEfdChannel
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var mTracer mocktracer.Tracer
// TestMain is the entry point for testing and runs before any test.
func TestMain(m *testing.M) {
currentM = m
integrations.EnsureCiVisibilityInitialization()
mTracer = integrations.InitializeCIVisibilityMock()

// (*M)(m).Run() cast m to gotesting.M and just run
Expand Down
24 changes: 15 additions & 9 deletions internal/civisibility/utils/net/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type (

var _ Client = &client{}

func NewClient() Client {
func NewClientWithServiceName(serviceName string) Client {
ciTags := utils.GetCITags()

// get the environment
Expand All @@ -75,16 +75,18 @@ func NewClient() Client {
}

// get the service name
serviceName := os.Getenv("DD_SERVICE")
if serviceName == "" {
if repoURL, ok := ciTags[constants.GitRepositoryURL]; ok {
// regex to sanitize the repository url to be used as a service name
repoRegex := regexp.MustCompile(`(?m)/([a-zA-Z0-9\-_.]*)$`)
matches := repoRegex.FindStringSubmatch(repoURL)
if len(matches) > 1 {
repoURL = strings.TrimSuffix(matches[1], ".git")
serviceName = os.Getenv("DD_SERVICE")
if serviceName == "" {
if repoURL, ok := ciTags[constants.GitRepositoryURL]; ok {
// regex to sanitize the repository url to be used as a service name
repoRegex := regexp.MustCompile(`(?m)/([a-zA-Z0-9\-_.]*)$`)
matches := repoRegex.FindStringSubmatch(repoURL)
if len(matches) > 1 {
repoURL = strings.TrimSuffix(matches[1], ".git")
}
serviceName = repoURL
}
serviceName = repoURL
}
}

Expand Down Expand Up @@ -204,6 +206,10 @@ func NewClient() Client {
}
}

func NewClient() Client {
return NewClientWithServiceName("")
}

func (c *client) getURLPath(urlPath string) string {
if c.agentless {
return fmt.Sprintf("%s/%s", c.baseURL, urlPath)
Expand Down

0 comments on commit 50e8be3

Please sign in to comment.