Skip to content

Commit

Permalink
Merge branch 'main' into cloud
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored May 1, 2024
2 parents 076b7c1 + 2868dd3 commit 1669d7d
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 10 deletions.
6 changes: 3 additions & 3 deletions config/flipt.schema.cue
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ import "strings"
}

#tracing: {
enabled?: bool | *false
exporter?: *"jaeger" | "zipkin" | "otlp"
samplingRatio?: float & >=0 & <=1 | *1
enabled?: bool | *false
exporter?: *"jaeger" | "zipkin" | "otlp"
sampling_ratio?: float & >=0 & <=1 | *1
propagators?: [
..."tracecontext" | "baggage" | "b3" | "b3multi" | "jaeger" | "xray" | "ottrace" | "none",
] | *["tracecontext", "baggage"]
Expand Down
2 changes: 1 addition & 1 deletion config/flipt.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@
"enum": ["jaeger", "zipkin", "otlp"],
"default": "jaeger"
},
"samplingRatio": {
"sampling_ratio": {
"type": "number",
"default": 1,
"minimum": 0,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ require (
github.com/hashicorp/cap v0.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/iancoleman/strcase v0.3.0
github.com/jackc/pgx/v5 v5.5.5
github.com/libsql/libsql-client-go v0.0.0-20230917132930-48c310b27e7b
github.com/magefile/mage v1.15.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
Expand Down
93 changes: 93 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"testing"
"time"

"github.com/iancoleman/strcase"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -1502,3 +1503,95 @@ func TestGetConfigFile(t *testing.T) {
})
}
}

var (
// add any struct tags to match their camelCase equivalents here.
camelCaseMatchers = map[string]string{
"requireTLS": "requireTLS",
"discoveryURL": "discoveryURL",
}
)

func TestStructTags(t *testing.T) {
configType := reflect.TypeOf(Config{})
configTags := getStructTags(configType)

for k, v := range camelCaseMatchers {
strcase.ConfigureAcronym(k, v)
}

// Validate the struct tags for the Config struct.
// recursively validate the struct tags for all sub-structs.
validateStructTags(t, configTags, configType)
}

func validateStructTags(t *testing.T, tags map[string]map[string]string, tType reflect.Type) {
tName := tType.Name()
for fieldName, fieldTags := range tags {
fieldType, ok := tType.FieldByName(fieldName)
require.True(t, ok, "field %s not found in type %s", fieldName, tName)

// Validate the `json` struct tag.
jsonTag, ok := fieldTags["json"]
if ok {
require.True(t, isCamelCase(jsonTag), "json tag for field '%s.%s' should be camelCase but is '%s'", tName, fieldName, jsonTag)
}

// Validate the `mapstructure` struct tag.
mapstructureTag, ok := fieldTags["mapstructure"]
if ok {
require.True(t, isSnakeCase(mapstructureTag), "mapstructure tag for field '%s.%s' should be snake_case but is '%s'", tName, fieldName, mapstructureTag)
}

// Validate the `yaml` struct tag.
yamlTag, ok := fieldTags["yaml"]
if ok {
require.True(t, isSnakeCase(yamlTag), "yaml tag for field '%s.%s' should be snake_case but is '%s'", tName, fieldName, yamlTag)
}

// recursively validate the struct tags for all sub-structs.
if fieldType.Type.Kind() == reflect.Struct {
validateStructTags(t, getStructTags(fieldType.Type), fieldType.Type)
}
}
}

func isCamelCase(s string) bool {
return s == strcase.ToLowerCamel(s)
}

func isSnakeCase(s string) bool {
return s == strcase.ToSnake(s)
}

func getStructTags(t reflect.Type) map[string]map[string]string {
tags := make(map[string]map[string]string)

for i := 0; i < t.NumField(); i++ {
field := t.Field(i)

// Get the field name.
fieldName := field.Name

// Get the field tags.
fieldTags := make(map[string]string)
for _, tag := range []string{"json", "mapstructure", "yaml"} {
tagValue := field.Tag.Get(tag)
if tagValue == "-" {
fieldTags[tag] = "skip"
continue
}
values := strings.Split(tagValue, ",")
if len(values) > 1 {
tagValue = values[0]
}
if tagValue != "" {
fieldTags[tag] = tagValue
}
}

tags[fieldName] = fieldTags
}

return tags
}
2 changes: 1 addition & 1 deletion internal/config/testdata/tracing/otlp.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tracing:
enabled: true
exporter: otlp
samplingRatio: 0.5
sampling_ratio: 0.5
otlp:
endpoint: http://localhost:9999
headers:
Expand Down
2 changes: 1 addition & 1 deletion internal/config/testdata/tracing/wrong_sampling_ratio.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tracing:
enabled: true
samplingRatio: 1.1
sampling_ratio: 1.1
8 changes: 4 additions & 4 deletions internal/config/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ type TracingConfig struct {
Enabled bool `json:"enabled" mapstructure:"enabled" yaml:"enabled"`
Exporter TracingExporter `json:"exporter,omitempty" mapstructure:"exporter" yaml:"exporter,omitempty"`
Propagators []TracingPropagator `json:"propagators,omitempty" mapstructure:"propagators" yaml:"propagators,omitempty"`
SamplingRatio float64 `json:"samplingRatio,omitempty" mapstructure:"samplingRatio" yaml:"samplingRatio,omitempty"`
SamplingRatio float64 `json:"samplingRatio,omitempty" mapstructure:"sampling_ratio" yaml:"sampling_ratio,omitempty"`
Jaeger JaegerTracingConfig `json:"jaeger,omitempty" mapstructure:"jaeger" yaml:"jaeger,omitempty"`
Zipkin ZipkinTracingConfig `json:"zipkin,omitempty" mapstructure:"zipkin" yaml:"zipkin,omitempty"`
OTLP OTLPTracingConfig `json:"otlp,omitempty" mapstructure:"otlp" yaml:"otlp,omitempty"`
}

func (c *TracingConfig) setDefaults(v *viper.Viper) error {
v.SetDefault("tracing", map[string]any{
"enabled": false,
"exporter": TracingJaeger,
"samplingRatio": 1,
"enabled": false,
"exporter": TracingJaeger,
"sampling_ratio": 1,
"propagators": []TracingPropagator{
TracingPropagatorTraceContext,
TracingPropagatorBaggage,
Expand Down

0 comments on commit 1669d7d

Please sign in to comment.