fix: if not kratos context then panic will result (#1338)

* fix: if not kratos context panic

当ctx不是kratos的context,则app.Name()会导致panic
pull/1341/head
Xudong Cai 3 years ago committed by GitHub
parent 3f68c9a9e0
commit 80378ca10d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      middleware/tracing/metadata.go

@ -17,8 +17,10 @@ var _ propagation.TextMapPropagator = Metadata{}
// Inject sets metadata key-values from ctx into the carrier. // Inject sets metadata key-values from ctx into the carrier.
func (b Metadata) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { func (b Metadata) Inject(ctx context.Context, carrier propagation.TextMapCarrier) {
app, _ := kratos.FromContext(ctx) app, ok := kratos.FromContext(ctx)
if ok {
carrier.Set(serviceHeader, app.Name()) carrier.Set(serviceHeader, app.Name())
}
} }
// Extract returns a copy of parent with the metadata from the carrier added. // Extract returns a copy of parent with the metadata from the carrier added.

Loading…
Cancel
Save