Skip to content

Commit ba6eb69

Browse files
committed
fix: add project-ids and monitor-ids to analytics
1 parent d009e2f commit ba6eb69

File tree

6 files changed

+406
-0
lines changed

6 files changed

+406
-0
lines changed

pkg/analytics/instrumentation_collector.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"github.com/snyk/go-application-framework/pkg/logging"
88
"os/user"
9+
"strings"
910
"time"
1011

1112
"github.com/rs/zerolog"
@@ -38,6 +39,8 @@ type InstrumentationCollector interface {
3839
SetStatus(s Status)
3940
SetTestSummary(s json_schemas.TestSummary)
4041
SetTargetId(t string) // maybe use package-url library and types
42+
SetProjectIds(projectIds []string)
43+
SetMonitorIds(monitorIds []string)
4144
AddError(err error)
4245
AddExtension(key string, value interface{})
4346
}
@@ -62,6 +65,8 @@ type instrumentationCollectorImpl struct {
6265
status Status
6366
testSummary json_schemas.TestSummary
6467
targetId string
68+
projectIds []string
69+
monitorIds []string
6570
instrumentationErr []error
6671
extension map[string]interface{}
6772
}
@@ -129,6 +134,14 @@ func (ic *instrumentationCollectorImpl) SetTargetId(t string) {
129134
ic.targetId = t
130135
}
131136

137+
func (ic *instrumentationCollectorImpl) SetProjectIds(ids []string) {
138+
ic.projectIds = ids
139+
}
140+
141+
func (ic *instrumentationCollectorImpl) SetMonitorIds(ids []string) {
142+
ic.monitorIds = ids
143+
}
144+
132145
func (ic *instrumentationCollectorImpl) AddError(err error) {
133146
ic.instrumentationErr = append(ic.instrumentationErr, err)
134147
}
@@ -219,6 +232,21 @@ func (ic *instrumentationCollectorImpl) getV2Attributes() api.AnalyticsAttribute
219232

220233
func (ic *instrumentationCollectorImpl) getV2Interaction() api.Interaction {
221234
stage := toInteractionStage(ic.stage)
235+
236+
if len(ic.projectIds) > 0 {
237+
if ic.extension == nil {
238+
ic.extension = make(map[string]interface{})
239+
}
240+
ic.extension["project_ids"] = strings.Join(ic.projectIds, ",")
241+
}
242+
243+
if len(ic.monitorIds) > 0 {
244+
if ic.extension == nil {
245+
ic.extension = make(map[string]interface{})
246+
}
247+
ic.extension["monitor_ids"] = strings.Join(ic.monitorIds, ",")
248+
}
249+
222250
return api.Interaction{
223251
Categories: &ic.category,
224252
Errors: toInteractionErrors(ic.instrumentationErr),

pkg/analytics/instrumentation_collector_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,36 @@ func Test_InstrumentationCollector(t *testing.T) {
256256
actualCategory := ic.GetCategory()
257257
assert.Equal(t, mockCategory, actualCategory)
258258
})
259+
260+
t.Run("it should add project id to extension", func(t *testing.T) {
261+
ic := setupBaseCollector(t)
262+
expectedV2InstrumentationObject := buildExpectedBaseObject(t)
263+
264+
mockProjectId := "123e4567-e89b-12d3-a456-426614174000"
265+
mockMonitorId := "89674498-4fcc-4af3-9a80-0582a74614d5"
266+
mockProjectIds := []string{mockProjectId}
267+
mockMonitorIds := []string{mockMonitorId}
268+
ic.SetProjectIds(mockProjectIds)
269+
ic.SetMonitorIds(mockMonitorIds)
270+
271+
mockExtension := map[string]interface{}{
272+
"strings": "hello world",
273+
"project_ids": mockProjectId,
274+
"monitor_ids": mockMonitorId,
275+
}
276+
277+
expectedV2InstrumentationObject.Data.Attributes.Interaction.Extension = &mockExtension
278+
279+
actualV2InstrumentationObject, err := GetV2InstrumentationObject(ic)
280+
assert.NoError(t, err)
281+
282+
expectedV2InstrumentationJson, err := json.Marshal(expectedV2InstrumentationObject)
283+
assert.NoError(t, err)
284+
actualV2InstrumentationJson, err := json.Marshal(actualV2InstrumentationObject)
285+
assert.NoError(t, err)
286+
287+
assert.JSONEq(t, string(expectedV2InstrumentationJson), string(actualV2InstrumentationJson))
288+
})
259289
}
260290

261291
func setupBaseCollector(t *testing.T) InstrumentationCollector {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package instrumentation
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
func GetProjectIdAndMonitorIdFromText(text string) ([][2]string, error) {
8+
// Pattern: /org/{org-slug}/project/{project-uuid}/history/{history-uuid}
9+
re := regexp.MustCompile(`/org/[^/]+/project/([0-9a-fA-F-]{36})/history/([0-9a-fA-F-]{36})`)
10+
matches := re.FindAllStringSubmatch(text, -1)
11+
12+
if len(matches) == 0 {
13+
return nil, nil
14+
}
15+
16+
result := make([][2]string, 0, len(matches))
17+
for _, m := range matches {
18+
if len(m) < 3 {
19+
continue
20+
}
21+
result = append(result, [2]string{m[1], m[2]})
22+
}
23+
24+
return result, nil
25+
}

0 commit comments

Comments
 (0)