-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
461 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
Package otes provides es client with opentracing. | ||
For documentation about es usage, see https://github.com/olivere/elastic | ||
Integration | ||
package otes exports the configuration in the following format: | ||
es: | ||
default: | ||
"url": "http://localhost:9200" | ||
"index": "", | ||
"username": "", | ||
"password": "", | ||
"shards": 0, | ||
"replicas": 0, | ||
"sniff": false, | ||
"healthCheck": false, | ||
Add the es dependency to core: | ||
var c *core.C = core.New() | ||
c.Provide(otes.Providers()) | ||
Then you can invoke etcd from the application. | ||
c.Invoke(func(client *elastic.Client) { | ||
// Do something with es | ||
}) | ||
Sometimes there are valid reasons to connect to more than one es server. Inject | ||
otes.Maker to factory a *elastic.Client with a specific configuration entry. | ||
c.Invoke(function(maker otes.Maker) { | ||
client, err := maker.Make("default") | ||
// do something with client | ||
}) | ||
*/ | ||
package otes |
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,22 @@ | ||
// +build integration | ||
|
||
package otes_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/DoNewsCode/core" | ||
"github.com/DoNewsCode/core/otes" | ||
"github.com/olivere/elastic/v7" | ||
) | ||
|
||
func Example() { | ||
c := core.New(core.WithInline("log.level", "none")) | ||
c.ProvideEssentials() | ||
c.Provide(otes.Providers()) | ||
c.Invoke(func(esClient *elastic.Client) { | ||
running := esClient.IsRunning() | ||
fmt.Println(running) | ||
}) | ||
// Output: | ||
// true | ||
} |
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,84 @@ | ||
package otes | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/ext" | ||
) | ||
// hook is borrowed from https://github.com/olivere/elastic/tree/release-branch.v7/trace/opentracing | ||
// under MIT license: https://github.com/olivere/elastic/blob/release-branch.v7/LICENSE | ||
|
||
// Transport for tracing Elastic operations. | ||
type Transport struct { | ||
rt http.RoundTripper | ||
tracer opentracing.Tracer | ||
} | ||
|
||
// Option signature for specifying options, e.g. WithRoundTripper. | ||
type Option func(t *Transport) | ||
|
||
// WithRoundTripper specifies the http.RoundTripper to call | ||
// next after this transport. If it is nil (default), the | ||
// transport will use http.DefaultTransport. | ||
func WithRoundTripper(rt http.RoundTripper) Option { | ||
return func(t *Transport) { | ||
t.rt = rt | ||
} | ||
} | ||
|
||
// WithTracer specifies the opentracing.Tracer to call | ||
// this transport. | ||
func WithTracer(tracer opentracing.Tracer) Option { | ||
return func(t *Transport) { | ||
t.tracer = tracer | ||
} | ||
} | ||
|
||
// NewTransport specifies a transport that will trace Elastic | ||
// and report back via OpenTracing. | ||
func NewTransport(opts ...Option) *Transport { | ||
t := &Transport{} | ||
for _, o := range opts { | ||
o(t) | ||
} | ||
return t | ||
} | ||
|
||
// RoundTrip captures the request and starts an OpenTracing span | ||
// for Elastic PerformRequest operation. | ||
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
span, ctx := opentracing.StartSpanFromContext(req.Context(), "es.query") | ||
req = req.WithContext(ctx) | ||
defer span.Finish() | ||
|
||
ext.Component.Set(span, "github.com/olivere/elastic/v7") | ||
ext.HTTPUrl.Set(span, req.URL.String()) | ||
ext.HTTPMethod.Set(span, req.Method) | ||
ext.PeerHostname.Set(span, req.URL.Hostname()) | ||
ext.PeerPort.Set(span, toUint16(req.URL.Port())) | ||
|
||
var ( | ||
resp *http.Response | ||
err error | ||
) | ||
if t.rt != nil { | ||
resp, err = t.rt.RoundTrip(req) | ||
} else { | ||
resp, err = http.DefaultTransport.RoundTrip(req) | ||
} | ||
if err != nil { | ||
ext.Error.Set(span, true) | ||
} | ||
if resp != nil { | ||
ext.HTTPStatusCode.Set(span, uint16(resp.StatusCode)) | ||
} | ||
|
||
return resp, err | ||
} | ||
|
||
func toUint16(s string) uint16 { | ||
v, _ := strconv.ParseUint(s, 10, 16) | ||
return uint16(v) | ||
} |
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,39 @@ | ||
package otes | ||
|
||
import ( | ||
"fmt" | ||
"github.com/go-kit/kit/log" | ||
"github.com/go-kit/kit/log/level" | ||
) | ||
|
||
//LogAdapter is an adapter between kitlog and es Logger interface | ||
|
||
//info logs informational messages | ||
type esInfoLogAdapter struct { | ||
logger log.Logger | ||
} | ||
|
||
// Printf implements elastic.Logger | ||
func (l esInfoLogAdapter) Printf(msg string, v ...interface{}) { | ||
level.Info(l.logger).Log("msg", fmt.Sprintf(msg, v...)) | ||
} | ||
|
||
//error logs to the error log | ||
type esErrorLogAdapter struct { | ||
logger log.Logger | ||
} | ||
|
||
// Printf implements elastic.Logger | ||
func (l esErrorLogAdapter) Printf(msg string, v ...interface{}) { | ||
level.Error(l.logger).Log("msg", fmt.Sprintf(msg, v...)) | ||
} | ||
|
||
//trace log for debugging | ||
type esTraceLogAdapter struct { | ||
logger log.Logger | ||
} | ||
|
||
// Printf implements elastic.Logger | ||
func (l esTraceLogAdapter) Printf(msg string, v ...interface{}) { | ||
level.Debug(l.logger).Log("msg", fmt.Sprintf(msg, v...)) | ||
} |
Oops, something went wrong.