Skip to content

Commit

Permalink
chore(telemetry): refine telemetry package interface
Browse files Browse the repository at this point in the history
* TelemetryWerfIO interface with methods to send "CommandStarted" event.
* Commented out metrics related initialization code.

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jul 4, 2022
1 parent 950d235 commit c48f2e3
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 165 deletions.
15 changes: 6 additions & 9 deletions cmd/werf/common/telemetry.go
Expand Up @@ -9,17 +9,14 @@ import (

func InitTelemetry(ctx context.Context) {
// TODO: append error to the ~/.werf/telemetry_errors.log
err := telemetry.Init(ctx)
logboek.Context(ctx).Debug().LogF("Telemetry: init error: %s\n", err)
if err := telemetry.Init(ctx); err != nil {
logboek.Context(ctx).Debug().LogF("Telemetry: init error: %s\n", err)
}
}

func ShutdownTelemetry(ctx context.Context, exitCode int) {
telemetry.ChangeMetrics(func(metrics *telemetry.Metrics) {
metrics.ExitCode = exitCode
})

// TODO: append error to the ~/.werf/telemetry_errors.log
err := telemetry.Shutdown(ctx)

logboek.Context(ctx).Debug().LogF("Telemetry: shutdown error: %s\n", err)
if err := telemetry.Shutdown(ctx); err != nil {
logboek.Context(ctx).Debug().LogF("Telemetry: shutdown error: %s\n", err)
}
}
53 changes: 51 additions & 2 deletions cmd/werf/main.go
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,13 +54,31 @@ import (
"github.com/werf/werf/pkg/telemetry"
)

func getFullCommandName(cmd *cobra.Command) string {
commandParts := []string{cmd.Name()}
c := cmd
for {
p := c.Parent()
if p == nil {
break
}
commandParts = append(commandParts, p.Name())
c = p
}

var p []string
for i := 0; i < len(commandParts); i++ {
p = append(p, commandParts[len(commandParts)-i-1])
}

return strings.Join(p, " ")
}

func main() {
ctx := context.Background()

common.InitTelemetry(ctx)

telemetry.MessageEvent(ctx, "command started")

shouldTerminate, err := common.ContainerBackendProcessStartupHook()
if err != nil {
common.ShutdownTelemetry(ctx, 1)
Expand All @@ -80,6 +99,36 @@ func main() {

rootCmd := constructRootCmd()

commandsQueue := []*cobra.Command{rootCmd}
for len(commandsQueue) > 0 {
cmd := commandsQueue[0]
commandsQueue = commandsQueue[1:]

commandsQueue = append(commandsQueue, cmd.Commands()...)

if cmd.Runnable() {
oldRunE := cmd.RunE
cmd.RunE = nil

oldRun := cmd.Run
cmd.Run = nil

cmd.RunE = func(cmd *cobra.Command, args []string) error {
telemetry.GetTelemetryWerfIO().SetCommand(getFullCommandName(cmd))
telemetry.GetTelemetryWerfIO().CommandStarted(ctx)

if oldRunE != nil {
return oldRunE(cmd, args)
} else if oldRun != nil {
oldRun(cmd, args)
return nil
} else {
panic(fmt.Sprintf("unexpected command %q, please report bug to the https://github.com/werf/werf", cmd.Name()))
}
}
}
}

if err := rootCmd.Execute(); err != nil {
if helm_v3.IsPluginError(err) {
common.ShutdownTelemetry(ctx, helm_v3.PluginErrorCode(err))
Expand Down
35 changes: 0 additions & 35 deletions pkg/telemetry/event.go

This file was deleted.

25 changes: 0 additions & 25 deletions pkg/telemetry/trace_exporter.go → pkg/telemetry/helpers.go
@@ -1,39 +1,14 @@
package telemetry

import (
"context"
"fmt"
neturl "net/url"
"time"

"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/sdk/trace"
)

var (
tracerProvider *trace.TracerProvider
traceExporter *otlptrace.Exporter
)

func SetupTraceExporter(ctx context.Context, url string) error {
{
e, err := NewTraceExporter(url)
if err != nil {
return fmt.Errorf("unable to create telemetry trace exporter: %w", err)
}
traceExporter = e
}

tracerProvider = trace.NewTracerProvider(trace.WithSyncer(traceExporter))

if err := traceExporter.Start(ctx); err != nil {
return fmt.Errorf("error starting telemetry trace exporter: %w", err)
}

return nil
}

func NewTraceExporter(url string) (*otlptrace.Exporter, error) {
urlObj, err := neturl.Parse(url)
if err != nil {
Expand Down
44 changes: 32 additions & 12 deletions pkg/telemetry/metric_exporter.go
@@ -1,17 +1,6 @@
package telemetry

import (
"context"
"fmt"
neturl "net/url"
"time"

"go.opentelemetry.io/otel/exporters/otlp/otlpmetric"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
controller "go.opentelemetry.io/otel/sdk/metric/controller/basic"
processor "go.opentelemetry.io/otel/sdk/metric/processor/basic"
"go.opentelemetry.io/otel/sdk/metric/selector/simple"
)
/* Metric exporter initialization example
var (
ctrl *controller.Controller
Expand Down Expand Up @@ -70,3 +59,34 @@ func NewController(exporter *otlpmetric.Exporter) *controller.Controller {
controller.WithPushTimeout(5*time.Second),
)
}
*/

/* Metric exporter usage example
type Metrics struct {
Version string
Command string
OS string
Arch string
ExitCode int
}
func (m *Metrics) WriteOpenTelemetry(ctx context.Context, meter metric.Meter) error {
labels := []attribute.KeyValue{
attribute.String("version", m.Version),
attribute.String("command", m.Command),
attribute.String("os", m.OS),
attribute.String("arch", m.Arch),
attribute.Int("exit_code", m.ExitCode),
}
runCounter, err := meter.SyncInt64().Counter("runs", instrument.WithDescription("werf runs counter"))
if err != nil {
return fmt.Errorf("unable to record runs counter: %w", err)
}
runCounter.Add(ctx, 1, labels...)
logboek.Context(ctx).Debug().LogF("Telemetry: incremented runs counter: %#v\n", runCounter)
return nil
}
*/
39 changes: 0 additions & 39 deletions pkg/telemetry/metrics.go

This file was deleted.

9 changes: 9 additions & 0 deletions pkg/telemetry/no_telemetrywerfio.go
@@ -0,0 +1,9 @@
package telemetry

import "context"

type NoTelemetryWerfIO struct{}

func (t *NoTelemetryWerfIO) CommandStarted(context.Context) {}
func (t *NoTelemetryWerfIO) SetProjectID(string) {}
func (t *NoTelemetryWerfIO) SetCommand(string) {}
54 changes: 11 additions & 43 deletions pkg/telemetry/telemetry.go
Expand Up @@ -3,53 +3,32 @@ package telemetry
import (
"context"
"fmt"
"os"
"runtime"

"github.com/google/uuid"

"github.com/werf/logboek"
"github.com/werf/werf/pkg/util"
"github.com/werf/werf/pkg/werf"
)

const (
MetricsURL = "http://localhost:4318/v1/metrics"
TracesURL = "http://localhost:4318/v1/traces"
TracesURL = "http://localhost:4318/v1/traces"
)

var (
executionID string
projectID string
command string
metrics *Metrics
)
var telemetrywerfio *TelemetryWerfIO

func ChangeMetrics(changeFunc func(metrics *Metrics)) {
if !IsEnabled() {
return
func GetTelemetryWerfIO() TelemetryWerfIOInterface {
if telemetrywerfio == nil {
return &NoTelemetryWerfIO{}
}
changeFunc(metrics)
return telemetrywerfio
}

func Init(ctx context.Context) error {
if !IsEnabled() {
return nil
}

executionID = uuid.New().String()
projectID = "26a38bba-51ed-4f93-8104-e6f51a5454ef"
command = fmt.Sprintf("%v", os.Args)

metrics = &Metrics{
Version: werf.Version,
Command: "TODO",
OS: runtime.GOOS,
Arch: runtime.GOARCH,
}

if err := SetupTraceExporter(ctx, TracesURL); err != nil {
return fmt.Errorf("unable to setup telemetry traces exporter for url %q: %w", TracesURL, err)
if t, err := NewTelemetryWerfIO(TracesURL); err != nil {
return fmt.Errorf("unable to setup telemetry.werf.io exporter: %w", err)
} else {
telemetrywerfio = t
}

return nil
Expand All @@ -59,18 +38,7 @@ func Shutdown(ctx context.Context) error {
if !IsEnabled() {
return nil
}

if err := traceExporter.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown trace exporter: %w", err)
}

if err := tracerProvider.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown trace provider: %w", err)
}

logboek.Context(ctx).Debug().LogF("Telemetry collection done\n")

return nil
return telemetrywerfio.Shutdown(ctx)
}

func IsEnabled() bool {
Expand Down

0 comments on commit c48f2e3

Please sign in to comment.