Skip to content

Commit

Permalink
cmd: Get trace parent via opentelemetry lib
Browse files Browse the repository at this point in the history
Signed-off-by: Shota Sawada <xiootas@gmail.com>
  • Loading branch information
sawadashota committed Jan 10, 2020
1 parent 813d638 commit 6d6a2b7
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 41 deletions.
35 changes: 16 additions & 19 deletions cmd/server/handler.go
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/rs/cors"
"github.com/spf13/cobra"
"github.com/urfave/negroni"
"go.opentelemetry.io/otel/plugin/httptrace"

"github.com/ory/graceful"
"github.com/ory/hydra/client"
Expand Down Expand Up @@ -155,24 +156,16 @@ func RunServeAll(version, build, date string) func(cmd *cobra.Command, args []st
}

func setTracingLogger(d driver.Driver, logger *negronilogrus.Middleware) {
headers := make(map[string]string)

if d.Configuration().LogHeaderTraceID() != "" {
headers["trace_id"] = d.Configuration().LogHeaderTraceID()
}
if d.Configuration().LogHeaderSpanID() != "" {
headers["span_id"] = d.Configuration().LogHeaderSpanID()
}

if len(headers) == 0 {
return
}

logger.Before = func(entry *logrus.Entry, r *http.Request, _ string) *logrus.Entry {
fields := make(map[string]interface{})
for key, headerKey := range headers {
fields[key] = r.Header.Get(headerKey)
logger.Before = func(entry *logrus.Entry, r *http.Request, remoteAddr string) *logrus.Entry {
_, _, spanCtx := httptrace.Extract(r.Context(), r)
fields := logrus.Fields{
"request": r.RequestURI,
"method": r.Method,
"remote": remoteAddr,
"trace_id": spanCtx.TraceIDString(),
"span_id": spanCtx.SpanIDString(),
}

return entry.WithFields(fields)
}
}
Expand All @@ -199,7 +192,9 @@ func setup(d driver.Driver, cmd *cobra.Command) (admin *x.RouterAdmin, public *x
adminLogger.ExcludeURL(healthx.AliveCheckPath)
adminLogger.ExcludeURL(healthx.ReadyCheckPath)
}
setTracingLogger(d, adminLogger)
if d.Configuration().LogTracingAdmin() {
setTracingLogger(d, adminLogger)
}

adminmw.Use(adminLogger)
adminmw.Use(d.Registry().PrometheusManager())
Expand All @@ -212,7 +207,9 @@ func setup(d driver.Driver, cmd *cobra.Command) (admin *x.RouterAdmin, public *x
publicLogger.ExcludeURL(healthx.AliveCheckPath)
publicLogger.ExcludeURL(healthx.ReadyCheckPath)
}
setTracingLogger(d, publicLogger)
if d.Configuration().LogTracingPublic() {
setTracingLogger(d, publicLogger)
}

publicmw.Use(publicLogger)
publicmw.Use(d.Registry().PrometheusManager())
Expand Down
19 changes: 8 additions & 11 deletions docs/config.yaml
Expand Up @@ -79,17 +79,14 @@ log:
level: info
# Sets the log format. Leave it undefined for text based log format, or set to "json" for JSON formatting.
format: json
# Additional logging from HTTP header
header:
# Logging for traceable logging
# https://github.com/opentracing/specification/blob/master/rfc/trace_identifiers.md
#
# HTTP header key of trace id
# Typically, it is request id generated by caller service.
trace_id: ToTraceID
# HTTP header key of span id
# Typically, it is request id generated by reverse proxy.
span_id: ToSpanID
# Enable traceable logging
# Set HTTP `Traceparent` Header to log tracing id and span id.
# The `Traceparent` header contains the following fields: `version`, `trace-id`, `span-id`, and `trace-options`.
# Example: `Traceparent: 00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00`
# https://github.com/opentracing/specification/blob/master/rfc/trace_identifiers.md
tracing:
public: true
admin: true

# serve controls the configuration for the http(s) daemon(s).
serve:
Expand Down
4 changes: 2 additions & 2 deletions driver/configuration/provider.go
Expand Up @@ -59,8 +59,8 @@ type Provider interface {
LoginURL() *url.URL
LogoutURL() *url.URL
PKCEEnforced() bool
LogHeaderTraceID() string
LogHeaderSpanID() string
LogTracingPublic() bool
LogTracingAdmin() bool
}

func MustValidate(l logrus.FieldLogger, p Provider) {
Expand Down
12 changes: 6 additions & 6 deletions driver/configuration/provider_viper.go
Expand Up @@ -67,8 +67,8 @@ const (
ViperKeyAccessTokenStrategy = "strategies.access_token"
ViperKeySubjectIdentifierAlgorithmSalt = "oidc.subject_identifiers.pairwise.salt"
ViperKeyPKCEEnforced = "oauth2.pkce.enforced"
ViperKeyLogHeaderTraceID = "log.header.trace_id"
ViperKeyLogHeaderSpanID = "log.header.span_id"
ViperKeyLogTracingPublic = "log.tracing.public"
ViperKeyLogTracingAdmin = "log.tracing.admin"
)

func init() {
Expand Down Expand Up @@ -416,10 +416,10 @@ func (v *ViperProvider) PKCEEnforced() bool {
return viperx.GetBool(v.l, ViperKeyPKCEEnforced, false, "OAUTH2_PKCE_ENFORCED")
}

func (v *ViperProvider) LogHeaderTraceID() string {
return viperx.GetString(v.l, ViperKeyLogHeaderTraceID, "", "LOG_HEADER_TRACE_ID")
func (v *ViperProvider) LogTracingPublic() bool {
return viperx.GetBool(v.l, ViperKeyLogTracingPublic, false, "LOG_TRACING_PUBLIC")
}

func (v *ViperProvider) LogHeaderSpanID() string {
return viperx.GetString(v.l, ViperKeyLogHeaderSpanID, "", "LOG_HEADER_SPAN_ID")
func (v *ViperProvider) LogTracingAdmin() bool {
return viperx.GetBool(v.l, ViperKeyLogTracingAdmin, false, "LOG_TRACING_ADMIN")
}
17 changes: 14 additions & 3 deletions go.mod
Expand Up @@ -2,8 +2,11 @@ module github.com/ory/hydra

require (
github.com/Microsoft/go-winio v0.4.12 // indirect
github.com/bombsimon/wsl v1.2.8 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/fatih/color v1.9.0 // indirect
github.com/go-bindata/go-bindata v3.1.1+incompatible
github.com/go-critic/go-critic v0.4.1 // indirect
github.com/go-openapi/errors v0.19.2
github.com/go-openapi/runtime v0.19.5
github.com/go-openapi/strfmt v0.19.3
Expand All @@ -15,19 +18,20 @@ require (
github.com/gobuffalo/packr v1.24.0
github.com/gobwas/glob v0.2.3
github.com/golang/mock v1.3.1
github.com/golangci/golangci-lint v1.22.2 // indirect
github.com/gorilla/sessions v1.1.4-0.20181208214519-12bd4761fc66
github.com/gtank/cryptopasta v0.0.0-20170601214702-1f550f6f2f69
github.com/imdario/mergo v0.0.0-20171009183408-7fe0c75c13ab
github.com/jmoiron/sqlx v1.2.0
github.com/julienschmidt/httprouter v1.2.0
github.com/lib/pq v1.0.0
github.com/lib/pq v1.2.0
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/meatballhat/negroni-logrus v0.0.0-20170801195057-31067281800f
github.com/mendsley/gojwk v0.0.0-20141217222730-4d5ec6e58103
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/oleiade/reflections v1.0.0
github.com/olekukonko/tablewriter v0.0.1
github.com/opentracing/opentracing-go v1.1.0
github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e
github.com/ory/fosite v0.30.2
github.com/ory/go-acc v0.0.0-20181118080137-ddc355013f90
github.com/ory/graceful v0.1.1
Expand All @@ -36,25 +40,32 @@ require (
github.com/ory/viper v1.5.6
github.com/ory/x v0.0.88
github.com/pborman/uuid v1.2.0
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/phayes/freeport v0.0.0-20171002181615-b8543db493a5
github.com/pkg/errors v0.8.1
github.com/pkg/profile v1.3.0 // indirect
github.com/prometheus/client_golang v1.1.0
github.com/rs/cors v1.6.0
github.com/rubenv/sql-migrate v0.0.0-20190212093014-1007f53448d7
github.com/sawadashota/encrypta v0.0.2
github.com/securego/gosec v0.0.0-20200106085552-9cb83e10afad // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v0.0.5
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518
github.com/stretchr/testify v1.4.0
github.com/toqueteos/webbrowser v1.2.0
github.com/uber/jaeger-client-go v2.16.0+incompatible
github.com/uber/jaeger-lib v2.0.0+incompatible // indirect
github.com/urfave/negroni v1.0.0
go.opentelemetry.io/otel v0.2.1
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/tools v0.0.0-20191224055732-dd894d0a8a40
golang.org/x/sys v0.0.0-20200107162124-548cf772de50 // indirect
golang.org/x/tools v0.0.0-20200110042803-e2f26524b78c
gopkg.in/ini.v1 v1.51.1 // indirect
gopkg.in/square/go-jose.v2 v2.3.1
mvdan.cc/unparam v0.0.0-20191111180625-960b1ec0f2c2 // indirect
)

replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110319-2566ecd5d999
Expand Down

0 comments on commit 6d6a2b7

Please sign in to comment.