Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #67 from newrelic/fix-lint-issues
Browse files Browse the repository at this point in the history
Fix lint issues.
  • Loading branch information
a-feld authored Jul 1, 2021
2 parents b899b10 + 3e4c464 commit fdddaef
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 39 deletions.
2 changes: 1 addition & 1 deletion telemetry/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (ca *commonAttributes) WriteDataEntry(buf *bytes.Buffer) *bytes.Buffer {
// keys were invalid. If a marshalling error occurs, nil commonAttributes and an error
// will be returned.
func newCommonAttributes(attributes map[string]interface{}) (*commonAttributes, error) {
if attributes == nil || len(attributes) == 0 {
if len(attributes) == 0 {
return nil, nil
}
response := commonAttributes{}
Expand Down
4 changes: 2 additions & 2 deletions telemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ func (cfg *Config) logURL() string {
// https://github.com/newrelic/newrelic-telemetry-sdk-specs/blob/master/communication.md#user-agent
func (cfg *Config) userAgent() string {
agent := ""
if "" != cfg.Product {
if cfg.Product != "" {
agent += cfg.Product
if "" != cfg.ProductVersion {
if cfg.ProductVersion != "" {
agent += "/" + cfg.ProductVersion
}
}
Expand Down
2 changes: 1 addition & 1 deletion telemetry/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestConfigHarvestPeriod(t *testing.T) {
if nil == h || err != nil {
t.Fatal(h, err)
}
if 0 != h.config.HarvestPeriod {
if h.config.HarvestPeriod != 0 {
t.Error("config func does not set harvest period correctly")
}
}
Expand Down
29 changes: 20 additions & 9 deletions telemetry/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) {
WithEndpoint(spanURL.Host),
WithUserAgent(h.config.userAgent()),
)
if err != nil {
return nil, err
}

metricURL, err := url.Parse(h.config.metricURL())
if nil != err {
Expand All @@ -105,6 +108,9 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) {
WithEndpoint(metricURL.Host),
WithUserAgent(h.config.userAgent()),
)
if err != nil {
return nil, err
}

eventURL, err := url.Parse(h.config.eventURL())
if nil != err {
Expand All @@ -117,9 +123,12 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) {
WithEndpoint(eventURL.Host),
WithUserAgent(h.config.userAgent()),
)
if err != nil {
return nil, err
}

logURL, err := url.Parse(h.config.logURL())
if nil != err {
if err != nil {
return nil, err
}

Expand All @@ -129,6 +138,9 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) {
WithEndpoint(logURL.Host),
WithUserAgent(h.config.userAgent()),
)
if err != nil {
return nil, err
}

h.config.logDebug(map[string]interface{}{
"event": "harvester created",
Expand All @@ -141,7 +153,7 @@ func NewHarvester(options ...func(*Config)) (*Harvester, error) {
"version": version,
})

if 0 != h.config.HarvestPeriod {
if h.config.HarvestPeriod != 0 {
go harvestRoutine(h)
}

Expand All @@ -160,10 +172,10 @@ func (h *Harvester) RecordSpan(s Span) error {
if nil == h {
return nil
}
if "" == s.TraceID {
if s.TraceID == "" {
return errTraceIDUnset
}
if "" == s.ID {
if s.ID == "" {
return errSpanIDUnset
}
if s.Timestamp.IsZero() {
Expand Down Expand Up @@ -202,7 +214,7 @@ func (h *Harvester) RecordEvent(e Event) error {
if nil == h {
return nil
}
if "" == e.EventType {
if e.EventType == "" {
return errEventTypeUnset
}
if e.Timestamp.IsZero() {
Expand All @@ -221,7 +233,7 @@ func (h *Harvester) RecordLog(l Log) error {
if nil == h {
return nil
}
if "" == l.Message {
if l.Message == "" {
return errLogMessageUnset
}
if l.Timestamp.IsZero() {
Expand Down Expand Up @@ -261,7 +273,7 @@ func (r response) needsRetry(cfg *Config, attempts int) (bool, time.Duration) {
return false, 0
case 429:
// special retry backoff time
if "" != r.retryAfter {
if r.retryAfter != "" {
// Honor Retry-After header value in seconds
if d, err := time.ParseDuration(r.retryAfter + "s"); nil == err {
if d > backoff {
Expand Down Expand Up @@ -321,7 +333,7 @@ func (h *Harvester) swapOutMetrics(now time.Time) []*http.Request {
}
}

if 0 == len(rawMetrics) {
if len(rawMetrics) == 0 {
return nil
}

Expand Down Expand Up @@ -460,7 +472,6 @@ func harvestRequest(req *http.Request, cfg *Config) {
tmr := time.NewTimer(backoff)
select {
case <-tmr.C:
break
case <-req.Context().Done():
tmr.Stop()
return
Expand Down
24 changes: 5 additions & 19 deletions telemetry/harvester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func TestNewRequestHeaders(t *testing.T) {
t.Fatal(reqs)
}
req := reqs[0]
if h := req.Header.Get("Content-Encoding"); "gzip" != h {
if h := req.Header.Get("Content-Encoding"); h != "gzip" {
t.Error("incorrect Content-Encoding header", req.Header)
}
if h := req.Header.Get("User-Agent"); expectUserAgent != h {
Expand All @@ -282,13 +282,13 @@ func TestNewRequestHeaders(t *testing.T) {
t.Fatal(reqs)
}
req = reqs[0]
if h := req.Header.Get("Content-Type"); "application/json" != h {
if h := req.Header.Get("Content-Type"); h != "application/json" {
t.Error("incorrect Content-Type", h)
}
if h := req.Header.Get("Api-Key"); "api-key" != h {
if h := req.Header.Get("Api-Key"); h != "api-key" {
t.Error("incorrect Api-Key", h)
}
if h := req.Header.Get("Content-Encoding"); "gzip" != h {
if h := req.Header.Get("Content-Encoding"); h != "gzip" {
t.Error("incorrect Content-Encoding header", h)
}
if h := req.Header.Get("User-Agent"); expectUserAgent != h {
Expand All @@ -312,20 +312,6 @@ func emptyResponse(status int) *http.Response {
}
}

func uncompressBody(req *http.Request) (string, error) {
body, err := ioutil.ReadAll(req.Body)
defer req.Body.Close()

if err != nil {
return "", fmt.Errorf("unable to read body: %v", err)
}
uncompressed, err := internal.Uncompress(body)
if err != nil {
return "", fmt.Errorf("unable to uncompress body: %v", err)
}
return string(uncompressed), nil
}

// sortedMetricsHelper is used to sort metrics for JSON comparison.
type sortedMetricsHelper []json.RawMessage

Expand Down Expand Up @@ -447,7 +433,7 @@ func TestReturnCodes(t *testing.T) {
})
h.RecordSpan(sp)
h.HarvestNow(context.Background())
if (test.shouldRetry && 2 != posts) || (!test.shouldRetry && 1 != posts) {
if (test.shouldRetry && posts != 2) || (!test.shouldRetry && posts != 1) {
t.Error("incorrect number of posts", posts)
}
}
Expand Down
2 changes: 1 addition & 1 deletion telemetry/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func BenchmarkLogsJSON(b *testing.B) {
for i := 0; i < b.N; i++ {
buf.Reset()
if group.WriteDataEntry(buf); nil == buf.Bytes() || len(buf.Bytes()) == 0 {
b.Fatal(string(buf.Bytes()))
b.Fatal(buf.String())
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions telemetry/spans.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ func (s *Span) writeJSON(buf *bytes.Buffer) {
buf.WriteByte('{')
ww := internal.JSONFieldsWriter{Buf: buf}

if "" != s.Name {
if s.Name != "" {
ww.StringField("name", s.Name)
}
if "" != s.ParentID {
if s.ParentID != "" {
ww.StringField("parent.id", s.ParentID)
}
if 0 != s.Duration {
if s.Duration != 0 {
ww.FloatField("duration.ms", s.Duration.Seconds()*1000.0)
}
if "" != s.ServiceName {
if s.ServiceName != "" {
ww.StringField("service.name", s.ServiceName)
}

Expand Down
2 changes: 1 addition & 1 deletion telemetry/spans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestSpanCommonBlock(t *testing.T) {
}
buf := &bytes.Buffer{}
mapEntry.WriteDataEntry(buf)
json := string(buf.Bytes())
json := buf.String()
if test.expected != json {
t.Errorf("Expected spanCommonBlock to serialize to %s but was %s", test.expected, json)
}
Expand Down
2 changes: 1 addition & 1 deletion telemetry/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type jsonString string

// MarshalJSON returns the JSONString unmodified without any escaping.
func (js jsonString) MarshalJSON() ([]byte, error) {
if "" == js {
if js == "" {
return []byte("null"), nil
}
return []byte(js), nil
Expand Down

0 comments on commit fdddaef

Please sign in to comment.