github.com/kubeshop/testkube@v1.17.23/pkg/telemetry/payload.go (about) 1 package telemetry 2 3 import ( 4 "runtime" 5 "strings" 6 7 "github.com/kubeshop/testkube/pkg/utils" 8 "github.com/kubeshop/testkube/pkg/utils/text" 9 ) 10 11 const runContextAgent = "agent" 12 13 type Params struct { 14 EventCount int64 `json:"event_count,omitempty"` 15 EventCategory string `json:"event_category,omitempty"` 16 AppVersion string `json:"app_version,omitempty"` 17 AppName string `json:"app_name,omitempty"` 18 CustomDimensions string `json:"custom_dimensions,omitempty"` 19 DataSource string `json:"data_source,omitempty"` 20 Host string `json:"host,omitempty"` 21 MachineID string `json:"machine_id,omitempty"` 22 ClusterID string `json:"cluster_id,omitempty"` 23 OperatingSystem string `json:"operating_system,omitempty"` 24 Architecture string `json:"architecture,omitempty"` 25 TestType string `json:"test_type,omitempty"` 26 DurationMs int32 `json:"duration_ms,omitempty"` 27 Status string `json:"status,omitempty"` 28 TestSource string `json:"test_source,omitempty"` 29 TestSuiteSteps int32 `json:"test_suite_steps,omitempty"` 30 Context RunContext `json:"context,omitempty"` 31 ClusterType string `json:"cluster_type,omitempty"` 32 CliContext string `json:"cli_context,omitempty"` 33 Error string `json:"error,omitempty"` 34 ErrorType string `json:"error_type,omitempty"` 35 ErrorStackTrace string `json:"error_stacktrace,omitempty"` 36 TestWorkflowSteps int32 `json:"test_workflow_steps,omitempty"` 37 TestWorkflowTemplateUsed bool `json:"test_workflow_template_used,omitempty"` 38 TestWorkflowImage string `json:"test_workflow_image,omitempty"` 39 TestWorkflowArtifactUsed bool `json:"test_workflow_artifact_used,omitempty"` 40 TestWorkflowKubeshopGitURI bool `json:"test_workflow_kubeshop_git_uri,omitempty"` 41 License string `json:"license,omitempty"` 42 } 43 44 type Event struct { 45 Name string `json:"name"` 46 Params Params `json:"params,omitempty"` 47 } 48 49 type Payload struct { 50 UserID string `json:"user_id,omitempty"` 51 ClientID string `json:"client_id,omitempty"` 52 Events []Event `json:"events,omitempty"` 53 } 54 55 // CreateParams contains Test or Test suite creation parameters 56 type CreateParams struct { 57 AppVersion string 58 DataSource string 59 Host string 60 ClusterID string 61 TestType string 62 TestSource string 63 TestSuiteSteps int32 64 } 65 66 // RunParams contains Test or Test suite run parameters 67 type RunParams struct { 68 AppVersion string 69 DataSource string 70 Host string 71 ClusterID string 72 TestType string 73 DurationMs int32 74 Status string 75 } 76 77 type RunContext struct { 78 Type string 79 OrganizationId string 80 EnvironmentId string 81 } 82 83 type WorkflowParams struct { 84 TestWorkflowSteps int32 85 TestWorkflowTemplateUsed bool 86 TestWorkflowImage string 87 TestWorkflowArtifactUsed bool 88 TestWorkflowKubeshopGitURI bool 89 } 90 91 type CreateWorkflowParams struct { 92 CreateParams 93 WorkflowParams 94 } 95 96 type RunWorkflowParams struct { 97 RunParams 98 WorkflowParams 99 } 100 101 func NewCLIPayload(context RunContext, id, name, version, category, clusterType string) Payload { 102 return Payload{ 103 ClientID: id, 104 UserID: id, 105 Events: []Event{ 106 { 107 Name: text.GAEventName(name), 108 Params: Params{ 109 EventCount: 1, 110 EventCategory: category, 111 AppVersion: version, 112 AppName: "kubectl-testkube", 113 MachineID: GetMachineID(), 114 OperatingSystem: runtime.GOOS, 115 Architecture: runtime.GOARCH, 116 Context: context, 117 ClusterType: clusterType, 118 CliContext: GetCliRunContext(), 119 }, 120 }}, 121 } 122 } 123 124 func NewCLIWithLicensePayload(context RunContext, id, name, version, category, clusterType, license string) Payload { 125 return Payload{ 126 ClientID: id, 127 UserID: id, 128 Events: []Event{ 129 { 130 Name: text.GAEventName(name), 131 Params: Params{ 132 EventCount: 1, 133 EventCategory: category, 134 AppVersion: version, 135 AppName: "kubectl-testkube", 136 MachineID: GetMachineID(), 137 OperatingSystem: runtime.GOOS, 138 Architecture: runtime.GOARCH, 139 Context: context, 140 ClusterType: clusterType, 141 CliContext: GetCliRunContext(), 142 License: license, 143 }, 144 }}, 145 } 146 } 147 148 func NewAPIPayload(clusterId, name, version, host, clusterType string) Payload { 149 return Payload{ 150 ClientID: clusterId, 151 UserID: clusterId, 152 Events: []Event{ 153 { 154 Name: text.GAEventName(name), 155 Params: Params{ 156 EventCount: 1, 157 EventCategory: "api", 158 AppVersion: version, 159 AppName: "testkube-api-server", 160 Host: AnonymizeHost(host), 161 OperatingSystem: runtime.GOOS, 162 Architecture: runtime.GOARCH, 163 MachineID: GetMachineID(), 164 ClusterID: clusterId, 165 ClusterType: clusterType, 166 Context: getAgentContext(), 167 }, 168 }}, 169 } 170 } 171 172 // NewCreatePayload prepares payload for Test or Test suite creation 173 func NewCreatePayload(name, clusterType string, params CreateParams) Payload { 174 return Payload{ 175 ClientID: params.ClusterID, 176 UserID: params.ClusterID, 177 Events: []Event{ 178 { 179 Name: text.GAEventName(name), 180 Params: Params{ 181 EventCount: 1, 182 EventCategory: "api", 183 AppVersion: params.AppVersion, 184 AppName: "testkube-api-server", 185 Host: AnonymizeHost(params.Host), 186 OperatingSystem: runtime.GOOS, 187 Architecture: runtime.GOARCH, 188 MachineID: GetMachineID(), 189 ClusterID: params.ClusterID, 190 DataSource: params.DataSource, 191 TestType: params.TestType, 192 TestSource: params.TestSource, 193 TestSuiteSteps: params.TestSuiteSteps, 194 ClusterType: clusterType, 195 Context: getAgentContext(), 196 }, 197 }}, 198 } 199 } 200 201 // NewRunPayload prepares payload for Test or Test suite execution 202 func NewRunPayload(name, clusterType string, params RunParams) Payload { 203 return Payload{ 204 ClientID: params.ClusterID, 205 UserID: params.ClusterID, 206 Events: []Event{ 207 { 208 Name: text.GAEventName(name), 209 Params: Params{ 210 EventCount: 1, 211 EventCategory: "api", 212 AppVersion: params.AppVersion, 213 AppName: "testkube-api-server", 214 Host: AnonymizeHost(params.Host), 215 OperatingSystem: runtime.GOOS, 216 Architecture: runtime.GOARCH, 217 MachineID: GetMachineID(), 218 ClusterID: params.ClusterID, 219 DataSource: params.DataSource, 220 TestType: params.TestType, 221 DurationMs: params.DurationMs, 222 Status: params.Status, 223 ClusterType: clusterType, 224 Context: getAgentContext(), 225 }, 226 }}, 227 } 228 } 229 230 // NewCreateWorkflowPayload prepares payload for Test workflow creation 231 func NewCreateWorkflowPayload(name, clusterType string, params CreateWorkflowParams) Payload { 232 return Payload{ 233 ClientID: params.ClusterID, 234 UserID: params.ClusterID, 235 Events: []Event{ 236 { 237 Name: text.GAEventName(name), 238 Params: Params{ 239 EventCount: 1, 240 EventCategory: "api", 241 AppVersion: params.AppVersion, 242 AppName: "testkube-api-server", 243 Host: AnonymizeHost(params.Host), 244 OperatingSystem: runtime.GOOS, 245 Architecture: runtime.GOARCH, 246 MachineID: GetMachineID(), 247 ClusterID: params.ClusterID, 248 DataSource: params.DataSource, 249 TestType: params.TestType, 250 TestSource: params.TestSource, 251 TestSuiteSteps: params.TestSuiteSteps, 252 ClusterType: clusterType, 253 Context: getAgentContext(), 254 TestWorkflowSteps: params.TestWorkflowSteps, 255 TestWorkflowTemplateUsed: params.TestWorkflowTemplateUsed, 256 TestWorkflowImage: params.TestWorkflowImage, 257 TestWorkflowArtifactUsed: params.TestWorkflowArtifactUsed, 258 TestWorkflowKubeshopGitURI: params.TestWorkflowKubeshopGitURI, 259 }, 260 }}, 261 } 262 } 263 264 // NewRunWorkflowPayload prepares payload for Test workflow execution 265 func NewRunWorkflowPayload(name, clusterType string, params RunWorkflowParams) Payload { 266 return Payload{ 267 ClientID: params.ClusterID, 268 UserID: params.ClusterID, 269 Events: []Event{ 270 { 271 Name: text.GAEventName(name), 272 Params: Params{ 273 EventCount: 1, 274 EventCategory: "api", 275 AppVersion: params.AppVersion, 276 AppName: "testkube-api-server", 277 Host: AnonymizeHost(params.Host), 278 OperatingSystem: runtime.GOOS, 279 Architecture: runtime.GOARCH, 280 MachineID: GetMachineID(), 281 ClusterID: params.ClusterID, 282 DataSource: params.DataSource, 283 TestType: params.TestType, 284 DurationMs: params.DurationMs, 285 Status: params.Status, 286 ClusterType: clusterType, 287 Context: getAgentContext(), 288 TestWorkflowSteps: params.TestWorkflowSteps, 289 TestWorkflowTemplateUsed: params.TestWorkflowTemplateUsed, 290 TestWorkflowImage: params.TestWorkflowImage, 291 TestWorkflowArtifactUsed: params.TestWorkflowArtifactUsed, 292 TestWorkflowKubeshopGitURI: params.TestWorkflowKubeshopGitURI, 293 }, 294 }}, 295 } 296 } 297 298 const ( 299 APIHostLocal = "local" 300 APIHostExternal = "external" 301 APIHostTestkubeInternal = "testkube-internal" 302 ) 303 304 func AnonymizeHost(host string) string { 305 if strings.Contains(host, "testkube.io") { 306 return APIHostTestkubeInternal 307 } else if strings.Contains(host, "localhost:") { 308 return APIHostLocal 309 } 310 311 return APIHostExternal 312 } 313 314 func getAgentContext() RunContext { 315 orgID := utils.GetEnvVarWithDeprecation("TESTKUBE_PRO_ORG_ID", "TESTKUBE_CLOUD_ORG_ID", "") 316 envID := utils.GetEnvVarWithDeprecation("TESTKUBE_PRO_ENV_ID", "TESTKUBE_CLOUD_ENV_ID", "") 317 318 if orgID == "" || envID == "" { 319 return RunContext{} 320 } 321 return RunContext{ 322 Type: runContextAgent, 323 EnvironmentId: envID, 324 OrganizationId: orgID, 325 } 326 }