Skip to content

Commit

Permalink
Update instruments and add tracers
Browse files Browse the repository at this point in the history
  • Loading branch information
levichevdmitry committed Jun 8, 2021
1 parent ae1df63 commit 74d4030
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 33 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# <img src="https://uploads-ssl.webflow.com/5ea5d3315186cf5ec60c3ee4/5edf1c94ce4c859f2b188094_logo.svg" alt="Pip.Services Logo" width="200"> <br/> Remote Procedure Calls for Pip.Services in Go Changelog

## <a name="1.3.2"></a> 1.3.3 (2021-06-08)
### Features
* Update Instruments and added tracers
## <a name="1.3.2"></a> 1.3.2 (2021-05-06)

### Features
* **test** Refactor test services running
* Encode URL params
Expand All @@ -11,7 +13,6 @@
### Features
* Add InstrumentTiming


## <a name="1.3.0"></a> 1.3.0 (2021-04-23)

### Breaking Changes
Expand Down
4 changes: 2 additions & 2 deletions clients/CommandableHttpClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ func NewCommandableHttpClient(baseRoute string) *CommandableHttpClient {
func (c *CommandableHttpClient) CallCommand(prototype reflect.Type, name string, correlationId string, params *cdata.AnyValueMap) (result interface{}, err error) {
timing := c.Instrument(correlationId, c.BaseRoute+"."+name)
cRes, cErr := c.Call(prototype, "post", name, correlationId, nil, params.Value())
timing.EndTiming()
return c.InstrumentError(correlationId, c.BaseRoute+"."+name, cErr, cRes)
timing.EndTiming(cErr)
return cRes, cErr
}
28 changes: 19 additions & 9 deletions clients/DirectClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
crefer "github.com/pip-services3-go/pip-services3-commons-go/refer"
ccount "github.com/pip-services3-go/pip-services3-components-go/count"
ctrace "github.com/pip-services3-go/pip-services3-components-go/trace"
clog "github.com/pip-services3-go/pip-services3-components-go/log"
service "github.com/pip-services3-go/pip-services3-rpc-go/services"
)

/*
Expand Down Expand Up @@ -74,6 +76,8 @@ type DirectClient struct {
Counters *ccount.CompositeCounters
//The dependency resolver to get controller reference.
DependencyResolver crefer.DependencyResolver
// The tracer.
Tracer *ctrace.CompositeTracer;
}

// NewDirectClient is creates a new instance of the client.
Expand All @@ -83,6 +87,7 @@ func NewDirectClient() *DirectClient {
Logger: clog.NewCompositeLogger(),
Counters: ccount.NewCompositeCounters(),
DependencyResolver: *crefer.NewDependencyResolver(),
Tracer: ctrace.NewCompositeTracer(nil),
}
dc.DependencyResolver.Put("controller", "none")
return &dc
Expand All @@ -101,6 +106,7 @@ func (c *DirectClient) Configure(config *cconf.ConfigParams) {
func (c *DirectClient) SetReferences(references crefer.IReferences) {
c.Logger.SetReferences(references)
c.Counters.SetReferences(references)
c.Tracer.SetReferences(references)
c.DependencyResolver.SetReferences(references)
res, cErr := c.DependencyResolver.GetOneRequired("controller")
if cErr != nil {
Expand All @@ -115,10 +121,14 @@ func (c *DirectClient) SetReferences(references crefer.IReferences) {
// - correlationId string (optional) transaction id to trace execution through call chain.
// - name string a method name.
// Returns Timing object to end the time measurement.
func (c *DirectClient) Instrument(correlationId string, name string) *ccount.CounterTiming {
func (c *DirectClient) Instrument(correlationId string, name string) *service.InstrumentTiming {
c.Logger.Trace(correlationId, "Calling %s method", name)
c.Counters.IncrementOne(name + ".call_count")
return c.Counters.BeginTiming(name + ".call_time")

counterTiming := c.Counters.BeginTiming(name + ".call_time")
traceTiming := c.Tracer.BeginTrace(correlationId, name, "")
return service.NewInstrumentTiming(correlationId, name, "call",
c.Logger, c.Counters, counterTiming, traceTiming)
}

// InstrumentError method are adds instrumentation to error handling.
Expand All @@ -129,13 +139,13 @@ func (c *DirectClient) Instrument(correlationId string, name string) *ccount.Cou
// - result (optional) an execution result
// Retruns: result interface{}, err error
// an execution result and error
func (c *DirectClient) InstrumentError(correlationId string, name string, inErr error, inRes interface{}) (result interface{}, err error) {
if inErr != nil {
c.Logger.Error(correlationId, inErr, "Failed to call %s method", name)
c.Counters.IncrementOne(name + ".call_errors")
}
return inRes, inErr
}
// func (c *DirectClient) InstrumentError(correlationId string, name string, inErr error, inRes interface{}) (result interface{}, err error) {
// if inErr != nil {
// c.Logger.Error(correlationId, inErr, "Failed to call %s method", name)
// c.Counters.IncrementOne(name + ".call_errors")
// }
// return inRes, inErr
// }

// IsOpen method are checks if the component is opened.
// Returns true if the component has been opened and false otherwise.
Expand Down
21 changes: 15 additions & 6 deletions clients/RestClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ import (
cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
crefer "github.com/pip-services3-go/pip-services3-commons-go/refer"
ccount "github.com/pip-services3-go/pip-services3-components-go/count"
ctrace "github.com/pip-services3-go/pip-services3-components-go/trace"
clog "github.com/pip-services3-go/pip-services3-components-go/log"
rpccon "github.com/pip-services3-go/pip-services3-rpc-go/connect"
service "github.com/pip-services3-go/pip-services3-rpc-go/services"
)

/*
Expand Down Expand Up @@ -82,9 +84,11 @@ type RestClient struct {
//The connection resolver.
ConnectionResolver rpccon.HttpConnectionResolver
//The logger.
Logger clog.CompositeLogger
Logger *clog.CompositeLogger
//The performance counters.
Counters ccount.CompositeCounters
Counters *ccount.CompositeCounters
// The tracer.
Tracer *ctrace.CompositeTracer
//The configuration options.
Options cconf.ConfigParams
//The base route.
Expand Down Expand Up @@ -120,8 +124,9 @@ func NewRestClient() *RestClient {
"options.correlation_id", "query",
)
rc.ConnectionResolver = *rpccon.NewHttpConnectionResolver()
rc.Logger = *clog.NewCompositeLogger()
rc.Counters = *ccount.NewCompositeCounters()
rc.Logger = clog.NewCompositeLogger()
rc.Counters = ccount.NewCompositeCounters()
rc.Tracer = ctrace.NewCompositeTracer(nil)
rc.Options = *cconf.NewEmptyConfigParams()
rc.Retries = 1
rc.Headers = *cdata.NewEmptyStringValueMap()
Expand Down Expand Up @@ -150,6 +155,7 @@ func (c *RestClient) Configure(config *cconf.ConfigParams) {
func (c *RestClient) SetReferences(references crefer.IReferences) {
c.Logger.SetReferences(references)
c.Counters.SetReferences(references)
c.Tracer.SetReferences(references)
c.ConnectionResolver.SetReferences(references)
}

Expand All @@ -159,10 +165,13 @@ func (c *RestClient) SetReferences(references crefer.IReferences) {
// - correlationId string (optional) transaction id to trace execution through call chain.
// - name string a method name.
// Return Timing object to end the time measurement.
func (c *RestClient) Instrument(correlationId string, name string) *ccount.CounterTiming {
func (c *RestClient) Instrument(correlationId string, name string) *service.InstrumentTiming {
c.Logger.Trace(correlationId, "Calling %s method", name)
c.Counters.IncrementOne(name + ".call_count")
return c.Counters.BeginTiming(name + ".call_time")
counterTiming := c.Counters.BeginTiming(name + ".call_time")
traceTiming := c.Tracer.BeginTrace(correlationId, name, "")
return service.NewInstrumentTiming(correlationId, name, "call",
c.Logger, c.Counters, counterTiming, traceTiming)
}

// InstrumentError method are dds instrumentation to error handling.
Expand Down
2 changes: 1 addition & 1 deletion component.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pip-services3-rpc-go",
"registry": "github.com/pip-services3-go",
"version": "1.3.2",
"version": "1.3.3",
"build": 0
}
7 changes: 2 additions & 5 deletions services/CommandableHttpService.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,8 @@ func (c *CommandableHttpService) Register() {
timing := c.Instrument(correlationId, c.BaseRoute+"."+command.Name())

execRes, execErr := command.Execute(correlationId, args)
timing.EndTiming()
instrRes, instrErr := c.InstrumentError(correlationId,
c.BaseRoute+"."+command.Name(),
execErr, execRes)
c.SendResult(res, req, instrRes, instrErr)
timing.EndTiming(execErr)
c.SendResult(res, req, execRes, execErr)

})
}
Expand Down
12 changes: 10 additions & 2 deletions services/RestService.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
crefer "github.com/pip-services3-go/pip-services3-commons-go/refer"
cvalid "github.com/pip-services3-go/pip-services3-commons-go/validate"
ccount "github.com/pip-services3-go/pip-services3-components-go/count"
ctrace "github.com/pip-services3-go/pip-services3-components-go/trace"
clog "github.com/pip-services3-go/pip-services3-components-go/log"
)

Expand Down Expand Up @@ -134,6 +135,8 @@ type RestService struct {
Logger *clog.CompositeLogger
//The performance counters.
Counters *ccount.CompositeCounters
// The tracer.
Tracer *ctrace.CompositeTracer

SwaggerService ISwaggerService
SwaggerEnabled bool
Expand All @@ -154,6 +157,7 @@ func InheritRestService(overrides IRestServiceOverrides) *RestService {
rs.DependencyResolver.Configure(rs.defaultConfig)
rs.Logger = clog.NewCompositeLogger()
rs.Counters = ccount.NewCompositeCounters()
rs.Tracer = ctrace.NewCompositeTracer(nil)
rs.SwaggerEnabled = false
rs.SwaggerRoute = "swagger"
return &rs
Expand All @@ -180,6 +184,7 @@ func (c *RestService) SetReferences(references crefer.IReferences) {

c.Logger.SetReferences(references)
c.Counters.SetReferences(references)
c.Tracer.SetReferences(references)
c.DependencyResolver.SetReferences(references)

// Get endpoint
Expand Down Expand Up @@ -233,10 +238,13 @@ func (c *RestService) createEndpoint() *HttpEndpoint {
// - correlationId (optional) transaction id to trace execution through call chain.
// - name a method name.
// Returns Timing object to end the time measurement.
func (c *RestService) Instrument(correlationId string, name string) *ccount.CounterTiming {
func (c *RestService) Instrument(correlationId string, name string) *InstrumentTiming {
c.Logger.Trace(correlationId, "Executing %s method", name)
c.Counters.IncrementOne(name + ".exec_count")
return c.Counters.BeginTiming(name + ".exec_time")
counterTiming := c.Counters.BeginTiming(name + ".exec_time")
traceTiming := c.Tracer.BeginTrace(correlationId, name, "")
return NewInstrumentTiming(correlationId, name, "exec",
c.Logger, c.Counters, counterTiming, traceTiming)
}

// InstrumentError method are adds instrumentation to error handling.
Expand Down
12 changes: 6 additions & 6 deletions test/clients/DummyDirectClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (c *DummyDirectClient) GetDummies(correlationId string, filter *cdata.Filte

timing := c.Instrument(correlationId, "dummy.get_page_by_filter")
result, err = c.specificController.GetPageByFilter(correlationId, filter, paging)
timing.EndTiming()
timing.EndTiming(err)
return result, err

}
Expand All @@ -44,38 +44,38 @@ func (c *DummyDirectClient) GetDummyById(correlationId string, dummyId string) (

timing := c.Instrument(correlationId, "dummy.get_one_by_id")
result, err = c.specificController.GetOneById(correlationId, dummyId)
timing.EndTiming()
timing.EndTiming(err)
return result, err
}

func (c *DummyDirectClient) CreateDummy(correlationId string, dummy tdata.Dummy) (result *tdata.Dummy, err error) {

timing := c.Instrument(correlationId, "dummy.create")
result, err = c.specificController.Create(correlationId, dummy)
timing.EndTiming()
timing.EndTiming(err)
return result, err
}

func (c *DummyDirectClient) UpdateDummy(correlationId string, dummy tdata.Dummy) (result *tdata.Dummy, err error) {

timing := c.Instrument(correlationId, "dummy.update")
result, err = c.specificController.Update(correlationId, dummy)
timing.EndTiming()
timing.EndTiming(err)
return result, err
}

func (c *DummyDirectClient) DeleteDummy(correlationId string, dummyId string) (result *tdata.Dummy, err error) {

timing := c.Instrument(correlationId, "dummy.delete_by_id")
result, err = c.specificController.DeleteById(correlationId, dummyId)
timing.EndTiming()
timing.EndTiming(err)
return result, err
}

func (c *DummyDirectClient) CheckCorrelationId(correlationId string) (result map[string]string, err error) {

timing := c.Instrument(correlationId, "dummy.delete_by_id")
result, err = c.specificController.CheckCorrelationId(correlationId)
timing.EndTiming()
timing.EndTiming(err)
return result, err
}

0 comments on commit 74d4030

Please sign in to comment.