Skip to content

Commit

Permalink
chore(telemetry): full async mode for telemetrywerfio package, no err…
Browse files Browse the repository at this point in the history
…ors in logs

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jul 6, 2022
1 parent 3d20113 commit 9a31346
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
23 changes: 17 additions & 6 deletions cmd/werf/common/telemetry.go
Expand Up @@ -2,21 +2,32 @@ package common

import (
"context"
"fmt"
"os"

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

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

func ShutdownTelemetry(ctx context.Context, exitCode int) {
// TODO: append error to the ~/.werf/telemetry_errors.log
if err := telemetry.Shutdown(ctx); err != nil {
logboek.Context(ctx).Debug().LogF("Telemetry: shutdown error: %s\n", err)
logTelemetryError(ctx, fmt.Sprintf("unable to shutdown: %s", err))
}
}

func logTelemetryError(ctx context.Context, msg string) {
if !util.GetBoolEnvironmentDefaultFalse("WERF_TELEMETRY_LOGS") {
return
}
fmt.Fprintf(os.Stderr, "Telemetry error: %s\n", msg)
}
5 changes: 3 additions & 2 deletions cmd/werf/main.go
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -75,7 +74,7 @@ func getFullCommandName(cmd *cobra.Command) string {
}

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

common.InitTelemetry(ctx)

Expand Down Expand Up @@ -138,6 +137,8 @@ func main() {
common.TerminateWithError(err.Error(), 1)
}
}

common.ShutdownTelemetry(ctx, 0)
}

func constructRootCmd() *cobra.Command {
Expand Down
22 changes: 21 additions & 1 deletion pkg/telemetry/telemetry.go
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"go.opentelemetry.io/otel"

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

Expand All @@ -20,7 +22,11 @@ func GetTelemetryWerfIO() TelemetryWerfIOInterface {
return telemetrywerfio
}

func Init(ctx context.Context) error {
type TelemetryOptions struct {
ErrorHandlerFunc func(err error)
}

func Init(ctx context.Context, opts TelemetryOptions) error {
if !IsEnabled() {
return nil
}
Expand All @@ -31,9 +37,23 @@ func Init(ctx context.Context) error {
telemetrywerfio = t
}

otel.SetErrorHandler(&callFuncErrorHandler{f: opts.ErrorHandlerFunc})

if err := telemetrywerfio.Start(ctx); err != nil {
return fmt.Errorf("unable to start telemetry.werf.io exporter: %w", err)
}

return nil
}

type callFuncErrorHandler struct{ f func(error) }

func (h *callFuncErrorHandler) Handle(err error) {
if h.f != nil {
h.f(err)
}
}

func Shutdown(ctx context.Context) error {
if !IsEnabled() {
return nil
Expand Down
11 changes: 8 additions & 3 deletions pkg/telemetry/telemetrywerfio.go
Expand Up @@ -34,9 +34,14 @@ func NewTelemetryWerfIO(url string) (*TelemetryWerfIO, error) {
}

return &TelemetryWerfIO{
tracerProvider: sdktrace.NewTracerProvider(sdktrace.WithSyncer(e)),
traceExporter: e,
executionID: uuid.New().String(),
tracerProvider: sdktrace.NewTracerProvider(
sdktrace.WithBatcher(e,
sdktrace.WithBatchTimeout(0),
sdktrace.WithExportTimeout(3*time.Second),
),
),
traceExporter: e,
executionID: uuid.New().String(),
}, nil
}

Expand Down

0 comments on commit 9a31346

Please sign in to comment.