From c56e90b0935a24da9fa661268909202e7dde3491 Mon Sep 17 00:00:00 2001 From: AbhishekKr Date: Mon, 12 Aug 2024 20:47:02 +0530 Subject: [PATCH] fix test; remove newrelic add dummy log metric Signed-off-by: AbhishekKr --- consumer/consumer.go | 2 +- consumer/consumer_test.go | 13 -------- consumer/plugin_consumer.go | 2 +- consumer/tcp_consumer.go | 5 +++ go.mod | 16 +++++---- go.sum | 23 ++++--------- instrumentation/metric.go | 41 +++++++++++++++++++++++ instrumentation/newrelic.go | 59 --------------------------------- producer/producer.go | 4 --- producer/producer_test.go | 21 +++--------- transformer/transformer.go | 5 --- transformer/transformer_test.go | 48 ++++++--------------------- 12 files changed, 79 insertions(+), 160 deletions(-) create mode 100644 instrumentation/metric.go delete mode 100644 instrumentation/newrelic.go diff --git a/consumer/consumer.go b/consumer/consumer.go index 279fe59..685a272 100644 --- a/consumer/consumer.go +++ b/consumer/consumer.go @@ -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]() diff --git a/consumer/consumer_test.go b/consumer/consumer_test.go index 359b2fe..c0e2354 100644 --- a/consumer/consumer_test.go +++ b/consumer/consumer_test.go @@ -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" ) @@ -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 } diff --git a/consumer/plugin_consumer.go b/consumer/plugin_consumer.go index e14ddea..6612e7d 100644 --- a/consumer/plugin_consumer.go +++ b/consumer/plugin_consumer.go @@ -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())() } diff --git a/consumer/tcp_consumer.go b/consumer/tcp_consumer.go index e4e1ab0..ffeafd7 100644 --- a/consumer/tcp_consumer.go +++ b/consumer/tcp_consumer.go @@ -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" ) @@ -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)) diff --git a/go.mod b/go.mod index 1c03d3e..0444288 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 63850f8..7a95e5e 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/instrumentation/metric.go b/instrumentation/metric.go new file mode 100644 index 0000000..cd24664 --- /dev/null +++ b/instrumentation/metric.go @@ -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)) +} diff --git a/instrumentation/newrelic.go b/instrumentation/newrelic.go deleted file mode 100644 index 2064fdd..0000000 --- a/instrumentation/newrelic.go +++ /dev/null @@ -1,59 +0,0 @@ -package instrumentation - -import ( - "net/http" - - "github.com/gol-gol/golenv" - newrelic "github.com/newrelic/go-agent" - - logger "github.com/OpenChaos/ogi/logger" -) - -type NewrelicCtx struct { - NewrelicApp newrelic.Application -} - -var ( - instrumentationCtx *NewrelicCtx -) - -func init() { - config := newrelic.NewConfig( - golenv.OverrideIfEnv("NEWRELIC_APP_NAME", "kafka_ogi"), - golenv.OverrideIfEnv("NEWRELIC_LICENSE_KEY", "x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x"), - ) - newrelicApp, err := newrelic.NewApplication(config) - if err != nil { - logger.Errorf("newrelic init failed: %s", err) - return - } - instrumentationCtx = &NewrelicCtx{ - NewrelicApp: newrelicApp, - } -} - -func StartTransaction(txn string, w http.ResponseWriter, r *http.Request) newrelic.Transaction { - defer func() { - if r := recover(); r != nil { - err := r.(error) - logger.Errorf("start transaction failed: %s", err) - } - }() - - if instrumentationCtx.NewrelicApp != nil { - return instrumentationCtx.NewrelicApp.StartTransaction(txn, w, r) - } - logger.Errorln("newrelic is not initialized") - return nil -} - -func EndTransaction(transaction *newrelic.Transaction) { - defer func() { - if r := recover(); r != nil { - err := r.(error) - logger.Errorf("end transaction failed: %s", err) - } - }() - - (*transaction).End() -} diff --git a/producer/producer.go b/producer/producer.go index b3846d7..88297fb 100644 --- a/producer/producer.go +++ b/producer/producer.go @@ -1,7 +1,6 @@ package ogiproducer import ( - "github.com/OpenChaos/ogi/instrumentation" logger "github.com/OpenChaos/ogi/logger" "github.com/gol-gol/golenv" ) @@ -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() diff --git a/producer/producer_test.go b/producer/producer_test.go index b6900f3..6ed2eae 100644 --- a/producer/producer_test.go +++ b/producer/producer_test.go @@ -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" ) @@ -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) { @@ -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 diff --git a/transformer/transformer.go b/transformer/transformer.go index 5a7bd2e..9c7fa64 100644 --- a/transformer/transformer.go +++ b/transformer/transformer.go @@ -2,8 +2,6 @@ package ogitransformer import ( "github.com/gol-gol/golenv" - - "github.com/OpenChaos/ogi/instrumentation" ) type Transformer interface { @@ -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) } diff --git a/transformer/transformer_test.go b/transformer/transformer_test.go index 9a06eeb..f9865b3 100644 --- a/transformer/transformer_test.go +++ b/transformer/transformer_test.go @@ -2,14 +2,11 @@ package ogitransformer import ( "errors" - "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" ) @@ -24,38 +21,25 @@ func init() { logger.SetupLogger() } -func (m *MockTransformer) Transform(msg []byte) error { +func (m *MockTransformer) Transform(msgid string, msg []byte) ([]byte, error) { countOfMockTransformerCalled = 1 if len(msg) == 0 { countOfMockTransformerError += 1 - return errors.New("transform failed") + return []byte{}, errors.New("transform failed") } - return nil + return []byte{}, nil } func TestTransformSuccess(t *testing.T) { - var nr, nrEnd *monkey.PatchGuard - var nrB, nrEndB, transparentTransformerB bool - nr = monkey.Patch(instrumentation.StartTransaction, func(string, http.ResponseWriter, *http.Request) newrelic.Transaction { - nr.Unpatch() - defer nr.Restore() - nrB = true - return nil - }) - nrEnd = monkey.Patch(instrumentation.EndTransaction, func(*newrelic.Transaction) { - nrEnd.Unpatch() - defer nrEnd.Restore() - nrEndB = true - }) + var transparentTransformerB bool monkey.Patch(NewTransparentTransformer, func() Transformer { transparentTransformerB = true return &MockTransformer{} }) - Transform([]byte("{}")) + _, e := Transform("ulid", []byte("{}")) + assert.Equal(t, e, nil) - assert.True(t, nrB) - assert.True(t, nrEndB) assert.True(t, transparentTransformerB) assert.Equal(t, 1, countOfMockTransformerCalled) assert.Equal(t, 0, countOfMockTransformerError) @@ -63,27 +47,15 @@ func TestTransformSuccess(t *testing.T) { } func TestTransformFailure(t *testing.T) { - var nr, nrEnd *monkey.PatchGuard - var nrB, nrEndB, transparentTransformerB bool - nr = monkey.Patch(instrumentation.StartTransaction, func(string, http.ResponseWriter, *http.Request) newrelic.Transaction { - nr.Unpatch() - defer nr.Restore() - nrB = true - return nil - }) - nrEnd = monkey.Patch(instrumentation.EndTransaction, func(*newrelic.Transaction) { - nrEnd.Unpatch() - defer nrEnd.Restore() - nrEndB = true - }) + var transparentTransformerB bool monkey.Patch(NewTransparentTransformer, func() Transformer { transparentTransformerB = true return &MockTransformer{} }) - Transform([]byte("")) - assert.True(t, nrB) - assert.True(t, nrEndB) + _, e := Transform("id", []byte("")) + assert.Equal(t, e.Error(), "transform failed") + assert.True(t, transparentTransformerB) assert.Equal(t, 1, countOfMockTransformerCalled) assert.Equal(t, 1, countOfMockTransformerError)