Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Basic OTEL Tracing to TFBuddy #26

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ build-binary:

WORKDIR /src
COPY . /src
RUN GOARM=${VARIANT#v} go build -ldflags "-X main.GitCommit=$GIT_COMMIT -X main.GitTag=$GIT_TAG" -o tfbuddy
RUN GOARM=${VARIANT#v} go build -ldflags "-X github.com/zapier/tfbuddy/pkg.GitCommit=$GIT_COMMIT -X github.com/zapier/tfbuddy/pkg.GitTag=$GIT_TAG" -o tfbuddy
SAVE ARTIFACT tfbuddy

build-docker:
Expand Down
5 changes: 3 additions & 2 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ test_go(
labels=["tfbuddy"]
)

build_cmd='CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -gcflags="all=-N -l" -o build/tfbuddy ./'
arch="arm64" if str(local("uname -m")).strip('\n') == "arm64" else "amd64"
build_cmd='CGO_ENABLED=0 GOOS=linux GOARCH={} go build -gcflags="all=-N -l" -o build/tfbuddy ./'
local_resource(
'go-build',
build_cmd,
build_cmd.format(arch),
deps=[
'./main.go',
'./go.mod',
Expand Down
29 changes: 28 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package cmd

import (
"context"
"fmt"
"log"
"os"
"strconv"
"strings"

"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/zapier/tfbuddy/internal/logging"
"github.com/zapier/tfbuddy/internal/telemetry"
"github.com/zapier/tfbuddy/pkg"

"github.com/spf13/viper"
)
Expand All @@ -33,7 +39,7 @@ func resolveLogLevel() zerolog.Level {

lvl, err := zerolog.ParseLevel(logLevel)
if err != nil {
fmt.Println("could not parse log level, defaulting to 'info'")
log.Println("could not parse log level, defaulting to 'info'")
lvl = zerolog.InfoLevel
}
return lvl
Expand All @@ -51,6 +57,25 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "v", "info", "Set the log output level (info, debug, trace)")
}

func initTelemetry(ctx context.Context) (*telemetry.OperatorTelemetry, error) {
enableOtel, _ := strconv.ParseBool(os.Getenv("TFBUDDY_OTEL_ENABLED"))
otelHost := os.Getenv("TFBUDDY_OTEL_COLLECTOR_HOST")
otelPort := os.Getenv("TFBUDDY_OTEL_COLLECTOR_PORT")

opts := telemetry.Options{
Enabled: enableOtel,
Host: otelHost,
Port: otelPort,
Version: pkg.GitTag,
CommitSHA: pkg.GitCommit,
}
log.Printf("enabled: %v\thost: %s\tport: %s\n", enableOtel, otelHost, otelPort)

log.Printf("OpenTelemetry Opts: %+v\n", opts)

return telemetry.Init(ctx, "tfbuddy", opts)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
Expand All @@ -67,6 +92,8 @@ func initConfig() {
viper.SetConfigName(".tfbuddy")
}

viper.SetEnvPrefix("TFBUDDY")
viper.EnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv() // read in environment variables that match

// If a config file is found, read it in.
Expand Down
16 changes: 14 additions & 2 deletions cmd/tfc_hook_handler.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
package cmd

import (
"context"

"github.com/spf13/cobra"

"github.com/zapier/tfbuddy/internal/logging"
"github.com/zapier/tfbuddy/pkg/hooks"
)

var gitlabToken string
var otelEnabled bool
var otelCollectorHost string
var otelCollectorPort string

// tfcHookHandlerCmd represents the run command
var tfcHookHandlerCmd = &cobra.Command{
Use: "handler",
Short: "Start a hooks handler for Gitlab & Terraform cloud.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
logging.SetupLogOutput(resolveLogLevel())
ctx := context.Background()

t, err := initTelemetry(ctx)
if err != nil {
panic(err)
}
defer t.Shutdown()

hooks.StartServer()

},
PreRunE: func(cmd *cobra.Command, args []string) error {
return nil
Expand Down
4 changes: 3 additions & 1 deletion cmd/tfc_lock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"os"

Expand All @@ -14,7 +15,8 @@ var tfcLockCmd = &cobra.Command{
Short: "Lock a Terraform workspace.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
tfc_utils.LockUnlockWorkspace(tfcToken, tfcWorkspace, true, "Locked from MR")
ctx := context.Background()
tfc_utils.LockUnlockWorkspace(ctx, tfcToken, tfcWorkspace, true, "Locked from MR")
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if tfcWorkspace == "" {
Expand Down
4 changes: 3 additions & 1 deletion cmd/tfc_unlock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"context"
"fmt"
"os"

Expand All @@ -14,7 +15,8 @@ var tfcUnlockCmd = &cobra.Command{
Short: "Unlock a Terraform workspace.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
tfc_utils.LockUnlockWorkspace(tfcToken, tfcWorkspace, false, "")
ctx := context.Background()
tfc_utils.LockUnlockWorkspace(ctx, tfcToken, tfcWorkspace, false, "")
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if tfcWorkspace == "" {
Expand Down
45 changes: 31 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@ require (
github.com/jessevdk/go-flags v1.5.0
github.com/kr/pretty v0.3.1
github.com/labstack/echo-contrib v0.13.0
github.com/labstack/echo/v4 v4.9.1
github.com/labstack/echo/v4 v4.10.2
github.com/nats-io/nats-server/v2 v2.9.8
github.com/nats-io/nats.go v1.21.0
github.com/nats-io/nats.go v1.25.0
github.com/prometheus/client_golang v1.14.0
github.com/rs/zerolog v1.28.0
github.com/rzajac/zltest v0.12.0
github.com/sl1pm4t/gongs v0.0.0-20221205005205-6f4e6d147fab
github.com/sl1pm4t/gongs v0.0.0-20230501190600-06976a7fac23
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.4
github.com/xanzy/go-gitlab v0.77.0
github.com/ziflex/lecho/v3 v3.3.0
golang.org/x/oauth2 v0.3.0
go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho v0.40.0
go.opentelemetry.io/contrib/instrumentation/runtime v0.40.0
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v0.37.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0
go.opentelemetry.io/otel/metric v0.37.0
go.opentelemetry.io/otel/sdk v1.14.0
go.opentelemetry.io/otel/sdk/metric v0.37.0
go.opentelemetry.io/otel/trace v1.14.0
golang.org/x/oauth2 v0.4.0
google.golang.org/grpc v1.53.0
gopkg.in/dealancer/validate.v2 v2.1.0
gopkg.in/errgo.v2 v2.1.0
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -48,9 +58,12 @@ require (
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/hashicorp/go-slug v0.11.1 // indirect
Expand All @@ -67,14 +80,13 @@ require (
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/nats-io/jwt/v2 v2.3.0 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pjbgf/sha1cd v0.2.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -88,20 +100,25 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/zclconf/go-cty v1.13.2 // indirect
golang.org/x/crypto v0.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlpmetric v0.37.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.5.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading
Loading