Skip to content

Commit

Permalink
feat(telemetry): added CommandExited durationMs field
Browse files Browse the repository at this point in the history
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 7d7c71a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
28 changes: 15 additions & 13 deletions pkg/telemetry/event.go
Expand Up @@ -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 }
14 changes: 5 additions & 9 deletions pkg/telemetry/telemetrywerfio.go
Expand Up @@ -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
Expand All @@ -69,6 +63,7 @@ func NewTelemetryWerfIO(url string, opts TelemetryWerfIOOptions) (*TelemetryWerf
),
traceExporter: e,
executionID: uuid.New().String(),
startedAt: time.Now(),
}, nil
}

Expand Down Expand Up @@ -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{} {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 7d7c71a

Please sign in to comment.