Skip to content

Commit 8e009f5

Browse files
authored
telemetry updates (#156)
Signed-off-by: Kim Tsao <[email protected]> Signed-off-by: Kim Tsao <[email protected]>
1 parent c8f6c26 commit 8e009f5

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed

pkg/devfile/parser/context/content.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ func (d *DevfileCtx) SetDevfileContent() error {
6767
var err error
6868
var data []byte
6969
if d.url != "" {
70-
data, err = util.DownloadFileInMemory(d.url)
70+
// set the client identifier for telemetry
71+
params := util.HTTPRequestParams{URL: d.url, TelemetryClientName: util.TelemetryClientName}
72+
data, err = util.DownloadInMemory(params)
7173
if err != nil {
7274
return errors.Wrap(err, "error getting devfile info from url")
7375
}

pkg/devfile/parser/parse.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ import (
1919
"context"
2020
"encoding/json"
2121
"fmt"
22-
"github.com/devfile/api/v2/pkg/attributes"
23-
registryLibrary "github.com/devfile/registry-support/registry-library/library"
2422
"io/ioutil"
2523
"net/url"
2624
"os"
2725
"path"
2826
"strings"
2927

28+
"github.com/devfile/api/v2/pkg/attributes"
29+
registryLibrary "github.com/devfile/registry-support/registry-library/library"
30+
31+
"reflect"
32+
3033
devfileCtx "github.com/devfile/library/v2/pkg/devfile/parser/context"
3134
"github.com/devfile/library/v2/pkg/devfile/parser/data"
3235
"github.com/devfile/library/v2/pkg/devfile/parser/data/v2/common"
3336
"github.com/devfile/library/v2/pkg/util"
3437
"k8s.io/apimachinery/pkg/types"
3538
"k8s.io/client-go/tools/clientcmd"
3639
"k8s.io/klog"
37-
"reflect"
3840
"sigs.k8s.io/controller-runtime/pkg/client"
3941

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

522524
param.Timeout = httpTimeout
523-
525+
//suppress telemetry for parent uri references
526+
param.TelemetryClientName = util.TelemetryIndirectDevfileCall
524527
return util.HTTPGetRequest(param, 0)
525528
}
526529

@@ -530,8 +533,8 @@ func getResourcesFromRegistry(id, registryURL, destDir string) error {
530533
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
531534
}
532535
defer os.RemoveAll(stackDir)
533-
534-
err = registryLibrary.PullStackFromRegistry(registryURL, id, stackDir, registryLibrary.RegistryOptions{})
536+
//suppress telemetry for downloading resources from parent reference
537+
err = registryLibrary.PullStackFromRegistry(registryURL, id, stackDir, registryLibrary.RegistryOptions{Telemetry: registryLibrary.TelemetryData{Client: util.TelemetryIndirectDevfileCall}})
535538
if err != nil {
536539
return fmt.Errorf("failed to pull stack from registry %s", registryURL)
537540
}
@@ -828,7 +831,8 @@ func getKubernetesDefinitionFromUri(uri string, d devfileCtx.DevfileCtx) ([]byte
828831
// absolute URL address
829832
newUri = uri
830833
}
831-
data, err = util.DownloadFileInMemory(newUri)
834+
params := util.HTTPRequestParams{URL: newUri}
835+
data, err = util.DownloadInMemory(params)
832836
if err != nil {
833837
return nil, errors.Wrapf(err, "error getting kubernetes resources definition info from url '%s'", newUri)
834838
}

pkg/devfile/parser/reader.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ func ReadKubernetesYaml(src YamlSrc, fs *afero.Afero) ([]interface{}, error) {
6363
var err error
6464

6565
if src.URL != "" {
66-
data, err = util.DownloadFileInMemory(src.URL)
66+
params := util.HTTPRequestParams{URL: src.URL}
67+
data, err = util.DownloadInMemory(params)
6768
if err != nil {
6869
return nil, errors.Wrapf(err, "failed to download file %q", src.URL)
6970
}

pkg/util/util.go

+43-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"bytes"
2222
"crypto/rand"
2323
"fmt"
24-
"github.com/go-git/go-git/v5/plumbing"
2524
"io"
2625
"io/ioutil"
2726
"math/big"
@@ -42,6 +41,8 @@ import (
4241
"syscall"
4342
"time"
4443

44+
"github.com/go-git/go-git/v5/plumbing"
45+
4546
"github.com/devfile/library/v2/pkg/testingutil/filesystem"
4647
"github.com/fatih/color"
4748
gitpkg "github.com/go-git/go-git/v5"
@@ -57,9 +58,11 @@ import (
5758
)
5859

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

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

8790
// HTTPRequestParams holds parameters of forming http request
8891
type HTTPRequestParams struct {
89-
URL string
90-
Token string
91-
Timeout *int
92+
URL string
93+
Token string
94+
Timeout *int
95+
TelemetryClientName string //optional client name for telemetry
9296
}
9397

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

751+
//add the telemetry client name
752+
req.Header.Add("Client", request.TelemetryClientName)
753+
747754
overriddenTimeout := HTTPRequestResponseTimeout
748755
timeout := request.Timeout
749756
if timeout != nil {
@@ -1056,6 +1063,7 @@ func DownloadFile(params DownloadParams) error {
10561063
}
10571064

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

1084+
// DownloadInMemory uses HTTPRequestParams to download the file and return bytes
1085+
func DownloadInMemory(params HTTPRequestParams) ([]byte, error) {
1086+
1087+
var httpClient = &http.Client{Transport: &http.Transport{
1088+
ResponseHeaderTimeout: HTTPRequestResponseTimeout,
1089+
}, Timeout: HTTPRequestResponseTimeout}
1090+
1091+
url := params.URL
1092+
req, err := http.NewRequest("GET", url, nil)
1093+
if err != nil {
1094+
return nil, err
1095+
}
1096+
1097+
//add the telemetry client name in the header
1098+
req.Header.Add("Client", params.TelemetryClientName)
1099+
resp, err := httpClient.Do(req)
1100+
if err != nil {
1101+
return nil, err
1102+
}
1103+
// We have a non 1xx / 2xx status, return an error
1104+
if (resp.StatusCode - 300) > 0 {
1105+
return nil, errors.Errorf("failed to retrieve %s, %v: %s", url, resp.StatusCode, http.StatusText(resp.StatusCode))
1106+
}
1107+
defer resp.Body.Close()
1108+
1109+
return ioutil.ReadAll(resp.Body)
1110+
}
1111+
10761112
// ValidateK8sResourceName sanitizes kubernetes resource name with the following requirements:
10771113
// - Contain at most 63 characters
10781114
// - Contain only lowercase alphanumeric characters or ‘-’

0 commit comments

Comments
 (0)