Skip to content

Commit

Permalink
fix test; remove newrelic add dummy log metric
Browse files Browse the repository at this point in the history
Signed-off-by: AbhishekKr <[email protected]>
  • Loading branch information
abhishekkr committed Aug 12, 2024
1 parent 07395c6 commit c56e90b
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 160 deletions.
2 changes: 1 addition & 1 deletion consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
)

func Consume() {
txn := instrumentation.StartTransaction("consume_transaction", nil, nil)
txn := instrumentation.StartTransaction("CONSUMER")
defer instrumentation.EndTransaction(&txn)

consumer := consumerMap[ConsumerType]()
Expand Down
13 changes: 0 additions & 13 deletions consumer/consumer_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package ogiconsumer

import (
"net/http"
"testing"

"bou.ke/monkey"
newrelic "github.com/newrelic/go-agent"
"github.com/stretchr/testify/assert"

instrumentation "github.com/OpenChaos/ogi/instrumentation"
logger "github.com/OpenChaos/ogi/logger"
)

Expand All @@ -24,23 +21,13 @@ func (m *MockConsumer) Consume() {
}

func TestConsume(t *testing.T) {
var nrB, nrEndB bool
logger.SetupLogger()

monkey.Patch(instrumentation.StartTransaction, func(string, http.ResponseWriter, *http.Request) newrelic.Transaction {
nrB = true
return nil
})
monkey.Patch(instrumentation.EndTransaction, func(*newrelic.Transaction) {
nrEndB = true
})
monkey.Patch(NewTCPServer, func() Consumer {
return &MockConsumer{}
})
Consume()

assert.True(t, nrB)
assert.True(t, nrEndB)
assert.Equal(t, 1, countOfMockConsumerCalled)
countOfMockConsumerCalled = 0 //resetting
}
2 changes: 1 addition & 1 deletion consumer/plugin_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewConsumerPlugin() Consumer {
}

func (plugin *ConsumerPlugin) Consume() {
txn := instrumentation.StartTransaction("event_kafka_message_transaction", nil, nil)
txn := instrumentation.StartTransaction(ConsumerPluginPath)
defer instrumentation.EndTransaction(&txn)
plugin.ConsumeFunc.(func())()
}
5 changes: 5 additions & 0 deletions consumer/tcp_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gol-gol/golenv"
ulid "github.com/oklog/ulid/v2"

instrumentation "github.com/OpenChaos/ogi/instrumentation"
ogitransformer "github.com/OpenChaos/ogi/transformer"
)

Expand Down Expand Up @@ -57,7 +58,11 @@ func (t *TCPServer) Start() {
}

msgid := ulid.Make().String()

txn := instrumentation.StartTransaction(msgid)
t.Transform(msgid, netData)
instrumentation.EndTransaction(&txn)

tym := time.Now()
response := msgid + ": " + tym.Format(time.RFC3339) + "\n"
c.Write([]byte(response))
Expand Down
16 changes: 10 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
module github.com/OpenChaos/ogi

go 1.15
go 1.22.6

require (
bou.ke/monkey v1.0.2
github.com/gol-gol/golconv v0.0.0-20230302172921-7c155f24578e // indirect
github.com/gol-gol/golenv v0.0.0-20230302172901-210791b57f21
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/newrelic/go-agent v3.8.1+incompatible
github.com/oklog/ulid/v2 v2.1.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.24.0 // indirect
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gol-gol/golconv v0.0.0-20230302154024-425de09df6c5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 6 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,25 @@ bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gol-gol/golconv v0.0.0-20230302154024-425de09df6c5 h1:3nhHK/QWfB7jjijFzF2wNB9yc2u4DH5VA+whZK/8U30=
github.com/gol-gol/golconv v0.0.0-20230302154024-425de09df6c5/go.mod h1:T/uFyCj4rCA2xXJUOt1bhxyw72cTKp1nQL5nte33+lU=
github.com/gol-gol/golconv v0.0.0-20230302172921-7c155f24578e h1:eD9L74T9k79Jk0grO7PJvCn0+0xPOeT/KELFPgV21VE=
github.com/gol-gol/golconv v0.0.0-20230302172921-7c155f24578e/go.mod h1:T/uFyCj4rCA2xXJUOt1bhxyw72cTKp1nQL5nte33+lU=
github.com/gol-gol/golenv v0.0.0-20230302172901-210791b57f21 h1:5fZosHmekqbOv0VsRona/tL2lnH8mHOo+ej8RGKiDX0=
github.com/gol-gol/golenv v0.0.0-20230302172901-210791b57f21/go.mod h1:JVzU4RV7SBNrDnew3lSO/LuLboHW0ZNuyW2V/DaOWLE=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/newrelic/go-agent v3.8.1+incompatible h1:8TAEekJseggmwfn79CjoV308PyNlzDVExkUwFeDBUxk=
github.com/newrelic/go-agent v3.8.1+incompatible/go.mod h1:a8Fv1b/fYhFSReoTU6HDkTYIMZeSVNffmoS726Y0LzQ=
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6 h1:DvY3Zkh7KabQE/kfzMvYvKirSiguP9Q/veMtkYyf0o8=
golang.org/x/sys v0.0.0-20200826173525-f9321e4c35a6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
41 changes: 41 additions & 0 deletions instrumentation/metric.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package instrumentation

import (
"time"

"github.com/gol-gol/golenv"

logger "github.com/OpenChaos/ogi/logger"
)

type MetricTxn struct {
Key string
StartedAt time.Time
}

var (
ENABLE_INSTRUMENTATION = golenv.OverrideIfEnvBool("OGI_INSTRUMENTATION", false)
)

func init() {
if !ENABLE_INSTRUMENTATION {
return
}
}

func StartTransaction(key string) MetricTxn {
if !ENABLE_INSTRUMENTATION {
return MetricTxn{}
}
return MetricTxn{
Key: key,
StartedAt: time.Now(),
}
}

func EndTransaction(txn *MetricTxn) {
if !ENABLE_INSTRUMENTATION {
return
}
logger.Infof("[METRIC] %s (%v)", txn.Key, time.Since(txn.StartedAt))
}
59 changes: 0 additions & 59 deletions instrumentation/newrelic.go

This file was deleted.

4 changes: 0 additions & 4 deletions producer/producer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ogiproducer

import (
"github.com/OpenChaos/ogi/instrumentation"
logger "github.com/OpenChaos/ogi/logger"
"github.com/gol-gol/golenv"
)
Expand All @@ -27,9 +26,6 @@ func NewProducer() Producer {
}

func Produce(msgid string, msg []byte) ([]byte, error) {
txn := instrumentation.StartTransaction("produce_transaction", nil, nil)
defer instrumentation.EndTransaction(&txn)

producer := NewProducer()
defer producer.Close()

Expand Down
21 changes: 5 additions & 16 deletions producer/producer_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package ogiproducer

import (
"net/http"
"testing"

"bou.ke/monkey"
newrelic "github.com/newrelic/go-agent"
"github.com/stretchr/testify/assert"

instrumentation "github.com/OpenChaos/ogi/instrumentation"
logger "github.com/OpenChaos/ogi/logger"
)

Expand All @@ -27,9 +24,9 @@ func (m *MockProducer) Close() {
countOfMockProducerCloseCalled += 1
return
}
func (m *MockProducer) Produce(t string, msg []byte, key string) {
func (m *MockProducer) Produce(msgid string, msg []byte) ([]byte, error) {
countOfMockProducerProduceCalled += 1
return
return []byte{}, nil
}

func TestNewProducer(t *testing.T) {
Expand All @@ -38,22 +35,14 @@ func TestNewProducer(t *testing.T) {
}

func TestProduce(t *testing.T) {
var nrB, nrEndB bool
mockProducer := &MockProducer{}
monkey.Patch(instrumentation.StartTransaction, func(string, http.ResponseWriter, *http.Request) newrelic.Transaction {
nrB = true
return nil
})
monkey.Patch(instrumentation.EndTransaction, func(*newrelic.Transaction) {
nrEndB = true
})
monkey.Patch(NewProducer, func() Producer {
return mockProducer
})

Produce("topik", []byte{}, "mykey")
assert.True(t, nrB)
assert.True(t, nrEndB)
_, e := Produce("ulid", []byte{})
assert.Equal(t, e, nil)

assert.Equal(t, countOfMockProducerProduceCalled, 1)
assert.Equal(t, countOfMockProducerCloseCalled, 1)
countOfMockProducerProduceCalled = 0 // resetting
Expand Down
5 changes: 0 additions & 5 deletions transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package ogitransformer

import (
"github.com/gol-gol/golenv"

"github.com/OpenChaos/ogi/instrumentation"
)

type Transformer interface {
Expand All @@ -22,9 +20,6 @@ var (
)

func Transform(msgid string, msg []byte) ([]byte, error) {
txn := instrumentation.StartTransaction("transform_transaction", nil, nil)
defer instrumentation.EndTransaction(&txn)

transformer := transformerMap[TransformerType]()
return transformer.Transform(msgid, msg)
}
Loading

0 comments on commit c56e90b

Please sign in to comment.