We can assert that a metric is registered and collected using testutil.CollectAndCount and testutil.CollectAndCompare etc. But is there a way to collect the metrics by metric name and the labels if it's CounterVec.
for reference
https://godoc.org/github.com/prometheus/client_golang/prometheus/testutil
As I understood your question, you want to test the value of a metric with a specific label from a metrics collection like CounterVec.
You can do so by using the ToFloat64 function in combination with the WithLabelsValue function, as in the following example:
import (
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
)
func TestVecMetricT(t *testing.T) {
assert := assert.New(t)
var C = prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "C",
Help: "Help",
}, []string{"subname"},
)
prometheus.MustRegister(C)
C.WithLabelValues("firstLabel").Inc()
C.WithLabelValues("secondLabel").Inc()
C.WithLabelValues("thirdLabel").Inc()
C.WithLabelValues("thirdLabel").Inc()
// collected three metrics
assert.Equal(3, testutil.CollectAndCount(C))
// check the expected values using the ToFloat64 function
assert.Equal(float64(1), testutil.ToFloat64(C.WithLabelValues("firstLabel")))
assert.Equal(float64(1), testutil.ToFloat64(C.WithLabelValues("secondLabel")))
assert.Equal(float64(2), testutil.ToFloat64(C.WithLabelValues("thirdLabel")))
}
Correct me if I'm wrong, but I don't think there a way to use the testutil package to get a slice of label values from a metric collection like CounterVec.
Related
essentially, I'm trying to build capture groups in golang. I'm utilizing the following web page, which seems to indicate that this should work properly as I've written it
For random reasons this is time sensitive, I'm sure you can sympathize
package main
import (
"fmt"
"regexp"
)
func main() {
var r = regexp.MustCompile(`/number(?P<value>.*?)into|field(?P<field>.*?)of|type(?P<type>.*?)$/g`)
fmt.Printf("%#v\n", r.FindStringSubmatch(`cannot unmarshal number 400.50 into Go struct field MyStruct.Numbers of type int64`))
fmt.Printf("%#v\n", r.SubexpNames())
}
This of course produces a result that I don't expect, which is inconsistent with the results on the regex builder website. This is probably because it was built for use with a different language, but I'm ignorant of another website that is more suited for golang that also supports building capture groups, and could use an assist on this one, as it's out of my usual wheelhouse.
the output of the above code using the regex format I have provided is
[]string{"field", "", "", ""}
[]string{"", "value", "field", "type"}
I'd love for it to be as close as possible to
[]string{"field", "cannot unmarshal number (number)", "into go struct (Mystruct.Numbers)", "of type (int64)"}
[]string{"", "value", "field", "type"}
just as it shows on the regex scratchpad above.
It would also be convenient to only match the first instance that matches.
This looks like an XY Problem.
Extract the data directly from the json.UnmarshalTypeError instead of parsing the string representation of the error.
This program:
var v MyStruct
err := json.Unmarshal([]byte(`{"numbers": 400.50}`), &v)
if e, ok := err.(*json.UnmarshalTypeError); ok {
fmt.Printf("Value: %s\nStruct.Field: %s\nType: %s\n",
e.Value, e.Struct+"."+e.Field, e.Type)
}
prints the output:
Value: number 400.50
Struct.Field: MyStruct.Numbers
Type: int64
Run it on the Go playground.
Using example golang gin code from README:
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl",
gin.H{
"foo": "bar",
})
}
}
// in template index.tmpl
<script>
{{.foo}}
</script>
// result in html
<script>
"bar"
</script>
But how can I get it without the quotes, I need just bar vs "bar"?
the template package implements an HTML context aware engine to provide injection safe html.
In other words it knows it executes inside a script tag, thus it does not output raw string but json encoded strings compatible with js.
To fix it, unlike the comment suggests, make the string a template.JS value and the security measures will not attempt to protect the strings.
ref
- https://golang.org/pkg/html/template/
Package template (html/template) implements data-driven templates for
generating HTML output safe against code injection.
https://golang.org/pkg/html/template/#JS
Use of this type presents a security risk: the encapsulated content
should come from a trusted source, as it will be included verbatim in
the template output.
package main
import (
"html/template"
"os"
)
func main() {
c := `<script>
{{.foo}}
{{.oof}}
</script>`
d := map[string]interface{}{"foo": "bar", "oof": template.JS("rab")}
template.Must(template.New("").Parse(c)).Execute(os.Stdout, d)
}
https://play.golang.org/p/6qLnc9ALCeC
In my app I have a sidebar with a list of "saved searches" and a central area that should show the results of a search. Whenever I click on a saved search link, I want to update the central area with the results of that search.
What is the proper way to do this with apollo-react?
I tried with this:
// SidebarConnector.js:
const withVideoSearch = graphql(
VIDEO_SEARCH_QUERY,
{
name: 'videoSearchQuery',
props: ({ videoSearchQuery }) => {
return ({
searchVideos: videoSearchQuery.refetch,
});
},
}
);
export default withVideoSearch(Sidebar);
My saved searches are doing a searchVideos({ query: "some query" }) on click which, based on the above, is doing a refetch for the VIDEO_SEARCH_QUERY query with different variables.
This works fine, the call is made to the graphql server and results are returned just fine.
For the main component that shows the list of results I use:
export default graphql(VIDEO_SEARCH_QUERY)(ResultList);
Initially the main component gets its results from the server as if the query was done without variables which is fine, exactly how I want it.
The problem is that every refetch seems to create a different entry in ROOT_QUERY in apollo's store and my main component is "locked" into the one without variables.
Here's what apollo's store looks like after the initial fetch and one of the refetches triggered from a saved search:
ROOT_QUERY
searchVideos({"query":"coke"}): [Video]
0:▾Video:arLaecAu5ns
searchVideos({"query":null}): [Video]
0:▾Video:3HXg-oVMA0c
So my question is how to either switch the main component to the "current search" or how to overwrite the store on every refresh so that there's only one key so the main component updates correctly.
For completeness here's my VIDEO_SEARCH_QUERY:
export const VIDEO_SEARCH_QUERY = gql`
query searchVideos($query: String) {
searchVideos(query: $query) {
...fVideo
}
}
${fVideo}
`;
Maybe I'm misunderstanding your use case, but it seems like there's no need to utilize refetch here. It would be simpler to persist whatever the selected search string is as state, pass that state down as a prop to your main component and then just use that prop as the variable in your GraphQL request. So the graphql call inside your ResultList component would look something like this:
const options = props => ({ variables: { query: props.searchString } })
export default graphql(VIDEO_SEARCH_QUERY, { options })(ResultList);
Then just have your onClick handler for each saved search set the state to whatever that search string is, and Apollo will do the rest. This is super easy with Redux -- just fire off the appropriate action. If you're not using Redux, you may have to lift the state up so it can then be passed down as a prop, but the concept is the same.
I want to make sure my Go package use var instances provided by a "dal" package and does not accidentally import and use db access packages directly.
I guess I can do regexp search on source but I wonder if there is a way to ensure the rule through standard Go testing?
Just to give an idea what I'm going to do:
Interface package:
package dal
type UserDal interface {
GetUser(id int) User
}
Implementation package:
package dal_db_specific
import (
"some_db"
"dal"
)
type UserDalDbSpecific struct {
}
func (_ UserDalDbSpecific) GetUser(id int) User {
some_db.executeQuery(...)
...
return user
}
register_dal() {
dal.UserDal = UserDalDbSpecific{}
}
User code package:
import (
"dal"
"some_db" <-- Fail here!
)
func someFunc() {
user := dal.User.GetUser(1) // Right way
some_db.DoSomething() <-- Fail here!
}
Slightly more reliable than grep: parse the target source using the standard parser package and inspect the AST. You'd be looking for ImportSpec nodes matching the DB access packages. Fail the test if any are found.
Has anyone tried storing and/or searching on geocodes (e.g. lat/long) in Firebase? This is functionality that is built into MongoDB and as I'm considering using Firebase for our backend instead, I need to know how I'd handle this scenario in Firebase.
Thanks!
The folks at Firebase recently open-sourced a library that allows you to store and query location data in Firebase. In short, this is very easily accomplished because all keys in Firebase are strings, and Firebase has support for startAt() and endAt() queries that allow you to do the appropriate windowing for geohashes and bounding boxes.
For implementation details and usage, check out the live demos, source code, and their blog post on GeoFire.
Hey I just finished building a real time google map using firebase and GeoFire. GeoFire is really cool and easy to use. It allows you to query using lon lat and radius. It returns a key that you can use to query your firebase db. You set the key, while you create the geoFire object, to be whatever you want. It is usually a ref that you can use to get the object that is associated with that distance.
Here is a link to geoFire:
https://github.com/firebase/geofire-js
Here is an example use case:
You have a lon lat, that you got using navigator:
var lon = '123.1232';
var lat = '-123.756';
var user = {
name: 'test user',
longitude: lon,
latitude: lat
}
usersRef.push(user).then(function(response) {
var key = response.key;
var coords = [lon, lat];
geoFire.set(key, coords, function(success){
console.log('User and geofire object has been created');
});
})
Now you can query for the user using:
// Set your current lon lat and radius
var geoQuery = geoFire.query({
center: [latitude, longitude],
radius: radiusKm
});
geoQuery.on('key_entered', function(key, location, distance) {
// You can now get the user using the key
var user = firebaseRefUrl + '/' + key;
// Here you can create an array of users that you can bind to a scope in the controller
});
If you are using google maps. I reccomend you use angular-google-maps.
Its a really cool google maps directive that takes in an array of markers and circles. So when ever $scope.markers or $scope.circles change in the controller it will automatically be applied to the map without any messy code. They have very good documentation.
Here is a link:
http://angular-ui.github.io/angular-google-maps/