Skip to content

Commit

Permalink
telemetry updates (#156)
Browse files Browse the repository at this point in the history
Signed-off-by: Kim Tsao <ktsao@redhat.com>

Signed-off-by: Kim Tsao <ktsao@redhat.com>
  • Loading branch information
kim-tsao committed Nov 24, 2022
1 parent c8f6c26 commit 8e009f5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
4 changes: 3 additions & 1 deletion pkg/devfile/parser/context/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func (d *DevfileCtx) SetDevfileContent() error {
var err error
var data []byte
if d.url != "" {
data, err = util.DownloadFileInMemory(d.url)
// set the client identifier for telemetry
params := util.HTTPRequestParams{URL: d.url, TelemetryClientName: util.TelemetryClientName}
data, err = util.DownloadInMemory(params)
if err != nil {
return errors.Wrap(err, "error getting devfile info from url")
}
Expand Down
18 changes: 11 additions & 7 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/devfile/api/v2/pkg/attributes"
registryLibrary "github.com/devfile/registry-support/registry-library/library"
"io/ioutil"
"net/url"
"os"
"path"
"strings"

"github.com/devfile/api/v2/pkg/attributes"
registryLibrary "github.com/devfile/registry-support/registry-library/library"

"reflect"

devfileCtx "github.com/devfile/library/v2/pkg/devfile/parser/context"
"github.com/devfile/library/v2/pkg/devfile/parser/data"
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
"github.com/devfile/library/v2/pkg/util"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/client"

v1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
Expand Down Expand Up @@ -520,7 +522,8 @@ func getDevfileFromRegistry(id, registryURL, version string, httpTimeout *int) (
}

param.Timeout = httpTimeout

//suppress telemetry for parent uri references
param.TelemetryClientName = util.TelemetryIndirectDevfileCall
return util.HTTPGetRequest(param, 0)
}

Expand All @@ -530,8 +533,8 @@ func getResourcesFromRegistry(id, registryURL, destDir string) error {
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
}
defer os.RemoveAll(stackDir)

err = registryLibrary.PullStackFromRegistry(registryURL, id, stackDir, registryLibrary.RegistryOptions{})
//suppress telemetry for downloading resources from parent reference
err = registryLibrary.PullStackFromRegistry(registryURL, id, stackDir, registryLibrary.RegistryOptions{Telemetry: registryLibrary.TelemetryData{Client: util.TelemetryIndirectDevfileCall}})
if err != nil {
return fmt.Errorf("failed to pull stack from registry %s", registryURL)
}
Expand Down Expand Up @@ -828,7 +831,8 @@ func getKubernetesDefinitionFromUri(uri string, d devfileCtx.DevfileCtx) ([]byte
// absolute URL address
newUri = uri
}
data, err = util.DownloadFileInMemory(newUri)
params := util.HTTPRequestParams{URL: newUri}
data, err = util.DownloadInMemory(params)
if err != nil {
return nil, errors.Wrapf(err, "error getting kubernetes resources definition info from url '%s'", newUri)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/devfile/parser/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ func ReadKubernetesYaml(src YamlSrc, fs *afero.Afero) ([]interface{}, error) {
var err error

if src.URL != "" {
data, err = util.DownloadFileInMemory(src.URL)
params := util.HTTPRequestParams{URL: src.URL}
data, err = util.DownloadInMemory(params)
if err != nil {
return nil, errors.Wrapf(err, "failed to download file %q", src.URL)
}
Expand Down
50 changes: 43 additions & 7 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"bytes"
"crypto/rand"
"fmt"
"github.com/go-git/go-git/v5/plumbing"
"io"
"io/ioutil"
"math/big"
Expand All @@ -42,6 +41,8 @@ import (
"syscall"
"time"

"github.com/go-git/go-git/v5/plumbing"

"github.com/devfile/library/v2/pkg/testingutil/filesystem"
"github.com/fatih/color"
gitpkg "github.com/go-git/go-git/v5"
Expand All @@ -57,9 +58,11 @@ import (
)

const (
HTTPRequestResponseTimeout = 30 * time.Second // HTTPRequestTimeout configures timeout of all HTTP requests
ModeReadWriteFile = 0600 // default Permission for a file
CredentialPrefix = "odo-" // CredentialPrefix is the prefix of the credential that uses to access secure registry
HTTPRequestResponseTimeout = 30 * time.Second // HTTPRequestTimeout configures timeout of all HTTP requests
ModeReadWriteFile = 0600 // default Permission for a file
CredentialPrefix = "odo-" // CredentialPrefix is the prefix of the credential that uses to access secure registry
TelemetryClientName = "devfile-library" //TelemetryClientName is the name of the devfile library client
TelemetryIndirectDevfileCall = "devfile-library-indirect" //TelemetryIndirectDevfileCall is used to identify calls made to retrieve the parent or plugin devfile
)

// httpCacheDir determines directory where odo will cache HTTP respones
Expand All @@ -86,9 +89,10 @@ type ResourceRequirementInfo struct {

// HTTPRequestParams holds parameters of forming http request
type HTTPRequestParams struct {
URL string
Token string
Timeout *int
URL string
Token string
Timeout *int
TelemetryClientName string //optional client name for telemetry
}

// DownloadParams holds parameters of forming file download request
Expand Down Expand Up @@ -744,6 +748,9 @@ func HTTPGetRequest(request HTTPRequestParams, cacheFor int) ([]byte, error) {
req.Header.Add("Authorization", bearer)
}

//add the telemetry client name
req.Header.Add("Client", request.TelemetryClientName)

overriddenTimeout := HTTPRequestResponseTimeout
timeout := request.Timeout
if timeout != nil {
Expand Down Expand Up @@ -1056,6 +1063,7 @@ func DownloadFile(params DownloadParams) error {
}

// DownloadFileInMemory uses the url to download the file and return bytes
// Deprecated, use DownloadInMemory() instead
func DownloadFileInMemory(url string) ([]byte, error) {
var httpClient = &http.Client{Transport: &http.Transport{
ResponseHeaderTimeout: HTTPRequestResponseTimeout,
Expand All @@ -1073,6 +1081,34 @@ func DownloadFileInMemory(url string) ([]byte, error) {
return ioutil.ReadAll(resp.Body)
}

// DownloadInMemory uses HTTPRequestParams to download the file and return bytes
func DownloadInMemory(params HTTPRequestParams) ([]byte, error) {

var httpClient = &http.Client{Transport: &http.Transport{
ResponseHeaderTimeout: HTTPRequestResponseTimeout,
}, Timeout: HTTPRequestResponseTimeout}

url := params.URL
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}

//add the telemetry client name in the header
req.Header.Add("Client", params.TelemetryClientName)
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
// We have a non 1xx / 2xx status, return an error
if (resp.StatusCode - 300) > 0 {
return nil, errors.Errorf("failed to retrieve %s, %v: %s", url, resp.StatusCode, http.StatusText(resp.StatusCode))
}
defer resp.Body.Close()

return ioutil.ReadAll(resp.Body)
}

// ValidateK8sResourceName sanitizes kubernetes resource name with the following requirements:
// - Contain at most 63 characters
// - Contain only lowercase alphanumeric characters or ‘-’
Expand Down

0 comments on commit 8e009f5

Please sign in to comment.