Skip to content
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
10 changes: 9 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ tag := $(shell git describe --exact-match --tags 2>/dev/null)

branch := $(shell git rev-parse --abbrev-ref HEAD)
commit := $(shell git rev-parse --short=8 HEAD)
upstream_version ?= 1.32.1

ifdef NIGHTLY
version := $(next_version)
Expand All @@ -32,6 +33,13 @@ else ifneq ($(findstring -rc,$(tag)),)
deb_version := $(version)~$(rc)-1
deb_iteration := 0
tar_version := $(version)~$(rc)
else ifneq ($(filter bklite-%,$(tag)),)
version := $(patsubst bklite-%,%,$(tag))
rpm_version := $(version)-1
rpm_iteration := 1
deb_version := $(version)-1
deb_iteration := 1
tar_version := $(version)
else
version := $(tag:v%=%)
rpm_version := $(version)-1
Expand All @@ -46,7 +54,7 @@ GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
HOSTGO := env -u GOOS -u GOARCH -u GOARM -- go
INTERNAL_PKG=github.com/influxdata/telegraf/internal
LDFLAGS := $(LDFLAGS) -X $(INTERNAL_PKG).Commit=$(commit) -X $(INTERNAL_PKG).Branch=$(branch)
LDFLAGS := $(LDFLAGS) -X $(INTERNAL_PKG).Commit=$(commit) -X $(INTERNAL_PKG).Branch=$(branch) -X $(INTERNAL_PKG).Upstream=$(upstream_version)
ifneq ($(tag),)
LDFLAGS += -X $(INTERNAL_PKG).Version=$(version)
else
Expand Down
7 changes: 7 additions & 0 deletions cmd/telegraf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
return fmt.Errorf("%w and %w", err, err2)
}
return nil
case cCtx.Bool("upstream-version"):
fmt.Fprintf(outputBuffer, "%s\n", internal.FormatUpstreamVersion())
return nil
// DEPRECATED
case cCtx.Bool("version"):
fmt.Fprintf(outputBuffer, "%s\n", internal.FormatFullVersion())
Expand Down Expand Up @@ -356,6 +359,10 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
Name: "version",
Usage: "DEPRECATED: display the version and exit",
},
&cli.BoolFlag{
Name: "upstream-version",
Usage: "print the upstream Telegraf version compiled into this binary and exit",
},
// Already covered with the subcommand `./telegraf config`
&cli.BoolFlag{
Name: "sample-config",
Expand Down
71 changes: 33 additions & 38 deletions cmd/telegraf/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,30 +435,21 @@ func TestCommandVersion(t *testing.T) {
ExpectedOutput string
}{
{
Version: "v2.0.0",
ExpectedOutput: "Telegraf v2.0.0\n",
Version: "0.5.0",
Branch: "bklite",
Commit: "abcdef",
ExpectedOutput: "0.5.0\n",
},
{
ExpectedOutput: "Telegraf unknown\n",
},
{
Version: "v2.0.0",
Branch: "master",
ExpectedOutput: "Telegraf v2.0.0 (git: master@unknown)\n",
},
{
Version: "v2.0.0",
Branch: "master",
Commit: "123",
ExpectedOutput: "Telegraf v2.0.0 (git: master@123)\n",
},
{
Version: "v2.0.0",
Commit: "123",
ExpectedOutput: "Telegraf v2.0.0 (git: unknown@123)\n",
ExpectedOutput: "unknown\n",
},
}

origVersion, origBranch, origCommit := internal.Version, internal.Branch, internal.Commit
t.Cleanup(func() {
internal.Version, internal.Branch, internal.Commit = origVersion, origBranch, origCommit
})

for _, test := range tests {
buf := new(bytes.Buffer)
args := os.Args[0:1]
Expand All @@ -481,30 +472,21 @@ func TestFlagVersion(t *testing.T) {
ExpectedOutput string
}{
{
Version: "v2.0.0",
ExpectedOutput: "Telegraf v2.0.0\n",
},
{
ExpectedOutput: "Telegraf unknown\n",
},
{
Version: "v2.0.0",
Branch: "master",
ExpectedOutput: "Telegraf v2.0.0 (git: master@unknown)\n",
},
{
Version: "v2.0.0",
Branch: "master",
Commit: "123",
ExpectedOutput: "Telegraf v2.0.0 (git: master@123)\n",
Version: "0.5.0",
Branch: "bklite",
Commit: "abcdef",
ExpectedOutput: "0.5.0\n",
},
{
Version: "v2.0.0",
Commit: "123",
ExpectedOutput: "Telegraf v2.0.0 (git: unknown@123)\n",
ExpectedOutput: "unknown\n",
},
}

origVersion, origBranch, origCommit := internal.Version, internal.Branch, internal.Commit
t.Cleanup(func() {
internal.Version, internal.Branch, internal.Commit = origVersion, origBranch, origCommit
})

for _, test := range tests {
buf := new(bytes.Buffer)
args := os.Args[0:1]
Expand All @@ -518,6 +500,19 @@ func TestFlagVersion(t *testing.T) {
}
}

func TestFlagUpstreamVersion(t *testing.T) {
orig := internal.Upstream
t.Cleanup(func() { internal.Upstream = orig })
internal.Upstream = "1.32.1"
internal.Version = "0.5.0"

buf := new(bytes.Buffer)
args := append(os.Args[0:1], "--upstream-version")
err := runApp(args, buf, NewMockServer(), NewMockConfig(buf), NewMockTelegraf())
require.NoError(t, err)
require.Equal(t, "1.32.1\n", buf.String())
}

func TestGlobablBoolFlags(t *testing.T) {
commands := []string{
"--debug",
Expand Down
37 changes: 17 additions & 20 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,34 @@ var (

// Set via LDFLAGS -X
var (
Version = "unknown"
Branch = ""
Commit = ""
Version = "unknown"
Upstream = "1.32.1"
Branch = ""
Commit = ""
)

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

// FormatFullVersion is the BKLite release version printed by --version.
// Packaging stores this stdout as the node-management version, so it is only
// the x.y.z release number.
func FormatFullVersion() string {
var parts = []string{"Telegraf"}

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

if Branch != "" || Commit != "" {
if Branch == "" {
Branch = "unknown"
}
if Commit == "" {
Commit = "unknown"
}
git := fmt.Sprintf("(git: %s@%s)", Branch, Commit)
parts = append(parts, git)
// FormatUpstreamVersion is the upstream Telegraf version compiled into the
// binary. It is not part of --version.
func FormatUpstreamVersion() string {
if Upstream == "" {
return "unknown"
}

return strings.Join(parts, " ")
return Upstream
}

// ProductToken returns a tag for Telegraf that can be used in user agents.
Expand Down
5 changes: 5 additions & 0 deletions plugins/inputs/all/bklite_rocketmq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//go:build !custom || inputs || inputs.bklite_rocketmq

package all

import _ "github.com/influxdata/telegraf/plugins/inputs/bklite_rocketmq" // register plugin
46 changes: 46 additions & 0 deletions plugins/inputs/bklite_rocketmq/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# BKLite RocketMQ Input Plugin

`bklite_rocketmq` collects RocketMQ broker, producer, consumer, and consumer-group metrics through the NameServer admin protocol.

## Versions

The target is **RocketMQ 4.9.x** (4.9.4 through 4.9.8). That is the current 4.x line still running in production. The admin calls are the same remoting protocol used since 4.4, when ACL was added, so 4.4–4.8 clusters that speak this protocol also match.

RocketMQ **5.x** (current release 5.5.1) is a separate line. Since 5.1 it exposes its own Prometheus `/metrics`. This plugin does not collect that endpoint. A later BK-Lite pull path can select it; the measurement names here stay `bklite_rocketmq_*` so the two paths can share one metric contract.

`rocketmq-client-go` v2.1.2 can list topics from NameServer. It does not implement consume-stats, broker-runtime, or cluster-info queries. Those numeric fields are emitted when a stats source provides them, and are left absent otherwise. This plugin does not invent values and does not shell out to `mqadmin`.

## Configuration

```toml @sample.conf
# RocketMQ 4.9.x admin collection. Nameserver protocol matches 4.4 through 4.9.
# RocketMQ 5.x native /metrics is not collected by this plugin.
[[inputs.bklite_rocketmq]]
## NameServer addresses, host:port.
namesrv_addrs = ["127.0.0.1:9876"]

## Optional ACL. Leave empty when the cluster has ACL disabled.
# access_key = ""
# secret_key = ""

## Admin call timeout.
# response_timeout = "10s"

## Glob filters. Empty include means all. Used to bound topic and group cardinality.
# topic_include = []
# topic_exclude = []
# group_include = []
# group_exclude = []
```

## Metrics

| Measurement | Fields | Tags |
|---|---|---|
| `bklite_rocketmq_up` | `up` | `namesrv` |
| `bklite_rocketmq_broker` | `tps`, `qps`, `commitlog_disk_ratio` | `cluster`, `broker`, `broker_ip` |
| `bklite_rocketmq_producer` | `tps`, `message_size`, `offset` | `cluster`, `broker`, `topic` |
| `bklite_rocketmq_consumer` | `tps`, `message_size`, `offset` | `cluster`, `broker`, `group`, `topic` |
| `bklite_rocketmq_group` | `diff`, `ready`, `inflight`, `latency_ms`, `retry_diff`, `dlq` | `cluster`, `group`, `topic` |

`ready` is messages waiting to be consumed. `inflight` is messages pulled but not acked. When 4.x admin returns only one backlog, it is stored in `diff` and `ready` / `inflight` are omitted. A failed collection sets `up` to 0 and does not fail the Telegraf agent.
117 changes: 117 additions & 0 deletions plugins/inputs/bklite_rocketmq/bklite_rocketmq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//go:generate ../../../tools/readme_config_includer/generator
package bklite_rocketmq

import (
"context"
_ "embed"
"errors"
"strings"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/plugins/inputs"
)

//go:embed sample.conf
var sampleConfig string

const defaultResponseTimeout = 10 * time.Second

// RocketMQ collects RocketMQ 4.9.x (admin protocol 4.4–4.9) via NameServer.
// RocketMQ 5.x native Prometheus metrics are a separate pull path.
type RocketMQ struct {
NamesrvAddrs []string `toml:"namesrv_addrs"`
BrokerAddr string `toml:"broker_addr"`
AccessKey string `toml:"access_key"`
SecretKey string `toml:"secret_key"`
ResponseTimeout config.Duration `toml:"response_timeout"`
TopicInclude []string `toml:"topic_include"`
TopicExclude []string `toml:"topic_exclude"`
GroupInclude []string `toml:"group_include"`
GroupExclude []string `toml:"group_exclude"`
Log telegraf.Logger `toml:"-"`

topicFilter filter.Filter
groupFilter filter.Filter
source statsSource
}

type statsSource interface {
Collect(ctx context.Context) (snapshot, error)
}

func (*RocketMQ) SampleConfig() string {
return sampleConfig
}

func (r *RocketMQ) Init() error {
if len(r.NamesrvAddrs) == 0 {
return errors.New("namesrv_addrs is required")
}
if r.ResponseTimeout <= 0 {
r.ResponseTimeout = config.Duration(defaultResponseTimeout)
}
var err error
r.topicFilter, err = filter.NewIncludeExcludeFilter(r.TopicInclude, r.TopicExclude)
if err != nil {
return err
}
r.groupFilter, err = filter.NewIncludeExcludeFilter(r.GroupInclude, r.GroupExclude)
if err != nil {
return err
}
if r.source == nil {
r.source, err = newMQAdmin(r.NamesrvAddrs, r.BrokerAddr, r.AccessKey, r.SecretKey)
if err != nil {
return err
}
}
return nil
}

func (r *RocketMQ) Gather(acc telegraf.Accumulator) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(r.ResponseTimeout))
defer cancel()

snap, err := r.source.Collect(ctx)
if err != nil {
if r.Log != nil {
r.Log.Errorf("rocketmq collect failed: %v", err)
}
acc.AddError(err)
if snap.Namesrv == "" {
acc.AddGauge("bklite_rocketmq_up", map[string]interface{}{"up": int64(0)}, map[string]string{
"namesrv": r.namesrvTag(),
})
return nil
}
}
r.emit(acc, snap)
return nil
}

func (r *RocketMQ) namesrvTag() string {
return strings.Join(r.NamesrvAddrs, ",")
}

func (r *RocketMQ) allowTopic(name string) bool {
if r.topicFilter == nil {
return true
}
return r.topicFilter.Match(name)
}

func (r *RocketMQ) allowGroup(name string) bool {
if r.groupFilter == nil {
return true
}
return r.groupFilter.Match(name)
}

func init() {
inputs.Add("bklite_rocketmq", func() telegraf.Input {
return &RocketMQ{}
})
}
Loading
Loading