Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
plugin changes
Browse files Browse the repository at this point in the history
  • Loading branch information
GRECO, FRANK committed Aug 14, 2017
1 parent ec8c3d5 commit 17ade4d
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Extracted request metrics out of context

## [1.0.3] - 2017-08-11
### Changed
- Fixed issue that used wrong map key for total time metric
Expand Down
2 changes: 1 addition & 1 deletion helm/values.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

imageRegistry: northwesternmutual

dockerImageTag: v1.0.0
dockerImageTag: v1.1.0

pullPolicy: Always

Expand Down
20 changes: 20 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Copyright (c) 2017 Northwestern Mutual.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package metrics

// Metric represent a single request metric
Expand Down
20 changes: 20 additions & 0 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
// Copyright (c) 2017 Northwestern Mutual.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package metrics

import (
Expand Down
5 changes: 3 additions & 2 deletions plugins/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
pluginPkg "plugin"

"github.com/northwesternmutual/kanali/config"
"github.com/northwesternmutual/kanali/metrics"
"github.com/northwesternmutual/kanali/controller"
"github.com/northwesternmutual/kanali/spec"
"github.com/northwesternmutual/kanali/utils"
Expand All @@ -39,8 +40,8 @@ const pluginSymbolName = "Plugin"
// Plugin is an interface that is used for every Plugin used by Kanali.
// If external plugins are developed, they also must conform to this interface.
type Plugin interface {
OnRequest(ctx context.Context, proxy spec.APIProxy, ctlr controller.Controller, req *http.Request, span opentracing.Span) error
OnResponse(ctx context.Context, proxy spec.APIProxy, ctlr controller.Controller, req *http.Request, resp *http.Response, span opentracing.Span) error
OnRequest(ctx context.Context, m *metrics.Metrics, proxy spec.APIProxy, ctlr controller.Controller, req *http.Request, span opentracing.Span) error
OnResponse(ctx context.Context, m *metrics.Metrics, proxy spec.APIProxy, ctlr controller.Controller, req *http.Request, resp *http.Response, span opentracing.Span) error
}

// GetPlugin will use the Go plugin package and extract
Expand Down
13 changes: 7 additions & 6 deletions steps/mockplugins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,37 @@ import (
"net/http"

"github.com/northwesternmutual/kanali/controller"
"github.com/northwesternmutual/kanali/metrics"
"github.com/northwesternmutual/kanali/spec"
"github.com/opentracing/opentracing-go"
)

type fakePanicPlugin struct{}

func (plugin fakePanicPlugin) OnRequest(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
func (plugin fakePanicPlugin) OnRequest(ctx context.Context, m *metrics.Metrics, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
panic("intentional")
}

func (plugin fakePanicPlugin) OnResponse(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
func (plugin fakePanicPlugin) OnResponse(ctx context.Context, m *metrics.Metrics, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
panic("intentional")
}

type fakeSuccessPlugin struct{}

func (plugin fakeSuccessPlugin) OnRequest(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
func (plugin fakeSuccessPlugin) OnRequest(ctx context.Context, m *metrics.Metrics, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
return nil
}

func (plugin fakeSuccessPlugin) OnResponse(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
func (plugin fakeSuccessPlugin) OnResponse(ctx context.Context, m *metrics.Metrics, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
return nil
}

type fakeErrorPlugin struct{}

func (plugin fakeErrorPlugin) OnRequest(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
func (plugin fakeErrorPlugin) OnRequest(ctx context.Context, m *metrics.Metrics, p spec.APIProxy, c controller.Controller, r *http.Request, span opentracing.Span) error {
return errors.New("error")
}

func (plugin fakeErrorPlugin) OnResponse(ctx context.Context, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
func (plugin fakeErrorPlugin) OnResponse(ctx context.Context, m *metrics.Metrics, p spec.APIProxy, c controller.Controller, r *http.Request, resp *http.Response, span opentracing.Span) error {
return errors.New("error")
}
6 changes: 3 additions & 3 deletions steps/pluginsonrequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func (step PluginsOnRequestStep) Do(ctx context.Context, m *metrics.Metrics, c *
if err != nil {
return err
}
if err := doOnRequest(ctx, plugin.Name, proxy, *c, r, trace, *p); err != nil {
if err := doOnRequest(ctx, m, plugin.Name, proxy, *c, r, trace, *p); err != nil {
return err
}
}
return nil
}

func doOnRequest(ctx context.Context, name string, proxy spec.APIProxy, ctlr controller.Controller, req *http.Request, span opentracing.Span, p plugins.Plugin) (e error) {
func doOnRequest(ctx context.Context, m *metrics.Metrics, name string, proxy spec.APIProxy, ctlr controller.Controller, req *http.Request, span opentracing.Span, p plugins.Plugin) (e error) {
defer func() {
if r := recover(); r != nil {
logrus.Errorf("OnRequest paniced: %v", r)
Expand All @@ -79,7 +79,7 @@ func doOnRequest(ctx context.Context, name string, proxy spec.APIProxy, ctlr con
sp := opentracing.StartSpan(fmt.Sprintf("PLUGIN: ON_REQUEST: %s", name), opentracing.ChildOf(span.Context()))
defer sp.Finish()

if err := p.OnRequest(ctx, proxy, ctlr, req, sp); err != nil {
if err := p.OnRequest(ctx, m, proxy, ctlr, req, sp); err != nil {
return err
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions steps/pluginsonrequest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestPluginsOnRequestGetName(t *testing.T) {
}

func TestDoOnRequest(t *testing.T) {
assert.Equal(t, doOnRequest(context.Background(), "name", spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakePanicPlugin{}).Error(), "OnRequest paniced")
assert.Equal(t, doOnRequest(context.Background(), "name", spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakeErrorPlugin{}).Error(), "error")
assert.Nil(t, doOnRequest(context.Background(), "name", spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakeSuccessPlugin{}))
assert.Equal(t, doOnRequest(context.Background(), nil, "name", spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakePanicPlugin{}).Error(), "OnRequest paniced")
assert.Equal(t, doOnRequest(context.Background(), nil, "name", spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakeErrorPlugin{}).Error(), "error")
assert.Nil(t, doOnRequest(context.Background(), nil, "name", spec.APIProxy{}, controller.Controller{}, nil, opentracing.StartSpan("test span"), fakeSuccessPlugin{}))
}
6 changes: 3 additions & 3 deletions steps/pluginsonresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ func (step PluginsOnResponseStep) Do(ctx context.Context, m *metrics.Metrics, c
if err != nil {
return err
}
if err := doOnResponse(ctx, plugin.Name, proxy, *c, r, resp, trace, *p); err != nil {
if err := doOnResponse(ctx, m, plugin.Name, proxy, *c, r, resp, trace, *p); err != nil {
return err
}
}

return nil
}

func doOnResponse(ctx context.Context, name string, proxy spec.APIProxy, ctlr controller.Controller, req *http.Request, resp *http.Response, span opentracing.Span, p plugins.Plugin) (e error) {
func doOnResponse(ctx context.Context, m *metrics.Metrics, name string, proxy spec.APIProxy, ctlr controller.Controller, req *http.Request, resp *http.Response, span opentracing.Span, p plugins.Plugin) (e error) {
defer func() {
if r := recover(); r != nil {
logrus.Errorf("OnResponse paniced: %v", r)
Expand All @@ -80,7 +80,7 @@ func doOnResponse(ctx context.Context, name string, proxy spec.APIProxy, ctlr co
sp := opentracing.StartSpan(fmt.Sprintf("PLUGIN: ON_RESPONSE: %s", name), opentracing.ChildOf(span.Context()))
defer sp.Finish()

if err := p.OnResponse(ctx, proxy, ctlr, req, resp, sp); err != nil {
if err := p.OnResponse(ctx, m, proxy, ctlr, req, resp, sp); err != nil {
return err
}
return nil
Expand Down
6 changes: 3 additions & 3 deletions steps/pluginsonresponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestPluginsOnResponseGetName(t *testing.T) {
}

func TestDoOnResponse(t *testing.T) {
assert.Equal(t, doOnResponse(context.Background(), "name", spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakePanicPlugin{}).Error(), "OnResponse paniced")
assert.Equal(t, doOnResponse(context.Background(), "name", spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakeErrorPlugin{}).Error(), "error")
assert.Nil(t, doOnResponse(context.Background(), "name", spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakeSuccessPlugin{}))
assert.Equal(t, doOnResponse(context.Background(), nil, "name", spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakePanicPlugin{}).Error(), "OnResponse paniced")
assert.Equal(t, doOnResponse(context.Background(), nil, "name", spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakeErrorPlugin{}).Error(), "error")
assert.Nil(t, doOnResponse(context.Background(), nil, "name", spec.APIProxy{}, controller.Controller{}, nil, nil, opentracing.StartSpan("test span"), fakeSuccessPlugin{}))
}

0 comments on commit 17ade4d

Please sign in to comment.