Skip to content
Permalink
Browse files
feat(telemetry): added CommandExited durationMs field
Also small refactoring, less go-code same functionality.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jul 18, 2022
1 parent a1282cc commit 7d7c71a4ad63de03ea34dd627c44b5d354f02c37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
@@ -9,30 +9,32 @@ const (

type Event interface {
GetType() EventType
GetData() interface{}
}

type CommandOption struct {
Name string `json:"name"`
AsCli bool `json:"asCli"`
AsEnv bool `json:"asEnv"`
Count int `json:"count"`
}

func NewCommandStarted(commandOptions []CommandOption) *CommandStarted {
return &CommandStarted{commandOptions: commandOptions}
return &CommandStarted{CommandOptions: commandOptions}
}

type CommandStarted struct {
commandOptions []CommandOption
CommandOptions []CommandOption `json:"commandOptions,omitempty"`
}

func (e *CommandStarted) GetType() EventType { return CommandStartedEvent }
func (e *CommandStarted) GetData() interface{} {
if len(e.commandOptions) > 0 {
return map[string]interface{}{"commandOptions": e.commandOptions}
}
return nil
}

func NewCommandExited(exitCode int) *CommandExited { return &CommandExited{exitCode: exitCode} }
func NewCommandExited(exitCode int, durationMs int64) *CommandExited {
return &CommandExited{ExitCode: exitCode, DurationMs: durationMs}
}

type CommandExited struct {
exitCode int
ExitCode int `json:"exitCode"`
DurationMs int64 `json:"durationMs"`
}

func (e *CommandExited) GetType() EventType { return CommandExitedEvent }
func (e *CommandExited) GetData() interface{} { return map[string]interface{}{"exitCode": e.exitCode} }
func (e *CommandExited) GetType() EventType { return CommandExitedEvent }
@@ -31,18 +31,12 @@ type TelemetryWerfIOInterface interface {
CommandExited(ctx context.Context, exitCode int)
}

type CommandOption struct {
Name string `json:"name"`
AsCli bool `json:"asCli"`
AsEnv bool `json:"asEnv"`
Count int `json:"count"`
}

type TelemetryWerfIO struct {
handleErrorFunc func(err error)
tracerProvider *sdktrace.TracerProvider
traceExporter *otlptrace.Exporter

startedAt time.Time
executionID string
projectID string
command string
@@ -69,6 +63,7 @@ func NewTelemetryWerfIO(url string, opts TelemetryWerfIOOptions) (*TelemetryWerf
),
traceExporter: e,
executionID: uuid.New().String(),
startedAt: time.Now(),
}, nil
}

@@ -124,7 +119,8 @@ func (t *TelemetryWerfIO) CommandStarted(ctx context.Context) {
}

func (t *TelemetryWerfIO) CommandExited(ctx context.Context, exitCode int) {
t.sendEvent(ctx, NewCommandExited(exitCode))
duration := time.Now().Sub(t.startedAt)
t.sendEvent(ctx, NewCommandExited(exitCode, int64(duration/time.Millisecond)))
}

func (t *TelemetryWerfIO) getAttributes() map[string]interface{} {
@@ -161,7 +157,7 @@ func (t *TelemetryWerfIO) sendEvent(ctx context.Context, event Event) error {

span.SetAttributes(attribute.Key("eventType").String(string(event.GetType())))

rawEventData, err := json.Marshal(event.GetData())
rawEventData, err := json.Marshal(event)
if err != nil {
return fmt.Errorf("unable to marshal event data: %w", err)
}

0 comments on commit 7d7c71a

Please sign in to comment.