Skip to content

Commit

Permalink
Merge pull request #94 from kim-tsao/filter-viewer-events
Browse files Browse the repository at this point in the history
ignore registry viewer API events
  • Loading branch information
kim-tsao authored Dec 14, 2021
2 parents d7edf14 + 543d1d9 commit 2d64d8e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
9 changes: 5 additions & 4 deletions index/server/pkg/server/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ func serveDevfile(c *gin.Context) {
return
}

// Track event for telemetry
if enableTelemetry {
// Track event for telemetry. Ignore events from the registry-viewer since those are tracked on the client side
if enableTelemetry && !util.IsRegistryViewerEvent(c) {

user := util.GetUser(c)
client := util.GetClient(c)

Expand Down Expand Up @@ -235,8 +236,8 @@ func buildIndexAPIResponse(c *gin.Context) {
c.File(responseIndexPath)
}

// Track event for telemetry
if enableTelemetry {
// Track event for telemetry. Ignore events from the registry-viewer since those are tracked on the client side
if enableTelemetry && !util.IsRegistryViewerEvent(c) {
user := util.GetUser(c)
client := util.GetClient(c)
err := util.TrackEvent(analytics.Track{
Expand Down
3 changes: 2 additions & 1 deletion index/server/pkg/server/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ func ociServerProxy(c *gin.Context) {
resource = parts[3]
}

if resource == "blobs" {
//Ignore events from the registry-viewer since those are tracked on the client side
if resource == "blobs" && !util.IsRegistryViewerEvent(c) {
user := util.GetUser(c)
client := util.GetClient(c)

Expand Down
11 changes: 11 additions & 0 deletions index/server/pkg/util/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
const (
telemetryKey = "6HBMiy5UxBtsbxXx7O4n0t0u4dt8IAR3"
defaultUser = "devfile-registry"
viewerId = "registry-viewer"
)

//TrackEvent tracks event for telemetry
Expand Down Expand Up @@ -93,3 +94,13 @@ func getRegion(c *gin.Context) string {
}

}

//IsRegistryViewerEvent determines if the event is coming from the registry viewer client
func IsRegistryViewerEvent(c *gin.Context) bool {
client := GetClient(c)
if client == viewerId {
return true
}

return false
}
62 changes: 62 additions & 0 deletions index/server/pkg/util/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,65 @@ func TestSetContext(t *testing.T) {
})
}
}

func TestIsRegistryViewerEvent(t *testing.T) {
tests := []struct {
name string
context *gin.Context
want bool
}{
{
name: "Test registry-viewer event",
context: &gin.Context{
Request: &http.Request{
Header: http.Header{
"Client": {"registry-viewer"},
},
},
},
want: true,
},
{
name: "Test non registry-viewer event",
context: &gin.Context{
Request: &http.Request{
Header: http.Header{
"Client": {"odo"},
},
},
},
want: false,
},
{
name: "Test unset client header",
context: &gin.Context{
Request: &http.Request{
Header: http.Header{
"User": {"registry-viewer"},
},
},
},
want: false,
},
{
name: "Test case-sensitivity",
context: &gin.Context{
Request: &http.Request{
Header: http.Header{
"Client": {"REGISTRY-viewer"},
},
},
},
want: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := IsRegistryViewerEvent(test.context)
if got != test.want {
t.Errorf("Got: %v, Expected: %v", got, test.want)
}
})
}
}

0 comments on commit 2d64d8e

Please sign in to comment.