Skip to content

Commit

Permalink
fix: refactor telegraf version (#11656)
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasv authored Aug 17, 2022
1 parent 0481a78 commit 447e8a3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 76 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ MAKEFLAGS += --no-print-directory
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
HOSTGO := env -u GOOS -u GOARCH -u GOARM -- go

LDFLAGS := $(LDFLAGS) -X main.commit=$(commit) -X main.branch=$(branch) -X main.goos=$(GOOS) -X main.goarch=$(GOARCH)
INTERNAL_PKG=github.com/influxdata/telegraf/internal
LDFLAGS := $(LDFLAGS) -X $(INTERNAL_PKG).commit=$(commit) -X $(INTERNAL_PKG).branch=$(branch)
ifneq ($(tag),)
LDFLAGS += -X main.version=$(version)
LDFLAGS += -X $(INTERNAL_PKG).version=$(version)
else
LDFLAGS += -X main.version=$(version)-$(commit)
LDFLAGS += -X $(INTERNAL_PKG).version=$(version)-$(commit)
endif

# Go built-in race detector works only for 64 bits architectures.
Expand Down
40 changes: 3 additions & 37 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ var fPlugins = flag.String("plugin-directory", "",
"path to directory containing external plugins")
var fRunOnce = flag.Bool("once", false, "run one gather and exit")

var (
version string
commit string
branch string
)

var stop chan struct{}

func reloadLoop(
Expand Down Expand Up @@ -277,7 +271,7 @@ func runAgent(ctx context.Context,

logger.SetupLogging(logConfig)

log.Printf("I! Starting Telegraf %s", version)
log.Printf("I! Starting Telegraf %s", internal.Version())
log.Printf("I! Loaded inputs: %s", strings.Join(c.InputNames(), " "))
log.Printf("I! Loaded aggregators: %s", strings.Join(c.AggregatorNames(), " "))
log.Printf("I! Loaded processors: %s", strings.Join(c.ProcessorNames(), " "))
Expand Down Expand Up @@ -348,29 +342,6 @@ func usageExit(rc int) {
os.Exit(rc)
}

func formatFullVersion() string {
var parts = []string{"Telegraf"}

if version != "" {
parts = append(parts, version)
} else {
parts = append(parts, "unknown")
}

if branch != "" || commit != "" {
if branch == "" {
branch = "unknown"
}
if commit == "" {
commit = "unknown"
}
git := fmt.Sprintf("(git: %s %s)", branch, commit)
parts = append(parts, git)
}

return strings.Join(parts, " ")
}

func deleteEmpty(s []string) []string {
var r []string
for _, str := range s {
Expand Down Expand Up @@ -410,11 +381,6 @@ func main() {

logger.SetupLogging(logger.LogConfig{})

// Configure version
if err := internal.SetVersion(version); err != nil {
log.Println("Telegraf version already configured to: " + internal.Version())
}

// Load external plugins, if requested.
if *fPlugins != "" {
log.Printf("I! Loading external plugins from: %s", *fPlugins)
Expand Down Expand Up @@ -443,7 +409,7 @@ func main() {
if len(args) > 0 {
switch args[0] {
case "version":
fmt.Println(formatFullVersion())
fmt.Println(internal.FormatFullVersion())
return
case "config":
err := configCmd.Parse(args[1:])
Expand Down Expand Up @@ -534,7 +500,7 @@ func main() {
}
return
case *fVersion:
fmt.Println(formatFullVersion())
fmt.Println(internal.FormatFullVersion())
return
case *fSampleConfig:
printer.PrintSampleConfig(
Expand Down
47 changes: 30 additions & 17 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,48 @@ import (
const alphanum string = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

var (
ErrTimeout = errors.New("command timed out")
ErrorNotImplemented = errors.New("not implemented yet")
ErrorVersionAlreadySet = errors.New("version has already been set")
ErrTimeout = errors.New("command timed out")
ErrorNotImplemented = errors.New("not implemented yet")
)

// Set via the main module
var version string
// Set via LDFLAGS -X
var (
version = "unknown"
branch = ""
commit = ""
)

type ReadWaitCloser struct {
pipeReader *io.PipeReader
wg sync.WaitGroup
}

// SetVersion sets the telegraf agent version
func SetVersion(v string) error {
// Version returns the telegraf agent version
func Version() string {
return version
}

func FormatFullVersion() string {
var parts = []string{"Telegraf"}

if version != "" {
return ErrorVersionAlreadySet
}
version = v
if version == "" {
version = "unknown"
parts = append(parts, version)
} else {
parts = append(parts, "unknown")
}

return nil
}
if branch != "" || commit != "" {
if branch == "" {
branch = "unknown"
}
if commit == "" {
commit = "unknown"
}
git := fmt.Sprintf("(git: %s@%s)", branch, commit)
parts = append(parts, git)
}

// Version returns the telegraf agent version
func Version() string {
return version
return strings.Join(parts, " ")
}

// ProductToken returns a tag for Telegraf that can be used in user agents.
Expand Down
12 changes: 0 additions & 12 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,6 @@ func TestCompressWithGzipEarlyClose(t *testing.T) {
assert.Equal(t, r1, r2)
}

func TestVersionAlreadySet(t *testing.T) {
err := SetVersion("foo")
assert.NoError(t, err)

err = SetVersion("bar")

assert.Error(t, err)
assert.IsType(t, ErrorVersionAlreadySet, err)

assert.Equal(t, "foo", Version())
}

func TestAlignDuration(t *testing.T) {
tests := []struct {
name string
Expand Down
7 changes: 4 additions & 3 deletions plugins/inputs/internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ func TestSelfPlugin(t *testing.T) {
stat.Incr(1)
stat.Incr(2)
require.NoError(t, s.Gather(acc))

acc.AssertContainsTaggedFields(t, "internal_mytest",
map[string]interface{}{
"test": int64(3),
},
map[string]string{
"test": "foo",
"version": "",
"version": "unknown",
},
)
acc.ClearMetrics()
Expand All @@ -41,7 +42,7 @@ func TestSelfPlugin(t *testing.T) {
},
map[string]string{
"test": "foo",
"version": "",
"version": "unknown",
},
)
acc.ClearMetrics()
Expand All @@ -59,7 +60,7 @@ func TestSelfPlugin(t *testing.T) {
},
map[string]string{
"test": "foo",
"version": "",
"version": "unknown",
},
)
}
5 changes: 2 additions & 3 deletions plugins/outputs/opentelemetry/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package opentelemetry
import (
"context"
_ "embed"
"fmt"
"runtime"
"time"

ntls "crypto/tls"
Expand All @@ -23,11 +21,12 @@ import (

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/outputs"
)

var userAgent = fmt.Sprintf("telegraf (%s/%s)", runtime.GOOS, runtime.GOARCH)
var userAgent = internal.ProductToken()

// DO NOT REMOVE THE NEXT TWO LINES! This is required to embed the sampleConfig data.
//go:embed sample.conf
Expand Down

0 comments on commit 447e8a3

Please sign in to comment.