-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added statsD and new facebook injection module
- Loading branch information
1 parent
0f88662
commit 2a74d7d
Showing
16 changed files
with
208 additions
and
613 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
template_files/_build/dockerfile/microservice-template/config.ctmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"Mykey": "{{key "my-key"}}" | ||
"StatsDServerIP": {{key "StatsDServerIP"}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
{{if .StatsD}}"github.com/nicholasjackson/docker-testing/logging"{{end}} | ||
) | ||
|
||
// This is not particularlly a real world example it mearly shows how a builder or a factory could be injected | ||
// into the HealthHandler | ||
type HealthResponseBuilder struct { | ||
statusMessage string | ||
} | ||
|
||
func (b *HealthResponseBuilder) SetStatusMessage(message string) *HealthResponseBuilder { | ||
b.statusMessage = message | ||
return b | ||
} | ||
|
||
func (b *HealthResponseBuilder) Build() HealthResponse { | ||
var hr HealthResponse | ||
hr.StatusMessage = b.statusMessage | ||
return hr | ||
} | ||
|
||
type HealthDependencies struct { | ||
// if not specified will create singleton | ||
SingletonBuilder *HealthResponseBuilder `inject:""` | ||
{{if .StatsD}} | ||
// statsD interface must use a name type as injection cannot infer ducktypes | ||
Stats logging.StatsD `inject:"statsd"` | ||
{{end}} | ||
// if not specified in the graph will automatically create private instance | ||
PrivateBuilder *HealthResponseBuilder `inject:"private"` | ||
} | ||
|
||
type HealthResponse struct { | ||
StatusMessage string `json:"status_message"` | ||
} | ||
|
||
var HealthHandlerDependencies *HealthDependencies = &HealthDependencies{} | ||
|
||
{{if .StatsD}}const HEALTH_HANDLER_CALLED = "{{.ServiceName}}.health_handler"{{end}} | ||
|
||
func HealthHandler(rw http.ResponseWriter, r *http.Request) { | ||
// all HealthHandlerDependencies are automatically created by injection process | ||
{{if .StatsD}}HealthHandlerDependencies.Stats.Increment(HEALTH_HANDLER_CALLED){{end}} | ||
|
||
response := HealthHandlerDependencies.SingletonBuilder.SetStatusMessage("OK").Build() | ||
|
||
encoder := json.NewEncoder(rw) | ||
encoder.Encode(&response) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/facebookgo/inject" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
{{if .StatsD}} | ||
type MockStatsD struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *MockStatsD) Increment(label string) { | ||
_ = m.Mock.Called(label) | ||
} | ||
|
||
var statsDMock *MockStatsD | ||
{{end}} | ||
|
||
func TestSetup(t *testing.T) { | ||
// create an injection graph containing the mocked elements we wish to replace | ||
|
||
var g inject.Graph | ||
|
||
{{if .StatsD}}statsDMock = &MockStatsD{}{{end}} | ||
|
||
err := g.Provide( | ||
&inject.Object{Value: HealthHandlerDependencies}, | ||
{{if .StatsD}}&inject.Object{Value: statsDMock, Name: "statsd"},{{end}} | ||
) | ||
|
||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
if err := g.Populate(); err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
statsDMock.Mock.On("Increment", mock.Anything).Return() | ||
} | ||
|
||
// Simple test to show how we can use the ResponseRecorder to test our HTTP handlers | ||
func TestHealthHandler(t *testing.T) { | ||
var responseRecorder httptest.ResponseRecorder | ||
var request http.Request | ||
|
||
HealthHandler(&responseRecorder, &request) | ||
|
||
assert.Equal(t, 200, responseRecorder.Code) | ||
} | ||
|
||
{{if .StatsD}} | ||
func TestHealthHandlerSetStats(t *testing.T) { | ||
var responseRecorder httptest.ResponseRecorder | ||
var request http.Request | ||
|
||
HealthHandler(&responseRecorder, &request) | ||
|
||
statsDMock.Mock.AssertCalled(t, "Increment", HEALTH_HANDLER_CALLED) | ||
} | ||
{{end}} |
Oops, something went wrong.