Skip to content

Commit

Permalink
Add Prometheus supporting
Browse files Browse the repository at this point in the history
  • Loading branch information
ymtdzzz committed Sep 8, 2024
1 parent 93fb103 commit 8aa8259
Show file tree
Hide file tree
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
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 8aa8259

Please sign in to comment.