-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hmetrics: Add license/headers, make endpoint specifyable.
- Loading branch information
Edward Muller
committed
Feb 27, 2018
1 parent
aab1850
commit 9825c2a
Showing
10 changed files
with
208 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Copyright (c) 2018, Salesforce.com, Inc. | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above copyright notice, this | ||
list of conditions and the following disclaimer in the documentation and/or | ||
other materials provided with the distribution. | ||
|
||
* Neither the name of Salesforce.com nor the names of its contributors may be | ||
used to endorse or promote products derived from this software without specific | ||
prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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,5 +1,18 @@ | ||
/* Copyright (c) 2018 Salesforce | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
/* | ||
package hmetrics is a self-contained client for heroku Go runtime metrics. | ||
*/ | ||
Package hmetrics is a self-contained client for Heroku Go runtime metrics. | ||
Typical usage is through the `github.com/heroku/x/hmetrics/onload` package | ||
imported like so: | ||
import _ "github.com/heroku/x/hmetrics/onload" | ||
You can find more information about Heroku Go runtime metrics here: | ||
https://devcenter.heroku.com/articles/language-runtime-metrics-go | ||
*/ | ||
package hmetrics |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* Copyright (c) 2018 Salesforce | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
package hmetrics_test | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/heroku/x/hmetrics" | ||
) | ||
|
||
func ExampleReport_basic() { | ||
// Don't care about canceling or errors | ||
go hmetrics.Report(context.Background(), hmetrics.DefaultEndpoint, nil) | ||
|
||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
} | ||
http.ListenAndServe(":"+port, nil) | ||
} | ||
|
||
func ExampleReport_logging() { | ||
go func() { | ||
if err := hmetrics.Report(context.Background(), hmetrics.DefaultEndpoint, func(err error) error { | ||
log.Println("Error reporting metrics to heroku:", err) | ||
return nil | ||
}); err != nil { | ||
log.Fatal("Error starting hmetrics reporting:", err) | ||
} | ||
}() | ||
} | ||
func ExampleReport_advanced() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
go func() { | ||
type fataler interface { | ||
Fatal() bool | ||
} | ||
for { // try again and again on non fatal errors | ||
if err := hmetrics.Report(ctx, hmetrics.DefaultEndpoint, func(err error) error { | ||
log.Println("Error reporting metrics to heroku:", err) | ||
return nil | ||
}); err != nil { | ||
if f, ok := err.(fataler); ok && f.Fatal() { | ||
log.Fatal(err) | ||
} | ||
log.Println(err) | ||
} | ||
} | ||
}() | ||
|
||
port := os.Getenv("PORT") | ||
if port == "" { | ||
port = "8080" | ||
} | ||
http.ListenAndServe(":"+port, nil) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* Copyright (c) 2018 Salesforce | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
package hmetrics | ||
|
||
import "testing" | ||
|
||
func TestStartable_EmptyEndpoint(t *testing.T) { | ||
err := startable("") | ||
if err == nil { | ||
t.Errorf("Expected an error, but got nil instead") | ||
} | ||
} |
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 |
---|---|---|
@@ -1,27 +1,49 @@ | ||
/* Copyright (c) 2018 Salesforce | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
/* | ||
package onload automatically starts up the hmetrics reporting. | ||
Package onload automatically starts hmetrics reporting, ignoring errors and | ||
retrying reporting, backing off in 10 second increments. | ||
Use this package when you don't care about shutting down them metrics reporting or being notified of any reporting | ||
errors. | ||
Use this package when you don't care about stopping reporting, specifying the | ||
endpoint, or being notified of any reporting errors. | ||
usage: | ||
import ( | ||
_ "github.com/heroku/x/hmetrics/onload" | ||
) | ||
*/ | ||
See github.com/heroku/x/hmetrics documentation for more info about Heroku Go | ||
metrics and advanced usage. | ||
*/ | ||
package onload | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/heroku/x/hmetrics" | ||
) | ||
|
||
const ( | ||
interval = 10 | ||
) | ||
|
||
func init() { | ||
go func() { | ||
hmetrics.Report(context.Background(), nil) | ||
var backoff int64 | ||
for backoff = 1; ; backoff++ { | ||
start := time.Now() | ||
hmetrics.Report(context.Background(), hmetrics.DefaultEndpoint, nil) | ||
if time.Since(start) > 5*time.Minute { | ||
backoff = 1 | ||
} | ||
time.Sleep(time.Duration(backoff*interval) * time.Second) | ||
} | ||
}() | ||
} |