Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support setting the log level through the environment variable #9553

Merged
merged 1 commit into from
Mar 12, 2018
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
31 changes: 14 additions & 17 deletions cmd/influxd/run/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run

import (
"encoding"
"fmt"
"io/ioutil"
"log"
Expand All @@ -11,7 +12,6 @@ import (
"regexp"
"strconv"
"strings"
"time"

"github.com/BurntSushi/toml"
"github.com/influxdata/influxdb/coordinator"
Expand Down Expand Up @@ -202,8 +202,17 @@ func (c *Config) ApplyEnvOverrides(getenv func(string) string) error {
}

func (c *Config) applyEnvOverrides(getenv func(string) string, prefix string, spec reflect.Value, structKey string) error {
// If we have a pointer, dereference it
element := spec
// If spec is a named type and is addressable,
// check the address to see if it implements encoding.TextUnmarshaler.
if spec.Kind() != reflect.Ptr && spec.Type().Name() != "" && spec.CanAddr() {
v := spec.Addr()
if u, ok := v.Interface().(encoding.TextUnmarshaler); ok {
value := getenv(prefix)
return u.UnmarshalText([]byte(value))
}
}
// If we have a pointer, dereference it
if spec.Kind() == reflect.Ptr {
element = spec.Elem()
}
Expand All @@ -217,21 +226,9 @@ func (c *Config) applyEnvOverrides(getenv func(string) string, prefix string, sp
}
element.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var intValue int64

// Handle toml.Duration
if element.Type().Name() == "Duration" {
dur, err := time.ParseDuration(value)
if err != nil {
return fmt.Errorf("failed to apply %v to %v using type %v and value '%v'", prefix, structKey, element.Type().String(), value)
}
intValue = dur.Nanoseconds()
} else {
var err error
intValue, err = strconv.ParseInt(value, 0, element.Type().Bits())
if err != nil {
return fmt.Errorf("failed to apply %v to %v using type %v and value '%v'", prefix, structKey, element.Type().String(), value)
}
intValue, err := strconv.ParseInt(value, 0, element.Type().Bits())
if err != nil {
return fmt.Errorf("failed to apply %v to %v using type %v and value '%v'", prefix, structKey, element.Type().String(), value)
}
element.SetInt(intValue)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
Expand Down
75 changes: 41 additions & 34 deletions cmd/influxd/run/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"io/ioutil"
"os"
"testing"
"time"

"github.com/BurntSushi/toml"
"github.com/influxdata/influxdb/cmd/influxd/run"
influxtoml "github.com/influxdata/influxdb/toml"
"go.uber.org/zap/zapcore"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
Expand Down Expand Up @@ -151,40 +154,36 @@ enabled = true
t.Fatal(err)
}

if err := os.Setenv("INFLUXDB_UDP_BIND_ADDRESS", ":1234"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}

if err := os.Setenv("INFLUXDB_UDP_0_BIND_ADDRESS", ":5555"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}

if err := os.Setenv("INFLUXDB_GRAPHITE_0_TEMPLATES_0", "overide.* .template.0"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}

if err := os.Setenv("INFLUXDB_GRAPHITE_1_TEMPLATES", "overide.* .template.1.1,overide.* .template.1.2"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}

if err := os.Setenv("INFLUXDB_GRAPHITE_1_PROTOCOL", "udp"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}

if err := os.Setenv("INFLUXDB_COLLECTD_1_BIND_ADDRESS", ":1020"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}

if err := os.Setenv("INFLUXDB_OPENTSDB_0_BIND_ADDRESS", ":2020"); err != nil {
t.Fatalf("failed to set env var: %v", err)
}

// uint64 type
if err := os.Setenv("INFLUXDB_DATA_CACHE_MAX_MEMORY_SIZE", "1000"); err != nil {
t.Fatalf("failed to set env var: %v", err)
getenv := func(s string) string {
switch s {
case "INFLUXDB_UDP_BIND_ADDRESS":
return ":1234"
case "INFLUXDB_UDP_0_BIND_ADDRESS":
return ":5555"
case "INFLUXDB_GRAPHITE_0_TEMPLATES_0":
return "override.* .template.0"
case "INFLUXDB_GRAPHITE_1_TEMPLATES":
return "override.* .template.1.1,override.* .template.1.2"
case "INFLUXDB_GRAPHITE_1_PROTOCOL":
return "udp"
case "INFLUXDB_COLLECTD_1_BIND_ADDRESS":
return ":1020"
case "INFLUXDB_OPENTSDB_0_BIND_ADDRESS":
return ":2020"
case "INFLUXDB_DATA_CACHE_MAX_MEMORY_SIZE":
// uint64 type
return "1000"
case "INFLUXDB_LOGGING_LEVEL":
// logging type
return "warn"
case "INFLUXDB_COORDINATOR_QUERY_TIMEOUT":
// duration type
return "1m"
}
return ""
}

if err := c.ApplyEnvOverrides(os.Getenv); err != nil {
if err := c.ApplyEnvOverrides(getenv); err != nil {
t.Fatalf("failed to apply env overrides: %v", err)
}

Expand All @@ -196,11 +195,11 @@ enabled = true
t.Fatalf("unexpected udp bind address: %s", c.UDPInputs[1].BindAddress)
}

if len(c.GraphiteInputs[0].Templates) != 1 || c.GraphiteInputs[0].Templates[0] != "overide.* .template.0" {
if len(c.GraphiteInputs[0].Templates) != 1 || c.GraphiteInputs[0].Templates[0] != "override.* .template.0" {
t.Fatalf("unexpected graphite 0 templates: %+v", c.GraphiteInputs[0].Templates)
}

if len(c.GraphiteInputs[1].Templates) != 2 || c.GraphiteInputs[1].Templates[1] != "overide.* .template.1.2" {
if len(c.GraphiteInputs[1].Templates) != 2 || c.GraphiteInputs[1].Templates[1] != "override.* .template.1.2" {
t.Fatalf("unexpected graphite 1 templates: %+v", c.GraphiteInputs[1].Templates)
}

Expand All @@ -219,6 +218,14 @@ enabled = true
if c.Data.CacheMaxMemorySize != 1000 {
t.Fatalf("unexpected cache max memory size: %v", c.Data.CacheMaxMemorySize)
}

if c.Logging.Level != zapcore.WarnLevel {
t.Fatalf("unexpected logging level: %v", c.Logging.Level)
}

if c.Coordinator.QueryTimeout != influxtoml.Duration(time.Minute) {
t.Fatalf("unexpected query timeout: %v", c.Coordinator.QueryTimeout)
}
}

func TestConfig_ValidateNoServiceConfigured(t *testing.T) {
Expand Down