Skip to content

Commit

Permalink
Add Prometheus supporting
Browse files Browse the repository at this point in the history
ymtdzzz committed Sep 8, 2024
1 parent 93fb103 commit 8aa8259
Showing 9 changed files with 1,183 additions and 76 deletions.
2 changes: 2 additions & 0 deletions components.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ package main
import (
_ "embed"
"encoding/json"
"errors"
"html/template"
"strings"
)
@@ -15,6 +16,8 @@ type Config struct {
OTLPHTTPPort int
OTLPGRPCPort int
EnableZipkin bool
EnableProm bool
PromTarget []string
}

func (c *Config) RenderYml() (string, error) {
@@ -51,3 +54,12 @@ func structToMap(s interface{}) (map[string]any, error) {

return result, nil
}

// Validate checks if the otel-tui configuration is valid
func (cfg *Config) Validate() error {
if cfg.EnableProm && len(cfg.PromTarget) == 0 {
return errors.New("the target endpoints for the prometheus receiver (--prom-target) must be specified when prometheus receiver enabled")
}

return nil
}
25 changes: 22 additions & 3 deletions config.yml.tpl
Original file line number Diff line number Diff line change
@@ -6,8 +6,22 @@ receivers:
endpoint: {{ .OTLPHost }}:{{ .OTLPHTTPPort }}
grpc:
endpoint: {{ .OTLPHost }}:{{ .OTLPGRPCPort }}
{{if .EnableZipkin}} zipkin:
endpoint: 0.0.0.0:9411{{end}}
{{- if .EnableZipkin}}
zipkin:
endpoint: 0.0.0.0:9411
{{- end}}
{{- if .EnableProm}}
prometheus:
config:
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 15s
static_configs:
- targets:
{{- range $idx, $target := .PromTarget}}
- '{{ $target -}}'
{{- end}}
{{- end}}
processors:
exporters:
tui:
@@ -16,7 +30,9 @@ service:
traces:
receivers:
- otlp
{{if .EnableZipkin}} - zipkin{{end}}
{{- if .EnableZipkin}}
- zipkin
{{- end}}
processors:
exporters:
- tui
@@ -29,6 +45,9 @@ service:
metrics:
receivers:
- otlp
{{- if .EnableProm}}
- prometheus
{{- end}}
processors:
exporters:
- tui
66 changes: 63 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
@@ -12,6 +13,11 @@ func TestConfigRenderYml(t *testing.T) {
OTLPHTTPPort: 4318,
OTLPGRPCPort: 4317,
EnableZipkin: true,
EnableProm: true,
PromTarget: []string{
"localhost:9000",
"other-host:9000",
},
}
want := `yaml:
receivers:
@@ -23,6 +29,15 @@ receivers:
endpoint: 0.0.0.0:4317
zipkin:
endpoint: 0.0.0.0:9411
prometheus:
config:
scrape_configs:
- job_name: 'prometheus'
scrape_interval: 15s
static_configs:
- targets:
- 'localhost:9000'
- 'other-host:9000'
processors:
exporters:
tui:
@@ -44,6 +59,7 @@ service:
metrics:
receivers:
- otlp
- prometheus
processors:
exporters:
- tui
@@ -53,12 +69,13 @@ service:
assert.Equal(t, want, got)
}

func TestConfigRenderYmlZipkinDisabled(t *testing.T) {
func TestConfigRenderYmlMinimum(t *testing.T) {
cfg := &Config{
OTLPHost: "0.0.0.0",
OTLPHTTPPort: 4318,
OTLPGRPCPort: 4317,
EnableZipkin: false,
EnableProm: false,
}
want := `yaml:
receivers:
@@ -68,7 +85,6 @@ receivers:
endpoint: 0.0.0.0:4318
grpc:
endpoint: 0.0.0.0:4317
processors:
exporters:
tui:
@@ -77,7 +93,6 @@ service:
traces:
receivers:
- otlp
processors:
exporters:
- tui
@@ -98,3 +113,48 @@ service:
assert.Nil(t, err)
assert.Equal(t, want, got)
}

func TestValidate(t *testing.T) {
tests := []struct {
name string
cfg *Config
want error
}{
{
name: "OK_Minimum",
cfg: &Config{},
want: nil,
},
{
name: "OK_Maximum",
cfg: &Config{
OTLPHost: "0.0.0.0",
OTLPHTTPPort: 4318,
OTLPGRPCPort: 4317,
EnableZipkin: true,
EnableProm: true,
PromTarget: []string{
"localhost:9000",
"other-host:9000",
},
},
want: nil,
},
{
name: "NG_Prom",
cfg: &Config{
OTLPHost: "0.0.0.0",
OTLPHTTPPort: 4318,
OTLPGRPCPort: 4317,
EnableProm: true,
},
want: errors.New("The target endpoints for the prometheus receiver (--prom-target) must be specified when prometheus receiver enabled"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.cfg.Validate())
})
}
}
115 changes: 111 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ module github.com/ymtdzzz/otel-tui
go 1.22.1

require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.108.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver v0.108.0
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
@@ -21,64 +22,150 @@ require (
go.opentelemetry.io/collector/receiver v0.108.1
go.opentelemetry.io/collector/receiver/otlpreceiver v0.108.1
golang.org/x/sys v0.24.0
google.golang.org/genproto v0.0.0-20240617180043-68d350f18fd4
google.golang.org/genproto v0.0.0-20240708141625-4ad9e859172b
)

require (
cloud.google.com/go/auth v0.7.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
cloud.google.com/go/compute/metadata v0.4.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4 v4.3.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/Code-Hex/go-generics-cache v1.5.1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/alecthomas/units v0.0.0-20240626203959-61d1e3462e30 // indirect
github.com/apache/thrift v0.20.0 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go v1.54.19 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cncf/xds/go v0.0.0-20240423153145-555b57ec207b // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dennwc/varint v1.0.0 // indirect
github.com/digitalocean/godo v1.118.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v27.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/envoyproxy/go-control-plane v0.12.0 // indirect
github.com/envoyproxy/protoc-gen-validate v1.0.4 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.7.4 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/swag v0.22.9 // indirect
github.com/go-resty/resty/v2 v2.13.1 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/go-zookeeper/zk v1.0.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.5 // indirect
github.com/gophercloud/gophercloud v1.13.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grafana/regexp v0.0.0-20240518133315-a468a5bfb3bc // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 // indirect
github.com/hashicorp/consul/api v1.29.2 // indirect
github.com/hashicorp/cronexpr v1.1.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/nomad/api v0.0.0-20240717122358-3d93bd3778f3 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/hetznercloud/hcloud-go/v2 v2.10.2 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/ionos-cloud/sdk-go/v6 v6.1.11 // indirect
github.com/jaegertracing/jaeger v1.60.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/jpillora/backoff v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.1.1 // indirect
github.com/kolo/xmlrpc v0.0.0-20220921171641-a4b6fa1dd06b // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/linode/linodego v1.37.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lufia/plan9stats v0.0.0-20220913051719-115f729f3c8c // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/miekg/dns v1.1.61 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.2.3 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
github.com/navidys/tvxwidgets v0.7.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.108.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.108.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.108.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheus v0.108.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/zipkin v0.108.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/openzipkin/zipkin-go v0.4.3 // indirect
github.com/ovh/go-ovh v1.6.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20220216144756-c35f1ee13d7c // indirect
github.com/prometheus/client_golang v1.20.1 // indirect
github.com/prometheus/client_golang v1.20.2 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/common/sigv4 v0.1.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/prometheus/prometheus v0.54.1 // indirect
github.com/rivo/tview v0.0.0-20240616192244-23476fa0bab2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rs/cors v1.11.0 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.29 // indirect
github.com/shirou/gopsutil/v4 v4.24.7 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/vultr/govultr/v2 v2.17.2 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector v0.108.1 // indirect
go.opentelemetry.io/collector/client v1.14.1 // indirect
go.opentelemetry.io/collector/component/componentprofiles v0.108.1 // indirect
@@ -125,22 +212,42 @@ require (
go.opentelemetry.io/otel/sdk/metric v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.design/x/clipboard v0.7.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/exp/shiny v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/image v0.14.0 // indirect
golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
gonum.org/v1/gonum v0.15.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
google.golang.org/api v0.188.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240708141625-4ad9e859172b // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.29.3 // indirect
k8s.io/apimachinery v0.29.3 // indirect
k8s.io/client-go v0.29.3 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/ymtdzzz/otel-tui/tuiexporter => ./tuiexporter
808 changes: 794 additions & 14 deletions go.sum

Large diffs are not rendered by default.

211 changes: 163 additions & 48 deletions go.work.sum

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -41,9 +41,13 @@ func runInteractive(params otelcol.CollectorSettings) error {
}

func newCommand(params otelcol.CollectorSettings) *cobra.Command {
var httpPortFlag, grpcPortFlag int
var hostFlag string
var zipkinEnabledFlag bool
var (
httpPortFlag, grpcPortFlag int
hostFlag string
zipkinEnabledFlag bool
promEnabledFlag bool
promTargetFlag []string
)

rootCmd := &cobra.Command{
Use: params.BuildInfo.Command,
@@ -55,6 +59,12 @@ func newCommand(params otelcol.CollectorSettings) *cobra.Command {
OTLPHTTPPort: httpPortFlag,
OTLPGRPCPort: grpcPortFlag,
EnableZipkin: zipkinEnabledFlag,
EnableProm: promEnabledFlag,
PromTarget: promTargetFlag,
}

if err := cfg.Validate(); err != nil {
return err
}

configContents, err := cfg.RenderYml()
@@ -83,5 +93,7 @@ func newCommand(params otelcol.CollectorSettings) *cobra.Command {
rootCmd.Flags().IntVar(&grpcPortFlag, "grpc", 4317, "The port number on which we listen for OTLP grpc payloads")
rootCmd.Flags().StringVar(&hostFlag, "host", "0.0.0.0", "The host where we expose our OTLP endpoints")
rootCmd.Flags().BoolVar(&zipkinEnabledFlag, "enable-zipkin", false, "Enable the zipkin receiver")
rootCmd.Flags().BoolVar(&promEnabledFlag, "enable-prom", false, "Enable the prometheus receiver")
rootCmd.Flags().StringArrayVar(&promTargetFlag, "prom-target", []string{}, `The target endpoints for the prometheus receiver (--prom-target "localhost:9000" --prom-target "other-host:9000")`)
return rootCmd
}
2 changes: 1 addition & 1 deletion tuiexporter/internal/telemetry/store.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ import (

const (
MAX_SERVICE_SPAN_COUNT = 1000
MAX_METRIC_COUNT = 1000
MAX_METRIC_COUNT = 3000
MAX_LOG_COUNT = 1000
)

0 comments on commit 8aa8259

Please sign in to comment.