Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass cloud user id and cloud instance id while creating metrics client #1703

Merged
merged 6 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ func WithExperimentalFeatureFlags(experimentalFeatureFlags []kurtosis_core_rpc_a
}
}

// Deprecated: This isn't used anymore and the value passed will not be read anywhere
func WithCloudInstanceId(cloudInstanceId string) starlarkRunConfigOption {
h4ck3rk3y marked this conversation as resolved.
Show resolved Hide resolved
return func(config *StarlarkRunConfig) {
config.CloudInstanceId = cloudInstanceId
}
}

// Deprecated: This isn't used anymore and the value passed will not be read anywhere
func WithCloudUserId(cloudUserId string) starlarkRunConfigOption {
return func(config *StarlarkRunConfig) {
config.CloudUserId = cloudUserId
Expand Down
2 changes: 2 additions & 0 deletions api/protobuf/core/api_container_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,11 @@ message RunStarlarkPackageArgs {
repeated KurtosisFeatureFlag experimental_features = 11;

// Defaults to empty
// Deprecated: This value isn't used in the APIC anymore
optional string cloud_instance_id = 12;

// Defaults to empty
// Deprecated: This value isn't used in the APIC anymore
optional string cloud_user_id = 13;

// Defaults to empty
Expand Down
26 changes: 7 additions & 19 deletions cli/cli/commands/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,30 +322,13 @@ func run(
logrus.Debugf("'%v' arg is not empty; ignoring value of '%v' flag as '%v' arg takes precedence", inputArgsArgKey, packageArgsFileFlagKey, inputArgsArgKey)
}

cloudUserId := ""
cloudInstanceId := ""
currentContext, err := store.GetContextsConfigStore().GetCurrentContext()
if err != nil {
logrus.Warnf("Could not retrieve the current context. Kurtosis will assume context is local (no cloud user & instance id) and not" +
"map the enclave service ports. If you're running on a remote context and are seeing this error, then" +
"the enclave services will be unreachable locally. Turn on debug logging to see the actual error.")
logrus.Debugf("Error was: %v", err.Error())
} else {
if store.IsRemote(currentContext) {
cloudUserId = currentContext.GetRemoteContextV0().GetCloudUserId()
cloudInstanceId = currentContext.GetRemoteContextV0().GetCloudInstanceId()
}
}

starlarkRunConfig := starlark_run_config.NewRunStarlarkConfig(
starlark_run_config.WithDryRun(dryRun),
starlark_run_config.WithParallelism(castedParallelism),
starlark_run_config.WithExperimentalFeatureFlags(experimentalFlags),
starlark_run_config.WithMainFunctionName(mainFunctionName),
starlark_run_config.WithRelativePathToMainFile(relativePathToTheMainFile),
starlark_run_config.WithSerializedParams(packageArgs),
starlark_run_config.WithCloudUserId(cloudUserId),
starlark_run_config.WithCloudInstanceId(cloudInstanceId),
starlark_run_config.WithImageDownloadMode(*imageDownload),
)

Expand Down Expand Up @@ -428,7 +411,7 @@ func run(
logrus.Warn("Tried getting number of services in the enclave to log metrics but failed")
} else {
// TODO(gyani-cloud-metrics) move this to APIC
if err = metricsClient.TrackKurtosisRunFinishedEvent(starlarkScriptOrPackagePath, len(servicesInEnclavePostRun), runStatusForMetrics, cloudInstanceId, cloudUserId); err != nil {
if err = metricsClient.TrackKurtosisRunFinishedEvent(starlarkScriptOrPackagePath, len(servicesInEnclavePostRun), runStatusForMetrics); err != nil {
logrus.Warn("An error occurred tracking kurtosis run finished event")
}
}
Expand All @@ -443,7 +426,12 @@ func run(
return nil
}

if currentContext == nil {
currentContext, err := store.GetContextsConfigStore().GetCurrentContext()
if err != nil {
logrus.Warnf("Could not retrieve the current context. Kurtosis will assume context is local (no cloud user & instance id) and not" +
"map the enclave service ports. If you're running on a remote context and are seeing this error, then" +
"the enclave services will be unreachable locally. Turn on debug logging to see the actual error.")
logrus.Debugf("Error was: %v", err.Error())
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions cli/cli/helpers/engine_manager/engine_existence_guarantor.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/Masterminds/semver/v3"
"github.com/kurtosis-tech/kurtosis/api/golang/engine/lib/kurtosis_context"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_cloud_user_instance_id_helper"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_user_id_store"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_config/resolved_config"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface"
Expand Down Expand Up @@ -147,6 +148,8 @@ func (guarantor *engineExistenceGuarantor) VisitStopped() error {
return stacktrace.Propagate(err, "An error occurred getting metrics user id")
}

maybeCloudUserId, maybeCloudInstanceId := metrics_cloud_user_instance_id_helper.GetMaybeCloudUserAndInstanceID()

var engineLaunchErr error
if guarantor.imageVersionTag == defaultEngineImageVersionTag {
_, _, engineLaunchErr = guarantor.engineServerLauncher.LaunchWithDefaultVersion(
Expand All @@ -160,6 +163,8 @@ func (guarantor *engineExistenceGuarantor) VisitStopped() error {
guarantor.poolSize,
guarantor.enclaveEnvVars,
metrics_client.IsCI(),
maybeCloudUserId,
maybeCloudInstanceId,
)
} else {
_, _, engineLaunchErr = guarantor.engineServerLauncher.LaunchWithCustomVersion(
Expand All @@ -174,6 +179,8 @@ func (guarantor *engineExistenceGuarantor) VisitStopped() error {
guarantor.poolSize,
guarantor.enclaveEnvVars,
metrics_client.IsCI(),
maybeCloudUserId,
maybeCloudInstanceId,
)
}
if engineLaunchErr != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics_client_factory
import (
"github.com/kurtosis-tech/kurtosis/cli/cli/defaults"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/do_nothing_metrics_client_callback"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_cloud_user_instance_id_helper"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_user_id_store"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_cluster_setting"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_config"
Expand Down Expand Up @@ -31,6 +32,8 @@ func GetMetricsClient() (metrics_client.MetricsClient, func() error, error) {
return nil, nil, stacktrace.NewError("An error occurred while determining whether configuration already exists")
}

maybeCloudUserId, maybeCloudInstanceId := metrics_cloud_user_instance_id_helper.GetMaybeCloudUserAndInstanceID()

var sendUserMetrics bool
if hasConfig {
kurtosisConfig, err := kurtosisConfigStore.GetConfig()
Expand All @@ -53,7 +56,7 @@ func GetMetricsClient() (metrics_client.MetricsClient, func() error, error) {
shouldFlushMetricsClientQueueOnEachEvent,
do_nothing_metrics_client_callback.NewDoNothingMetricsClientCallback(),
analytics_logger.ConvertLogrusLoggerToAnalyticsLogger(logger),
metrics_client.IsCI()),
metrics_client.IsCI(), maybeCloudUserId, maybeCloudInstanceId),
)

if err != nil {
Expand All @@ -72,6 +75,8 @@ func GetSegmentClient() (metrics_client.MetricsClient, func() error, error) {
// this is force set to true in order to get the segment client
sendUserMetrics := true

maybeCloudUserId, maybeCloudInstanceId := metrics_cloud_user_instance_id_helper.GetMaybeCloudUserAndInstanceID()

logger := logrus.StandardLogger()
metricsClient, metricsClientCloseFunc, err := metrics_client.CreateMetricsClient(
metrics_client.NewMetricsClientCreatorOption(source.KurtosisCLISource,
Expand All @@ -82,7 +87,7 @@ func GetSegmentClient() (metrics_client.MetricsClient, func() error, error) {
shouldFlushMetricsClientQueueOnEachEvent,
do_nothing_metrics_client_callback.NewDoNothingMetricsClientCallback(),
analytics_logger.ConvertLogrusLoggerToAnalyticsLogger(logger),
metrics_client.IsCI()),
metrics_client.IsCI(), maybeCloudUserId, maybeCloudInstanceId),
)

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package metrics_cloud_user_instance_id_helper

import (
"github.com/kurtosis-tech/kurtosis/contexts-config-store/store"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"
"github.com/sirupsen/logrus"
)

func GetMaybeCloudUserAndInstanceID() (metrics_client.CloudUserID, metrics_client.CloudInstanceID) {
cloudUserId := ""
cloudInstanceId := ""
currentContext, err := store.GetContextsConfigStore().GetCurrentContext()
if err != nil {
logrus.Debugf("Could not retrieve the current context")
logrus.Debugf("Error was: %v", err.Error())
} else {
if store.IsRemote(currentContext) {
cloudUserId = currentContext.GetRemoteContextV0().GetCloudUserId()
cloudInstanceId = currentContext.GetRemoteContextV0().GetCloudInstanceId()
}
}
return metrics_client.CloudUserID(cloudUserId), metrics_client.CloudInstanceID(cloudInstanceId)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package user_send_metrics_election

import (
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_cloud_user_instance_id_helper"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_user_id_store"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/user_send_metrics_election/user_metrics_election_event_backlog"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_cluster_setting"
Expand Down Expand Up @@ -41,6 +42,8 @@ func SendAnyBackloggedUserMetricsElectionEvent() error {
}
}

maybeCloudUserID, maybeCloudInstanceID := metrics_cloud_user_instance_id_helper.GetMaybeCloudUserAndInstanceID()

if hasBackloggedEvent {
metricsUserIdStore := metrics_user_id_store.GetMetricsUserIDStore()

Expand All @@ -64,6 +67,8 @@ func SendAnyBackloggedUserMetricsElectionEvent() error {
metricsClientCallback,
analytics_logger.ConvertLogrusLoggerToAnalyticsLogger(logger),
metrics_client.IsCI(),
maybeCloudUserID,
maybeCloudInstanceID,
),
)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion cli/cli/kurtosis_config/email_collector/email_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package email_collector

import (
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/do_nothing_metrics_client_callback"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_cloud_user_instance_id_helper"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/metrics_user_id_store"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/prompt_displayer"
"github.com/kurtosis-tech/kurtosis/cli/cli/kurtosis_config/resolved_config"
Expand Down Expand Up @@ -41,6 +42,8 @@ func logUserEmailAddressAsMetric(userEmail string) {
}
logger := logrus.StandardLogger()

maybeCloudUserID, maybeCloudInstanceID := metrics_cloud_user_instance_id_helper.GetMaybeCloudUserAndInstanceID()

metricsClient, metricsClientCloseFunc, err := metrics_client.CreateMetricsClient(
metrics_client.NewMetricsClientCreatorOption(
source.KurtosisCLISource,
Expand All @@ -53,7 +56,9 @@ func logUserEmailAddressAsMetric(userEmail string) {
flushQueueOnEachEvent,
do_nothing_metrics_client_callback.NewDoNothingMetricsClientCallback(),
analytics_logger.ConvertLogrusLoggerToAnalyticsLogger(logger),
metrics_client.IsCI()),
metrics_client.IsCI(),
maybeCloudUserID,
maybeCloudInstanceID),
)
if err != nil {
logrus.Debugf("tried creating a metrics client but failed with error:\n%v", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/kurtosis/core/launcher/args"
"github.com/kurtosis-tech/kurtosis/kurtosis_version"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -42,6 +43,8 @@ func (launcher ApiContainerLauncher) LaunchWithDefaultVersion(
metricsUserID string,
didUserAcceptSendingMetrics bool,
isCI bool,
cloudUserID metrics_client.CloudUserID,
cloudInstanceID metrics_client.CloudInstanceID,
) (
resultApiContainer *api_container.APIContainer,
resultErr error,
Expand All @@ -58,6 +61,8 @@ func (launcher ApiContainerLauncher) LaunchWithDefaultVersion(
metricsUserID,
didUserAcceptSendingMetrics,
isCI,
cloudUserID,
cloudInstanceID,
)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred launching the API container with default version tag '%v'", kurtosis_version.KurtosisVersion)
Expand All @@ -77,6 +82,8 @@ func (launcher ApiContainerLauncher) LaunchWithCustomVersion(
metricsUserID string,
didUserAcceptSendingMetrics bool,
isCI bool,
cloudUserID metrics_client.CloudUserID,
cloudInstanceID metrics_client.CloudInstanceID,
) (
resultApiContainer *api_container.APIContainer,
resultErr error,
Expand All @@ -95,6 +102,8 @@ func (launcher ApiContainerLauncher) LaunchWithCustomVersion(
metricsUserID,
didUserAcceptSendingMetrics,
isCI,
cloudUserID,
cloudInstanceID,
)
if err != nil {
return nil, stacktrace.Propagate(err, "An error occurred creating the API container args")
Expand Down
20 changes: 20 additions & 0 deletions core/launcher/args/api_container_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package args
import (
"encoding/json"
"github.com/kurtosis-tech/kurtosis/core/launcher/args/kurtosis_backend_config"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"
"reflect"
"strings"

Expand Down Expand Up @@ -47,6 +48,17 @@ type APIContainerArgs struct {

//If its running in a CI environment
IsCI bool `json:"is_ci"`

// The Cloud User ID of the current user if available
CloudUserID metrics_client.CloudUserID `json:"cloud_user_id"`

// The Cloud Instance ID of the current user if available
CloudInstanceID metrics_client.CloudInstanceID `json:"cloud_instance_id"`
}

var skipValidation = map[string]bool{
"cloud_instance_id": true,
"cloud_user_id": true,
}

func (args *APIContainerArgs) UnmarshalJSON(data []byte) error {
Expand Down Expand Up @@ -96,6 +108,8 @@ func NewAPIContainerArgs(
metricsUserID string,
didUserAcceptSendingMetrics bool,
isCI bool,
cloudUserID metrics_client.CloudUserID,
cloudInstanceID metrics_client.CloudInstanceID,
) (*APIContainerArgs, error) {
result := &APIContainerArgs{
Version: version,
Expand All @@ -110,6 +124,8 @@ func NewAPIContainerArgs(
MetricsUserID: metricsUserID,
DidUserAcceptSendingMetrics: didUserAcceptSendingMetrics,
IsCI: isCI,
CloudUserID: cloudUserID,
CloudInstanceID: cloudInstanceID,
}

if err := result.validate(); err != nil {
Expand All @@ -127,6 +143,10 @@ func (args APIContainerArgs) validate() error {
field := reflectValType.Field(i)
jsonFieldName := field.Tag.Get(jsonFieldTag)

if _, found := skipValidation[jsonFieldName]; found {
continue
}

// Ensure no empty strings
strVal := reflectVal.Field(i).String()
if strings.TrimSpace(strVal) == "" {
Expand Down
2 changes: 2 additions & 0 deletions core/server/api_container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ func runMain() error {
doNothingMetricsClientCallback{},
analytics_logger.ConvertLogrusLoggerToAnalyticsLogger(logger),
serverArgs.IsCI,
serverArgs.CloudUserID,
serverArgs.CloudInstanceID,
),
)
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions core/server/api_container/server/api_container_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ const (
unlimitedLineCount = math.MaxInt
allFilePermissionsForOwner = 0700

defaultCloudUserId = ""
defaultCloudInstanceId = ""
defaultImageDownloadMode = kurtosis_core_rpc_api_bindings.ImageDownloadMode_missing
isScript = true
isNotScript = false
Expand Down Expand Up @@ -138,13 +136,11 @@ func (apicService *ApiContainerService) RunStarlarkScript(args *kurtosis_core_rp
dryRun := shared_utils.GetOrDefaultBool(args.DryRun, defaultStartosisDryRun)
mainFuncName := args.GetMainFunctionName()
experimentalFeatures := args.GetExperimentalFeatures()
cloudUserId := shared_utils.GetOrDefaultString(args.CloudUserId, defaultCloudUserId)
cloudInstanceID := shared_utils.GetOrDefaultString(args.CloudInstanceId, defaultCloudInstanceId)
ApiDownloadMode := shared_utils.GetOrDefault(args.ImageDownloadMode, defaultImageDownloadMode)

downloadMode := convertFromImageDownloadModeAPI(ApiDownloadMode)

metricsErr := apicService.metricsClient.TrackKurtosisRun(startosis_constants.PackageIdPlaceholderForStandaloneScript, isNotRemote, dryRun, isScript, cloudInstanceID, cloudUserId)
metricsErr := apicService.metricsClient.TrackKurtosisRun(startosis_constants.PackageIdPlaceholderForStandaloneScript, isNotRemote, dryRun, isScript)
if metricsErr != nil {
logrus.Warn("An error occurred tracking kurtosis run event")
}
Expand Down Expand Up @@ -240,8 +236,6 @@ func (apicService *ApiContainerService) RunStarlarkPackage(args *kurtosis_core_r
serializedParams := args.GetSerializedParams()
relativePathToMainFile := args.GetRelativePathToMainFile()
mainFuncName := args.GetMainFunctionName()
cloudUserId := shared_utils.GetOrDefaultString(args.CloudUserId, defaultCloudUserId)
cloudInstanceID := shared_utils.GetOrDefaultString(args.CloudInstanceId, defaultCloudInstanceId)
ApiDownloadMode := shared_utils.GetOrDefault(args.ImageDownloadMode, defaultImageDownloadMode)

downloadMode := convertFromImageDownloadModeAPI(ApiDownloadMode)
Expand Down Expand Up @@ -275,7 +269,7 @@ func (apicService *ApiContainerService) RunStarlarkPackage(args *kurtosis_core_r
packageReplaceOptions := kurtosisYml.GetPackageReplaceOptions()
logrus.Debugf("package replace options received '%+v'", packageReplaceOptions)

metricsErr := apicService.metricsClient.TrackKurtosisRun(packageName, isRemote, dryRun, isNotScript, cloudInstanceID, cloudUserId)
metricsErr := apicService.metricsClient.TrackKurtosisRun(packageName, isRemote, dryRun, isNotScript)
if metricsErr != nil {
logrus.Warn("An error occurred tracking kurtosis run event")
}
Expand Down
2 changes: 0 additions & 2 deletions docs/docs/api-reference/engine-apic-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,6 @@ This is a configuration object for Starlark Runs:
* `DryRun`: When set to true, the Kurtosis instructions are not executed. Configurable using `WithDryRun`; defaults to false.
* `Parallelism`: The level of parallelism for instructions that support parallelism. Configurable using `WithParallelism`; defaults to 4
* `ExperimentalFeatureFlags`: List of experimental features to turn on for this run. Leave empty to leave any experimental feature disabled. Configurable using `WithExperimentalFeatureFlags`; defaults to empty
* `CloudInstanceId`: The `CloudInstanceId` if running on Cloud Kurtosis. Configurable using `WithCloudInstanceID`; defaults to "".
* `CloudUserId`: The `CloudUserId` if running on Cloud Kurtosis. Configurable using `WithCloudUserId`; defaults to "".

ServiceContext
--------------
Expand Down