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

refactor: look up default values on struct tag #996

Closed
wants to merge 12 commits into from
34 changes: 17 additions & 17 deletions app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,23 @@ func newStartedApp(
enableHostMetadata bool,
) (*App, inject.Graph) {
c := &config.MockConfig{
GetSendDelayVal: 0,
GetTraceTimeoutVal: 10 * time.Millisecond,
GetMaxBatchSizeVal: 500,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
PeerManagementType: "file",
GetUpstreamBufferSizeVal: 10000,
GetPeerBufferSizeVal: 10000,
GetListenAddrVal: "127.0.0.1:" + strconv.Itoa(basePort),
GetPeerListenAddrVal: "127.0.0.1:" + strconv.Itoa(basePort+1),
IsAPIKeyValidFunc: func(k string) bool { return k == legacyAPIKey || k == nonLegacyAPIKey },
GetHoneycombAPIVal: "http://api.honeycomb.io",
GetCollectionConfigVal: config.CollectionConfig{CacheCapacity: 10000},
AddHostMetadataToTrace: enableHostMetadata,
TraceIdFieldNames: []string{"trace.trace_id"},
ParentIdFieldNames: []string{"trace.parent_id"},
SampleCache: config.SampleCacheConfig{KeptSize: 10000, DroppedSize: 100000, SizeCheckInterval: config.Duration(10 * time.Second)},
GetSendDelayVal: 0,
GetTraceTimeoutVal: 10 * time.Millisecond,
GetMaxBatchSizeVal: 500,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
PeerManagementType: "file",
GetUpstreamBufferSizeVal: 10000,
GetPeerBufferSizeVal: 10000,
GetListenAddrVal: "127.0.0.1:" + strconv.Itoa(basePort),
GetPeerListenAddrVal: "127.0.0.1:" + strconv.Itoa(basePort+1),
IsAPIKeyValidFunc: func(k string) bool { return k == legacyAPIKey || k == nonLegacyAPIKey },
GetHoneycombAPIVal: "http://api.honeycomb.io",
GetCollectionConfigVal: config.CollectionConfig{CacheCapacity: 10000},
AddHostMetadataToTraceVal: enableHostMetadata,
TraceIdFieldNames: []string{"trace.trace_id"},
ParentIdFieldNames: []string{"trace.parent_id"},
SampleCache: config.SampleCacheConfig{KeptSize: 10000, DroppedSize: 100000, SizeCheckInterval: config.Duration(10 * time.Second)},
}

var err error
Expand Down
8 changes: 7 additions & 1 deletion cmd/refinery/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ func main() {
os.Exit(1)
}

compressPeerCommunication, err := c.GetCompressPeerCommunication()

if err != nil {
fmt.Printf("unable to set GetCompressPeerCommunication")
os.Exit(1)
}
peerClient, err := libhoney.NewClient(libhoney.ClientConfig{
Transmission: &transmission.Honeycomb{
MaxBatchSize: c.GetMaxBatchSize(),
Expand All @@ -175,7 +181,7 @@ func main() {
PendingWorkCapacity: uint(c.GetPeerBufferSize()),
UserAgentAddition: userAgentAddition,
Transport: peerTransport,
DisableCompression: !c.GetCompressPeerCommunication(),
DisableCompression: !compressPeerCommunication,
EnableMsgpackEncoding: true,
Metrics: peerMetricsRecorder,
},
Expand Down
18 changes: 14 additions & 4 deletions collect/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ func (i *InMemCollector) Start() error {
i.reload = make(chan struct{}, 1)
i.datasetSamplers = make(map[string]sample.Sampler)

if i.Config.GetAddHostMetadataToTrace() {
addHostMetadataToTrace, err := i.Config.GetAddHostMetadataToTrace()
if err != nil {
return err
}

if addHostMetadataToTrace {
if hostname, err := os.Hostname(); err == nil && hostname != "" {
i.hostname = hostname
}
Expand Down Expand Up @@ -529,12 +534,14 @@ func (i *InMemCollector) dealWithSentTrace(tr cache.TraceSentRecord, sentReason
mergeTraceAndSpanSampleRates(sp, tr.Rate(), isDryRun)
// if this span is a late root span, possibly update it with our current span count
if i.isRootSpan(sp) {
addSpanCountToRoot, _ := i.Config.GetAddSpanCountToRoot()

if i.Config.GetAddCountsToRoot() {
sp.Data["meta.span_event_count"] = int64(tr.SpanEventCount())
sp.Data["meta.span_link_count"] = int64(tr.SpanLinkCount())
sp.Data["meta.span_count"] = int64(tr.SpanCount())
sp.Data["meta.event_count"] = int64(tr.DescendantCount())
} else if i.Config.GetAddSpanCountToRoot() {
} else if addSpanCountToRoot {
sp.Data["meta.span_count"] = int64(tr.DescendantCount())
}

Expand Down Expand Up @@ -623,12 +630,14 @@ func (i *InMemCollector) send(trace *types.Trace, sendReason string) {
// If we have a root span, update it with the count before determining the SampleRate.
if trace.RootSpan != nil {
rs := trace.RootSpan
addSpanCountToRoot, _ := i.Config.GetAddSpanCountToRoot()

if i.Config.GetAddCountsToRoot() {
rs.Data["meta.span_event_count"] = int64(trace.SpanEventCount())
rs.Data["meta.span_link_count"] = int64(trace.SpanLinkCount())
rs.Data["meta.span_count"] = int64(trace.SpanCount())
rs.Data["meta.event_count"] = int64(trace.DescendantCount())
} else if i.Config.GetAddSpanCountToRoot() {
} else if addSpanCountToRoot {
rs.Data["meta.span_count"] = int64(trace.DescendantCount())
}
}
Expand Down Expand Up @@ -679,12 +688,13 @@ func (i *InMemCollector) send(trace *types.Trace, sendReason string) {
// update the root span (if we have one, which we might not if the trace timed out)
// with the final total as of our send time
if i.isRootSpan(sp) {
addSpanCountToRoot, _ := i.Config.GetAddSpanCountToRoot()
if i.Config.GetAddCountsToRoot() {
sp.Data["meta.span_event_count"] = int64(trace.SpanEventCount())
sp.Data["meta.span_link_count"] = int64(trace.SpanLinkCount())
sp.Data["meta.span_count"] = int64(trace.SpanCount())
sp.Data["meta.event_count"] = int64(trace.DescendantCount())
} else if i.Config.GetAddSpanCountToRoot() {
} else if addSpanCountToRoot {
sp.Data["meta.span_count"] = int64(trace.DescendantCount())
}
}
Expand Down
56 changes: 28 additions & 28 deletions collect/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,13 @@ func TestDependencyInjection(t *testing.T) {
// This test also makes sure that AddCountsToRoot overrides the AddSpanCountToRoot config.
func TestAddCountsToRoot(t *testing.T) {
conf := &config.MockConfig{
GetSendDelayVal: 0,
GetTraceTimeoutVal: 60 * time.Second,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
AddSpanCountToRoot: true,
AddCountsToRoot: true,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
GetSendDelayVal: 0,
GetTraceTimeoutVal: 60 * time.Second,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
AddSpanCountToRootVal: true,
AddCountsToRoot: true,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
}

transmission := &transmit.MockTransmission{}
Expand Down Expand Up @@ -840,14 +840,14 @@ func TestAddCountsToRoot(t *testing.T) {
// even if the trace had already been sent
func TestLateRootGetsCounts(t *testing.T) {
conf := &config.MockConfig{
GetSendDelayVal: 0,
GetTraceTimeoutVal: 5 * time.Millisecond,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
AddSpanCountToRoot: true,
AddCountsToRoot: true,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
AddRuleReasonToTrace: true,
GetSendDelayVal: 0,
GetTraceTimeoutVal: 5 * time.Millisecond,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
AddSpanCountToRootVal: true,
AddCountsToRoot: true,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
AddRuleReasonToTrace: true,
}

transmission := &transmit.MockTransmission{}
Expand Down Expand Up @@ -923,12 +923,12 @@ func TestLateRootGetsCounts(t *testing.T) {
// the cache and that that trace gets span count added to it
func TestAddSpanCount(t *testing.T) {
conf := &config.MockConfig{
GetSendDelayVal: 0,
GetTraceTimeoutVal: 60 * time.Second,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
AddSpanCountToRoot: true,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
GetSendDelayVal: 0,
GetTraceTimeoutVal: 60 * time.Second,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
AddSpanCountToRootVal: true,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
}
transmission := &transmit.MockTransmission{}
transmission.Start()
Expand Down Expand Up @@ -985,13 +985,13 @@ func TestAddSpanCount(t *testing.T) {
// even if the trace had already been sent
func TestLateRootGetsSpanCount(t *testing.T) {
conf := &config.MockConfig{
GetSendDelayVal: 0,
GetTraceTimeoutVal: 5 * time.Millisecond,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
AddSpanCountToRoot: true,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
AddRuleReasonToTrace: true,
GetSendDelayVal: 0,
GetTraceTimeoutVal: 5 * time.Millisecond,
GetSamplerTypeVal: &config.DeterministicSamplerConfig{SampleRate: 1},
SendTickerVal: 2 * time.Millisecond,
AddSpanCountToRootVal: true,
ParentIdFieldNames: []string{"trace.parent_id", "parentId"},
AddRuleReasonToTrace: true,
}
transmission := &transmit.MockTransmission{}
transmission.Start()
Expand Down
8 changes: 4 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type Config interface {

// GetCompressPeerCommunication will be true if refinery should compress
// data before forwarding it to a peer.
GetCompressPeerCommunication() bool
GetCompressPeerCommunication() (compressPeerCommunication bool, err error)

// GetGRPCEnabled returns or not the GRPC server is enabled.
GetGRPCEnabled() bool
GetGRPCEnabled() (enabled bool, err error)

// GetGRPCListenAddr returns the address and port on which to listen for
// incoming events over gRPC
Expand Down Expand Up @@ -159,7 +159,7 @@ type Config interface {

GetIsDryRun() bool

GetAddHostMetadataToTrace() bool
GetAddHostMetadataToTrace() (addHostMetadataToTrace bool, err error)

GetAddRuleReasonToTrace() bool

Expand All @@ -174,7 +174,7 @@ type Config interface {

GetAdditionalErrorFields() []string

GetAddSpanCountToRoot() bool
GetAddSpanCountToRoot() (addSpanCountToRoot bool, err error)

GetAddCountsToRoot() bool

Expand Down
47 changes: 35 additions & 12 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,11 @@ func TestReadDefaults(t *testing.T) {
t.Error("received", d, "expected", false)
}

if d := c.GetAddHostMetadataToTrace(); d != true {
t.Error("received", d, "expected", true)
a, err := c.GetAddHostMetadataToTrace()
if a != true {
t.Error("received", a, "expected", true)
}
assert.NoError(t, err)

if d := c.GetEnvironmentCacheTTL(); d != time.Hour {
t.Error("received", d, "expected", time.Hour)
Expand Down Expand Up @@ -616,7 +618,9 @@ func TestHoneycombLoggerConfig(t *testing.T) {
assert.Equal(t, "http://honeycomb.io", loggerConfig.APIHost)
assert.Equal(t, "1234", loggerConfig.APIKey)
assert.Equal(t, "loggerDataset", loggerConfig.Dataset)
assert.Equal(t, true, loggerConfig.GetSamplerEnabled())
samplerEnabled, err := loggerConfig.GetSamplerEnabled()
assert.NoError(t, err)
assert.Equal(t, true, samplerEnabled)
assert.Equal(t, 5, loggerConfig.SamplerThroughput)
}

Expand All @@ -638,8 +642,8 @@ func TestHoneycombLoggerConfigDefaults(t *testing.T) {
loggerConfig, err := c.GetHoneycombLoggerConfig()

assert.NoError(t, err)

assert.Equal(t, true, loggerConfig.GetSamplerEnabled())
samplerEnabled, err := loggerConfig.GetSamplerEnabled()
assert.Equal(t, true, samplerEnabled)
assert.Equal(t, 10, loggerConfig.SamplerThroughput)
}

Expand All @@ -656,7 +660,9 @@ func TestHoneycombGRPCConfigDefaults(t *testing.T) {
c, err := getConfig([]string{"--no-validate", "--config", config, "--rules_config", rules})
assert.NoError(t, err)

assert.Equal(t, true, c.GetGRPCEnabled())
enabled, err := c.GetGRPCEnabled()
assert.Equal(t, true, enabled)
assert.NoError(t, err)

a, err := c.GetGRPCListenAddr()
assert.NoError(t, err)
Expand Down Expand Up @@ -773,7 +779,9 @@ func TestGRPCServerParameters(t *testing.T) {
assert.Equal(t, 3*time.Minute, time.Duration(gc.MaxConnectionAgeGrace))
assert.Equal(t, 4*time.Minute, time.Duration(gc.KeepAlive))
assert.Equal(t, 5*time.Minute, time.Duration(gc.KeepAliveTimeout))
assert.Equal(t, true, c.GetGRPCEnabled())
enabled, err := c.GetGRPCEnabled()
assert.Equal(t, true, enabled)
assert.NoError(t, err)
addr, err := c.GetGRPCListenAddr()
assert.NoError(t, err)
assert.Equal(t, "localhost:4317", addr)
Expand Down Expand Up @@ -907,13 +915,28 @@ func TestOverrideConfigDefaults(t *testing.T) {
c, err := getConfig([]string{"--no-validate", "--config", config, "--rules_config", rules})
assert.NoError(t, err)

assert.Equal(t, false, c.GetAddSpanCountToRoot())
assert.Equal(t, false, c.GetAddHostMetadataToTrace())
addSpanCountToRoot, err := c.GetAddSpanCountToRoot()
assert.Equal(t, false, addSpanCountToRoot)
assert.NoError(t, err)

addHostMetadataToTrace, err := c.GetAddHostMetadataToTrace()
assert.Equal(t, false, addHostMetadataToTrace)
assert.NoError(t, err)

loggerConfig, err := c.GetHoneycombLoggerConfig()
assert.NoError(t, err)
assert.Equal(t, false, loggerConfig.GetSamplerEnabled())
assert.Equal(t, false, c.GetCompressPeerCommunication())
assert.Equal(t, false, c.GetGRPCEnabled())

samplerEnabled, err := loggerConfig.GetSamplerEnabled()
assert.Equal(t, false, samplerEnabled)
assert.NoError(t, err)

compressPeerCommunication, err := c.GetCompressPeerCommunication()
assert.Equal(t, false, compressPeerCommunication)
assert.NoError(t, err)

enabled, err := c.GetGRPCEnabled()
assert.Equal(t, false, enabled)
assert.NoError(t, err)
}

func TestMemorySizeUnmarshal(t *testing.T) {
Expand Down
Loading
Loading