Skip to content

Commit

Permalink
feat: track enclave size after run has finished (#15)
Browse files Browse the repository at this point in the history
* updated metrics library

* this seems a lot cleaner

* thanks linter

* some fixes

* use named constants

* refactor

* moved around a comment
  • Loading branch information
h4ck3rk3y authored Feb 17, 2023
1 parent 84f1042 commit 80f35c8
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cli/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/kurtosis-tech/kurtosis/container-engine-lib v0.0.0 // local dependency
github.com/kurtosis-tech/kurtosis/engine/launcher v0.0.0 // local dependency
github.com/kurtosis-tech/kurtosis/kurtosis_version v0.0.0 // Local dependency generated during build
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409
github.com/manifoldco/promptui v0.9.0
github.com/mattn/go-isatty v0.0.14
Expand Down
4 changes: 2 additions & 2 deletions cli/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kurtosis-tech/free-ip-addr-tracker-lib v0.0.0-20211106222342-1f73d028840d h1:IWFwJfg2EhA/9Anv+VyG3LSTEZ/TZL30tUaVCFrzWK0=
github.com/kurtosis-tech/free-ip-addr-tracker-lib v0.0.0-20211106222342-1f73d028840d/go.mod h1:1PoW/0l4K80JRSj7A13aONsnaTdTM+dM36DIfDbYmVs=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230214135833-e362c2ee8600 h1:8GZVRAr16u7WCy/5xNsjZIgEcslh02cHCa3AzBSa7ME=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230214135833-e362c2ee8600/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e h1:VSoT53oeS53dGr28LtVVbwihdcRHQShsufbQjyHm/5M=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570 h1:2d8FxmA36Heb0uNziPof0Tqi70VhHr7H1cfoYY6IB9M=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409 h1:YQTATifMUwZEtZYb0LVA7DK2pj8s71iY8rzweuUQ5+g=
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409/go.mod h1:y5weVs5d9wXXHcDA1awRxkIhhHC1xxYJN8a7aXnE6S8=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
Expand Down
2 changes: 1 addition & 1 deletion core/server/api_container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func runMain() error {
startosisRunner := startosis_engine.NewStartosisRunner(
startosis_engine.NewStartosisInterpreter(serviceNetwork, gitPackageContentProvider, runtime_value_store.NewRuntimeValueStore()),
startosis_engine.NewStartosisValidator(&kurtosisBackend, serviceNetwork, filesArtifactStore),
startosis_engine.NewStartosisExecutor())
startosis_engine.NewStartosisExecutor(metricsClient, serviceNetwork))

//Creation of ApiContainerService
apiContainerService, err := server.NewApiContainerService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@ import (
"context"
"github.com/kurtosis-tech/kurtosis/api/golang/core/kurtosis_core_rpc_api_bindings"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/binding_constructors"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/service_network"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction"
metrics_client "github.com/kurtosis-tech/metrics-library/golang/lib/client"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"sync"
)

const (
progressMsg = "Execution in progress"
ParallelismParam = "PARALLELISM"
progressMsg = "Execution in progress"
ParallelismParam = "PARALLELISM"
executionFinishedSuccessfully = true
executionFailed = false
)

type StartosisExecutor struct {
mutex *sync.Mutex
mutex *sync.Mutex
metricsClient metrics_client.MetricsClient
serviceNetwork service_network.ServiceNetwork
}

type ExecutionError struct {
Error string
}

func NewStartosisExecutor() *StartosisExecutor {
func NewStartosisExecutor(metricsClient metrics_client.MetricsClient, serviceNetwork service_network.ServiceNetwork) *StartosisExecutor {
return &StartosisExecutor{
mutex: &sync.Mutex{},
mutex: &sync.Mutex{},
metricsClient: metricsClient,
serviceNetwork: serviceNetwork,
}
}

Expand All @@ -35,7 +44,7 @@ func NewStartosisExecutor() *StartosisExecutor {
// - A regular KurtosisInstruction that was successfully executed
// - A KurtosisExecutionError if the execution failed
// - A ProgressInfo to update the current "state" of the execution
func (executor *StartosisExecutor) Execute(ctx context.Context, dryRun bool, parallelism int, instructions []kurtosis_instruction.KurtosisInstruction, serializedScriptOutput string) <-chan *kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine {
func (executor *StartosisExecutor) Execute(ctx context.Context, dryRun bool, parallelism int, instructions []kurtosis_instruction.KurtosisInstruction, serializedScriptOutput string, packageId string) <-chan *kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine {
executor.mutex.Lock()
starlarkRunResponseLineStream := make(chan *kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine)
ctxWithParallelism := context.WithValue(ctx, ParallelismParam, parallelism)
Expand All @@ -60,6 +69,10 @@ func (executor *StartosisExecutor) Execute(ctx context.Context, dryRun bool, par
if err != nil {
propagatedError := stacktrace.Propagate(err, "An error occurred executing instruction (number %d): \n%v", instructionNumber, instruction.String())
serializedError := binding_constructors.NewStarlarkExecutionError(propagatedError.Error())
numServices := len(executor.serviceNetwork.GetServiceNames())
if err := executor.metricsClient.TrackKurtosisRunFinishedEvent(packageId, numServices, executionFailed); err != nil {
logrus.Errorf("An error occurred tracking kurtosis run finished event \n%s", err)
}
starlarkRunResponseLineStream <- binding_constructors.NewStarlarkRunResponseLineFromExecutionError(serializedError)
starlarkRunResponseLineStream <- binding_constructors.NewStarlarkRunResponseLineFromRunFailureEvent()
return
Expand All @@ -70,6 +83,10 @@ func (executor *StartosisExecutor) Execute(ctx context.Context, dryRun bool, par
}
}

numServices := len(executor.serviceNetwork.GetServiceNames())
if err := executor.metricsClient.TrackKurtosisRunFinishedEvent(packageId, numServices, executionFinishedSuccessfully); err != nil {
logrus.Errorf("An error occurred tracking kurtosis run finished event \n%s", err)
}
// TODO(gb): we should run magic string replacement on the output
starlarkRunResponseLineStream <- binding_constructors.NewStarlarkRunResponseLineFromRunSuccessEvent(serializedScriptOutput)
}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import (
"errors"
"github.com/kurtosis-tech/kurtosis/api/golang/core/kurtosis_core_rpc_api_bindings"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/binding_constructors"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/service"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/service_network"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction"
"github.com/kurtosis-tech/kurtosis/core/server/api_container/server/startosis_engine/kurtosis_instruction/mock_instruction"
metrics_client "github.com/kurtosis-tech/metrics-library/golang/lib/client"
"github.com/kurtosis-tech/metrics-library/golang/lib/source"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"strings"
Expand All @@ -22,15 +26,32 @@ const (

noScriptOutputObject = ""
noParallelism = 1

dummyPackageIdForTesting = "testing-package"
sourceVersionForTesting = ""
metricsUserIdForTesting = ""
backendTypeForTesting = ""
dontSendMetrics = false
dontFlushEnqueuedMetricsOnEachEvent = false
)

type doNothingMetricsClientCallback struct{}

func (d doNothingMetricsClientCallback) Success() {}
func (d doNothingMetricsClientCallback) Failure(err error) {}

var (
dummyPosition = kurtosis_instruction.NewInstructionPosition(12, 1, "dummyFile")
noInstructionArgsForTesting []*kurtosis_core_rpc_api_bindings.StarlarkInstructionArg
)

func TestExecuteKurtosisInstructions_ExecuteForReal_Success(t *testing.T) {
executor := NewStartosisExecutor()

executor, metricsClientCloser := newMockStartosisExecutorForTesting(t)
defer func() {
err := metricsClientCloser()
require.Nil(t, err)
}()

instruction1 := createMockInstruction(t, "instruction1", executeSuccessfully)
instruction2 := createMockInstruction(t, "instruction2", executeSuccessfully)
Expand All @@ -55,7 +76,11 @@ func TestExecuteKurtosisInstructions_ExecuteForReal_Success(t *testing.T) {
}

func TestExecuteKurtosisInstructions_ExecuteForReal_FailureHalfWay(t *testing.T) {
executor := NewStartosisExecutor()
executor, metricsClientCloser := newMockStartosisExecutorForTesting(t)
defer func() {
err := metricsClientCloser()
require.Nil(t, err)
}()

instruction1 := createMockInstruction(t, "instruction1", executeSuccessfully)
instruction2 := createMockInstruction(t, "instruction2", throwOnExecute)
Expand Down Expand Up @@ -92,7 +117,11 @@ instruction2()
}

func TestExecuteKurtosisInstructions_DoDryRun(t *testing.T) {
executor := NewStartosisExecutor()
executor, metricsClientCloser := newMockStartosisExecutorForTesting(t)
defer func() {
err := metricsClientCloser()
require.Nil(t, err)
}()

instruction1 := createMockInstruction(t, "instruction1", executeSuccessfully)
instruction2 := createMockInstruction(t, "instruction2", executeSuccessfully)
Expand Down Expand Up @@ -139,7 +168,8 @@ func createMockInstruction(t *testing.T, instructionName string, executeSuccessf
func executeSynchronously(t *testing.T, executor *StartosisExecutor, dryRun bool, instructions []kurtosis_instruction.KurtosisInstruction) (string, []*kurtosis_core_rpc_api_bindings.StarlarkInstruction, *kurtosis_core_rpc_api_bindings.StarlarkExecutionError) {
scriptOutput := strings.Builder{}
var serializedInstructions []*kurtosis_core_rpc_api_bindings.StarlarkInstruction
executionResponseLines := executor.Execute(context.Background(), dryRun, noParallelism, instructions, noScriptOutputObject)

executionResponseLines := executor.Execute(context.Background(), dryRun, noParallelism, instructions, noScriptOutputObject, dummyPackageIdForTesting)
for executionResponseLine := range executionResponseLines {
if executionResponseLine.GetError() != nil {
return scriptOutput.String(), serializedInstructions, executionResponseLine.GetError().GetExecutionError()
Expand All @@ -156,3 +186,12 @@ func executeSynchronously(t *testing.T, executor *StartosisExecutor, dryRun bool
}
return scriptOutput.String(), serializedInstructions, nil
}

func newMockStartosisExecutorForTesting(t *testing.T) (*StartosisExecutor, func() error) {
mockServiceNetwork := service_network.NewMockServiceNetwork(t)
mockServiceNetwork.EXPECT().GetServiceNames().Times(1).Return(map[service.ServiceName]bool{})
doNothingMetricsClient, metricsClientCloser, metricsClientCreationError := metrics_client.CreateMetricsClient(source.KurtosisCoreSource, sourceVersionForTesting, metricsUserIdForTesting, backendTypeForTesting, dontSendMetrics, dontFlushEnqueuedMetricsOnEachEvent, doNothingMetricsClientCallback{})
require.Nil(t, metricsClientCreationError)
executor := NewStartosisExecutor(doNothingMetricsClient, mockServiceNetwork)
return executor, metricsClientCloser
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewStartosisRunner(interpreter *StartosisInterpreter, validator *StartosisV
}
}

func (runner *StartosisRunner) Run(ctx context.Context, dryRun bool, parallelism int, moduleId string, serializedStartosis string, serializedParams string) <-chan *kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine {
func (runner *StartosisRunner) Run(ctx context.Context, dryRun bool, parallelism int, packageId string, serializedStartosis string, serializedParams string) <-chan *kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine {
// TODO(gb): add metric tracking maybe?
starlarkRunResponseLines := make(chan *kurtosis_core_rpc_api_bindings.StarlarkRunResponseLine)

Expand All @@ -43,7 +43,7 @@ func (runner *StartosisRunner) Run(ctx context.Context, dryRun bool, parallelism
startingInterpretationMsg, defaultCurrentStepNumber, defaultTotalStepsNumber)
starlarkRunResponseLines <- progressInfo

serializedScriptOutput, instructionsList, interpretationError := runner.startosisInterpreter.Interpret(ctx, moduleId, serializedStartosis, serializedParams)
serializedScriptOutput, instructionsList, interpretationError := runner.startosisInterpreter.Interpret(ctx, packageId, serializedStartosis, serializedParams)
if interpretationError != nil {
starlarkRunResponseLines <- binding_constructors.NewStarlarkRunResponseLineFromInterpretationError(interpretationError)
starlarkRunResponseLines <- binding_constructors.NewStarlarkRunResponseLineFromRunFailureEvent()
Expand All @@ -69,7 +69,7 @@ func (runner *StartosisRunner) Run(ctx context.Context, dryRun bool, parallelism
startingExecutionMsg, defaultCurrentStepNumber, totalNumberOfInstructions)
starlarkRunResponseLines <- progressInfo

executionResponseLinesChan := runner.startosisExecutor.Execute(ctx, dryRun, parallelism, instructionsList, serializedScriptOutput)
executionResponseLinesChan := runner.startosisExecutor.Execute(ctx, dryRun, parallelism, instructionsList, serializedScriptOutput, packageId)
if isRunFinished := forwardKurtosisResponseLineChannelUntilSourceIsClosed(executionResponseLinesChan, starlarkRunResponseLines); !isRunFinished {
logrus.Warnf("Execution finished but no 'RunFinishedEvent' was received through the stream. This is unexpected as every execution should be terminal.")
}
Expand Down
2 changes: 1 addition & 1 deletion core/server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.0
github.com/go-git/go-git/v5 v5.4.2
github.com/itchyny/gojq v0.12.9
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570
github.com/mholt/archiver v3.1.1+incompatible
github.com/pkg/errors v0.9.1
go.etcd.io/bbolt v1.3.6
Expand Down
4 changes: 2 additions & 2 deletions core/server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kurtosis-tech/free-ip-addr-tracker-lib v0.0.0-20211106222342-d3be9e82993e h1:CDNMjCNCvvCPSpoakxYZ3IHjBsxo9rLiMyjgvPYL87Q=
github.com/kurtosis-tech/free-ip-addr-tracker-lib v0.0.0-20211106222342-d3be9e82993e/go.mod h1:1PoW/0l4K80JRSj7A13aONsnaTdTM+dM36DIfDbYmVs=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230214135833-e362c2ee8600 h1:8GZVRAr16u7WCy/5xNsjZIgEcslh02cHCa3AzBSa7ME=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230214135833-e362c2ee8600/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e h1:VSoT53oeS53dGr28LtVVbwihdcRHQShsufbQjyHm/5M=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570 h1:2d8FxmA36Heb0uNziPof0Tqi70VhHr7H1cfoYY6IB9M=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/minimal-grpc-server/golang v0.0.0-20211201000847-a204edc5a0b3 h1:Begoh17x/9rLWQLM64A8oINjoQk1DFKN2iwhQf1i0Rc=
github.com/kurtosis-tech/minimal-grpc-server/golang v0.0.0-20211201000847-a204edc5a0b3/go.mod h1:Y0O+lt256iWpkdjDOB8r6csF96Pt3JVIFMbUuWIvr8o=
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409 h1:YQTATifMUwZEtZYb0LVA7DK2pj8s71iY8rzweuUQ5+g=
Expand Down
2 changes: 1 addition & 1 deletion engine/server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/kurtosis-tech/kurtosis/container-engine-lib v0.0.0 // local dependency
github.com/kurtosis-tech/kurtosis/core/launcher v0.0.0 // local dependency
github.com/kurtosis-tech/kurtosis/engine/launcher v0.0.0
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570
github.com/kurtosis-tech/minimal-grpc-server/golang v0.0.0-20211201000847-a204edc5a0b3
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409
github.com/sirupsen/logrus v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions engine/server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kurtosis-tech/free-ip-addr-tracker-lib v0.0.0-20211106222342-1f73d028840d h1:IWFwJfg2EhA/9Anv+VyG3LSTEZ/TZL30tUaVCFrzWK0=
github.com/kurtosis-tech/free-ip-addr-tracker-lib v0.0.0-20211106222342-1f73d028840d/go.mod h1:1PoW/0l4K80JRSj7A13aONsnaTdTM+dM36DIfDbYmVs=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230214135833-e362c2ee8600 h1:8GZVRAr16u7WCy/5xNsjZIgEcslh02cHCa3AzBSa7ME=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230214135833-e362c2ee8600/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e h1:VSoT53oeS53dGr28LtVVbwihdcRHQShsufbQjyHm/5M=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230215143841-da53ad89218e/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570 h1:2d8FxmA36Heb0uNziPof0Tqi70VhHr7H1cfoYY6IB9M=
github.com/kurtosis-tech/metrics-library/golang v0.0.0-20230216101811-98b2623f2570/go.mod h1:tteWV+M47xMHxqCIPQmdmgPW80rhN8YfzrgRRWbQhOw=
github.com/kurtosis-tech/minimal-grpc-server/golang v0.0.0-20211201000847-a204edc5a0b3 h1:Begoh17x/9rLWQLM64A8oINjoQk1DFKN2iwhQf1i0Rc=
github.com/kurtosis-tech/minimal-grpc-server/golang v0.0.0-20211201000847-a204edc5a0b3/go.mod h1:Y0O+lt256iWpkdjDOB8r6csF96Pt3JVIFMbUuWIvr8o=
github.com/kurtosis-tech/stacktrace v0.0.0-20211028211901-1c67a77b5409 h1:YQTATifMUwZEtZYb0LVA7DK2pj8s71iY8rzweuUQ5+g=
Expand Down

0 comments on commit 80f35c8

Please sign in to comment.