Example #1
0
        public override async ValueTask Invoke(StreamingHubContext context, Func <StreamingHubContext, ValueTask> next)
        {
            // https://github.com/open-telemetry/opentelemetry-specification/blob/master/semantic-conventions.md#grpc

            // span name must be `$package.$service/$method` but MagicOnion has no $package.
            var spanBuilder = tracer.SpanBuilder(context.ServiceContext.CallContext.Method, SpanKind.Server);

            if (sampler != null)
            {
                spanBuilder.SetSampler(sampler);
            }

            using (spanBuilder.StartScopedSpan(out var span))
            {
                try
                {
                    span.SetAttribute("component", "grpc");
                    //span.SetAttribute("request.size", context.GetRawRequest().LongLength);

                    await next(context);

                    //span.SetAttribute("response.size", context.GetRawResponse().LongLength);
                    span.SetAttribute("status_code", (long)context.ServiceContext.CallContext.Status.StatusCode);
                    span.Status = OpenTelemetrygRpcStatusHelper.ConvertStatus(context.ServiceContext.CallContext.Status.StatusCode).WithDescription(context.ServiceContext.CallContext.Status.Detail);
                }
                catch (Exception ex)
                {
                    span.SetAttribute("exception", ex.ToString());

                    span.SetAttribute("status_code", (long)context.ServiceContext.CallContext.Status.StatusCode);
                    span.Status = OpenTelemetrygRpcStatusHelper.ConvertStatus(context.ServiceContext.CallContext.Status.StatusCode).WithDescription(context.ServiceContext.CallContext.Status.Detail);
                    throw;
                }
            }
        }
        public override async ValueTask Invoke(ServiceContext context, Func <ServiceContext, ValueTask> next)
        {
            // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/rpc.md#grpc

            // TracerName must match with ActivitySource name.
            var tracer = tracerProvider.GetTracer(telemetryOption.ActivitySourceName, telemetryOption.TracerVersion);

            // Client -> Server incoming filter
            // span name must be `$package.$service/$method` but MagicOnion has no $package.
            using (var span = tracer.StartSpan($"{context.MethodType}:{context.CallContext.Method}", SpanKind.Server))
            {
                try
                {
                    span.SetAttribute("grpc.method", context.MethodType.ToString());
                    span.SetAttribute("rpc.system", "grpc");
                    span.SetAttribute("rpc.service", context.ServiceType.Name);
                    span.SetAttribute("rpc.method", context.CallContext.Method);
                    // todo: context.CallContext.Peer/Host format is https://github.com/grpc/grpc/blob/master/doc/naming.md and not uri standard.
                    span.SetAttribute("net.peer.name", context.CallContext.Peer);
                    span.SetAttribute("net.host.name", context.CallContext.Host);
                    span.SetAttribute("message.type", "RECIEVED");
                    span.SetAttribute("message.id", context.ContextId.ToString());
                    span.SetAttribute("message.uncompressed_size", context.GetRawRequest()?.LongLength.ToString() ?? "0");

                    // todo: net.peer.name not report on tracer. use custom tag
                    span.SetAttribute("magiconion.peer.ip", context.CallContext.Peer);
                    span.SetAttribute("magiconion.auth.enabled", (!string.IsNullOrEmpty(context.CallContext.AuthContext.PeerIdentityPropertyName)).ToString());
                    span.SetAttribute("magiconion.auth.peer.authenticated", context.CallContext.AuthContext.IsPeerAuthenticated.ToString());

                    await next(context);

                    span.SetAttribute("grpc.status_code", ((long)context.CallContext.Status.StatusCode).ToString());
                    span.Status = OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode);
                }
                catch (Exception ex)
                {
                    span.SetAttribute("exception", ex.ToString());
                    span.SetAttribute("grpc.status_code", ((long)context.CallContext.Status.StatusCode).ToString());
                    span.SetAttribute("grpc.status_detail", context.CallContext.Status.Detail);
                    span.Status = OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode);
                    throw;
                }
            }
        }
Example #3
0
        public override async ValueTask Invoke(ServiceContext context, Func <ServiceContext, ValueTask> next)
        {
            // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/rpc.md#grpc

            // Client -> Server incoming filter
            // span name must be `$package.$service/$method` but MagicOnion has no $package.
            using var activity = source.StartActivity($"{context.MethodType}:{context.CallContext.Method}", ActivityKind.Server);

            // add trace context to service context. it allows user to add their span directly to this context.
            context.SetTraceContext(activity.Context);

            try
            {
                activity.SetTag("grpc.method", context.MethodType.ToString());
                activity.SetTag("rpc.system", "grpc");
                activity.SetTag("rpc.service", context.ServiceType.Name);
                activity.SetTag("rpc.method", context.CallContext.Method);
                // todo: context.CallContext.Peer/Host format is https://github.com/grpc/grpc/blob/master/doc/naming.md and not uri standard.
                activity.SetTag("net.peer.name", context.CallContext.Peer);
                activity.SetTag("net.host.name", context.CallContext.Host);
                activity.SetTag("message.type", "RECIEVED");
                activity.SetTag("message.id", context.ContextId.ToString());
                activity.SetTag("message.uncompressed_size", context.GetRawRequest()?.LongLength.ToString() ?? "0");

                // todo: net.peer.name not report on tracer. use custom tag
                activity.SetTag("magiconion.peer.ip", context.CallContext.Peer);
                activity.SetTag("magiconion.auth.enabled", (!string.IsNullOrEmpty(context.CallContext.AuthContext.PeerIdentityPropertyName)).ToString());
                activity.SetTag("magiconion.auth.peer.authenticated", context.CallContext.AuthContext.IsPeerAuthenticated.ToString());

                await next(context);

                activity.SetTag("grpc.status_code", ((long)context.CallContext.Status.StatusCode).ToString());
                activity.SetStatus(OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode));
            }
            catch (Exception ex)
            {
                activity.SetTag("exception", ex.ToString());
                activity.SetTag("grpc.status_code", ((long)context.CallContext.Status.StatusCode).ToString());
                activity.SetTag("grpc.status_detail", context.CallContext.Status.Detail);
                activity.SetStatus(OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode));
                throw;
            }
        }
Example #4
0
        public override async ValueTask Invoke(ServiceContext context, Func <ServiceContext, ValueTask> next)
        {
            // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/rpc.md#grpc

            // span name must be `$package.$service/$method` but MagicOnion has no $package.
            var tracer = tracerFactcory.GetTracer(context.CallContext.Method);

            // incoming kind: SERVER
            using (tracer.StartActiveSpan($"{context.CallContext.Method}", SpanKind.Server, out var span))
            {
                try
                {
                    span.SetAttribute("rpc.service", serviceName);
                    span.SetAttribute("net.peer.ip", context.CallContext.Peer);
                    span.SetAttribute("net.host.name", context.CallContext.Host);
                    span.SetAttribute("message.type", "RECIEVED");
                    span.SetAttribute("message.id", context.ContextId.ToString());
                    span.SetAttribute("message.uncompressed_size", context.GetRawRequest()?.LongLength ?? 0);

                    span.SetAttribute("magiconion.method.type", context.MethodType.ToString());
                    span.SetAttribute("magiconion.service.type", context.ServiceType.Name);
                    span.SetAttribute("magiconion.auth.enabled", !string.IsNullOrEmpty(context.CallContext.AuthContext.PeerIdentityPropertyName));
                    span.SetAttribute("magiconion.auth.peer.authenticated", context.CallContext.AuthContext.IsPeerAuthenticated);

                    await next(context);

                    span.SetAttribute("grpc.status_code", (long)context.CallContext.Status.StatusCode);
                    span.Status = OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode).WithDescription(context.CallContext.Status.Detail);
                }
                catch (Exception ex)
                {
                    span.SetAttribute("exception", ex.ToString());
                    span.SetAttribute("grpc.status_code", (long)context.CallContext.Status.StatusCode);
                    span.Status = OpenTelemetrygRpcStatusHelper.ConvertStatus(context.CallContext.Status.StatusCode).WithDescription(context.CallContext.Status.Detail);
                    throw;
                }
            }
        }