hootsuite / healthchecks

A go implementation of the Health Checks API used for microservice exploration, documentation and monitoring.
Other
132 stars 10 forks source link

Integrate with application that used fasthttp #6

Closed messi655 closed 7 years ago

messi655 commented 7 years ago

Hi team @hootsuiteadmin , I implement a service that use fasthttp, and want to use this healthchecks framework to integrate to my service? How can I do that?

HootAdam commented 7 years ago

Hi @messi655,

To integrate with fasthttp, you need to make a handler func which will do the routing of requests to our internal helper functions for each status route. It should be pretty simple. An example for the standard net/http library is in https://github.com/hootsuite/healthchecks/blob/master/http.go.

I'm not too familiar with fasthttp, but to add another routing framework integration with healthchecks you should do something like this: 1) Provide a handler function to route requests like /status/... to the appropriate helper function in the healthchecks framework. Ex. https://github.com/hootsuite/healthchecks/blob/master/http.go. Since you are doing this for fasthttp, I would do this in a new fasthttp package.
2) Register this new handler function in your app and pass in your StatusEndpoints so that requests get routed to the right /status/... endpoint. Ex. https://github.com/hootsuite/healthchecks#how-to-use-it 3) Verify it all works as it should with tests like this for your implementation: https://github.com/hootsuite/healthchecks/blob/master/http_test.go

Send us a PR and we'll check it out. :) Thanks!

messi655 commented 7 years ago

hi @HootAdam ,

thanks for your feedback. I found another way like this:

`func status() {

DBdriver := GetConfigure("DB_DRIVER")
DBuser := GetConfigure("DB_USER")
DBpassword := GetConfigure("DB_PASSWORD")
DBschema := GetConfigure("DB_SCHEMA")
DBHost := GetConfigure("DB_HOST")
DBPort := GetConfigure("DB_PORT")

db, err := sql.Open(DBdriver, DBuser+":"+DBpassword+"@tcp("+DBHost+":"+DBPort+")/"+DBschema)
if err != nil {
    log.Fatal(err)
}

// Define a StatusEndpoint at '/status/db' for a database dependency
dbs := healthchecks.StatusEndpoint{
    Name:          "The DB",
    Slug:          "db",
    Type:          "internal",
    IsTraversable: false,
    StatusCheck:   sqlsc.SQLDBStatusChecker{DB: db},
    TraverseCheck: nil,
}

org := healthchecks.StatusEndpoint{
    Name:          "Organization Service",
    Slug:          "service-organization",
    Type:          "http",
    IsTraversable: true,
    StatusCheck: httpsc.HttpStatusChecker{
        BaseUrl: "http://localhost:8800",
    },
    TraverseCheck: httpsc.HttpStatusChecker{
        BaseUrl: "http://localhost:8800",
    },
}

// Add all your StatusEndpoints to a slice for your service.
// This will be used to initialize the framework in the next step.
statusEndpoints := []healthchecks.StatusEndpoint{dbs, org}

aboutFilePath := "conf/about.json"
versionFilePath := "conf/version.txt"
customData := make(map[string]interface{})
//mux := http.NewServeMux()

http.Handle("/status/", healthchecks.Handler(statusEndpoints, aboutFilePath, versionFilePath, customData))
log.Println("Listening...")
abc := http.ListenAndServe(":3333", nil)

log.Fatal(abc)

}`

and I use goroutine to integrate to our API. But when I run our api and I access to http://localhost:3333/status/service-organization the output is ["CRIT",{"description":"Organization Service","result":"CRIT","details":"Get http://localhost:8800/status/aggregate: dial tcp: lookup localhost on 127.0.1.1:53: no such host"}] Can you help this? why?

HootAdam commented 7 years ago

The org StatusEndpoint is an example of how you would do downstream checks of another service. Since you probably don't have another service running at localhost:8080, the status check is failing as it should. I would remove this check from your service and only initialize the database one if you have a DB.

vantinhuynh commented 7 years ago

thanks @HootAdam I got it.

HootAdam commented 7 years ago

OK great! @vantinhuynh Are there any other code changes needed to work with fasthttp?

vantinhuynh commented 7 years ago

@HootAdam I did not change to work with fasthttp. I only start on the other port (service port 8800, healthcheck port 3333).

HootAdam commented 7 years ago

@vantinhuynh Thats great! One other question, did you create the about.json file and version.txt file in your service so when you run /status/about you get all the meta data for your service?

Lastly, if you want to add a status check to a dependency, you can always check our shared status checks repository to see if one exists at https://github.com/hootsuite/healthchecks/tree/master/checks. If you create any status checks that you think the community would benefit from, you can always submit them.

Thanks!

vantinhuynh commented 7 years ago

@HootAdam One other question, did you create the about.json file and version.txt file in your service so when you run /status/about you get all the meta data for your service? => Yes, I did. It work perfect.

Thank @HootAdam I will.

HootAdam commented 7 years ago

@vantinhuynh Great. Closing this Issue. If you have any more questions let us know