Skip to content

Commit

Permalink
Merge pull request #155 from ymtdzzz/feature/prometheus-support
Browse files Browse the repository at this point in the history
Add Prometheus support
  • Loading branch information
ymtdzzz authored Sep 8, 2024
2 parents 93fb103 + 252d1ef commit 99d623a
Show file tree
Hide file tree
Showing 11 changed files with 1,204 additions and 87 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A terminal OpenTelemetry viewer inspired by [otel-desktop-viewer](https://github.com/CtrlSpice/otel-desktop-viewer/tree/main).

This tool currently supports OpenTelemetry and Zipkin formats.
This tool currently supports OpenTelemetry, Zipkin (Traces) and Prometheus (Metrics) formats.

Traces
![Traces](./docs/traces.png)
Expand All @@ -27,12 +27,14 @@ Usage:
otel-tui [flags]
Flags:
--enable-zipkin Enable the zipkin receiver
--grpc int The port number on which we listen for OTLP grpc payloads (default 4317)
-h, --help help for otel-tui
--host string The host where we expose our OTLP endpoints (default "0.0.0.0")
--http int The port number on which we listen for OTLP http payloads (default 4318)
-v, --version version for otel-tui
--enable-prom Enable the prometheus receiver
--enable-zipkin Enable the zipkin receiver
--grpc int The port number on which we listen for OTLP grpc payloads (default 4317)
-h, --help help for otel-tui
--host string The host where we expose our OTLP endpoints (default "0.0.0.0")
--http int The port number on which we listen for OTLP http payloads (default 4318)
--prom-target stringArray The target endpoints for the prometheus receiver (--prom-target "localhost:9000" --prom-target "other-host:9000")
-v, --version version for otel-tui
```

### Homebrew
Expand Down Expand Up @@ -131,7 +133,7 @@ There're a lot of things to do. Here are some of them:
- [ ] Display basic chart of the selected metric
- [x] Gauge
- [x] Sum
- [ ] Histogram
- [x] Histogram
- [ ] ExponentialHistogram
- [ ] Summary
- [ ] Metric list
Expand All @@ -155,7 +157,7 @@ There're a lot of things to do. Here are some of them:
- [x] Data rotation (current buffer size: 1000 service root spans and logs)
- [ ] ...
- Configurations
- [ ] Port
- [x] Port
- [ ] Refresh interval
- [ ] Buffer size
- [ ] ...
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
Expand Up @@ -3,6 +3,7 @@ package main
import (
_ "embed"
"encoding/json"
"errors"
"html/template"
"strings"
)
Expand All @@ -15,6 +16,8 @@ type Config struct {
OTLPHTTPPort int
OTLPGRPCPort int
EnableZipkin bool
EnableProm bool
PromTarget []string
}

func (c *Config) RenderYml() (string, error) {
Expand Down Expand Up @@ -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
Expand Up @@ -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:
Expand All @@ -16,7 +30,9 @@ service:
traces:
receivers:
- otlp
{{if .EnableZipkin}} - zipkin{{end}}
{{- if .EnableZipkin}}
- zipkin
{{- end}}
processors:
exporters:
- tui
Expand All @@ -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"
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -44,6 +59,7 @@ service:
metrics:
receivers:
- otlp
- prometheus
processors:
exporters:
- tui
Expand All @@ -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:
Expand All @@ -68,7 +85,6 @@ receivers:
endpoint: 0.0.0.0:4318
grpc:
endpoint: 0.0.0.0:4317
processors:
exporters:
tui:
Expand All @@ -77,7 +93,6 @@ service:
traces:
receivers:
- otlp
processors:
exporters:
- tui
Expand All @@ -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())
})
}
}
Loading

0 comments on commit 99d623a

Please sign in to comment.