Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit f2a9092

Browse files
authored
Add public GetSpanFromServerContext() API. (#60)
1 parent 861f45c commit f2a9092

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

opencensus/plugins/grpc/grpc_plugin.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
#define OPENCENSUS_PLUGINS_GRPC_GRPC_PLUGIN_H_
1717

1818
#include "absl/strings/string_view.h"
19+
#include "opencensus/trace/span.h"
20+
21+
namespace grpc {
22+
class ServerContext;
23+
}
1924

2025
namespace opencensus {
2126

@@ -24,6 +29,9 @@ namespace opencensus {
2429
// defined below.
2530
void RegisterGrpcPlugin();
2631

32+
// Returns the tracing Span for the current RPC.
33+
opencensus::trace::Span GetSpanFromServerContext(grpc::ServerContext* context);
34+
2735
// The tag key for the RPC method and status, set for all values recorded for
2836
// the following measures.
2937
extern const absl::string_view kMethodTagKey;

opencensus/plugins/grpc/internal/grpc_plugin.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
// limitations under the License.
1414

1515
#include "opencensus/plugins/grpc/grpc_plugin.h"
16+
17+
#include <grpc++/server_context.h>
18+
1619
#include "opencensus/plugins/grpc/internal/channel_filter.h"
1720
#include "opencensus/plugins/grpc/internal/client_filter.h"
1821
#include "opencensus/plugins/grpc/internal/measures.h"
1922
#include "opencensus/plugins/grpc/internal/server_filter.h"
20-
#include "src/core/lib/surface/channel_init.h"
23+
#include "opencensus/trace/span.h"
2124

2225
namespace opencensus {
2326

@@ -52,6 +55,11 @@ void RegisterGrpcPlugin() {
5255
RpcServerResponseCount();
5356
}
5457

58+
opencensus::trace::Span GetSpanFromServerContext(grpc::ServerContext* context) {
59+
return reinterpret_cast<const CensusContext*>(context->census_context())
60+
->Span();
61+
}
62+
5563
// These measure definitions should be kept in sync across opencensus
5664
// implementations--see
5765
// https://github.com/census-instrumentation/opencensus-java/blob/master/contrib/grpc_metrics/src/main/java/io/opencensus/contrib/grpc/metrics/RpcMeasureConstants.java.

0 commit comments

Comments
 (0)