Skip to content

Commit

Permalink
Add Basic OTEL Tracing to TFBuddy (zapier#26)
Browse files Browse the repository at this point in the history
* add otel tracing to tfbuddy

Signed-off-by: Matt Plachter <matthew.plachter@zapier.com>

* change over from fmt to log

Signed-off-by: Matt Plachter <matthew.plachter@zapier.com>

* fix trace names

Signed-off-by: Matt Plachter <matthew.plachter@zapier.com>

---------

Signed-off-by: Matt Plachter <matthew.plachter@zapier.com>
  • Loading branch information
mplachter authored Aug 16, 2023
1 parent 780416e commit f55683f
Show file tree
Hide file tree
Showing 54 changed files with 1,366 additions and 514 deletions.
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

0 comments on commit f55683f

Please sign in to comment.