private void ServiceRemotingClientEvents_SendRequest(object sender, EventArgs e) { try { ServiceRemotingRequestEventArgs eventArgs = e as ServiceRemotingRequestEventArgs; if (eventArgs == null) { ServiceFabricSDKEventSource.Log.InvalidEventArgument((typeof(ServiceRemotingRequestEventArgs)).Name, e.GetType().Name); return; } IService service = (IService)sender; var request = eventArgs.Request; var messageHeaders = request?.GetHeader(); // If there are no header objects passed in, we don't do anything. if (messageHeaders == null) { ServiceFabricSDKEventSource.Log.HeadersNotFound(); return; } var serviceUri = eventArgs.ServiceUri; var methodName = eventArgs.MethodName; // Weird case, just use the numerical id as the method name if (string.IsNullOrEmpty(methodName)) { methodName = messageHeaders.MethodId.ToString(CultureInfo.InvariantCulture); } // Call StartOperation, this will create a new activity with the current activity being the parent. // Since service remoting doesn't really have an URL like HTTP URL, we will do our best approximate that for // the Name, Type, Data, and Target properties var operation = client.StartOperation <DependencyTelemetry>(methodName); operation.Telemetry.Type = ServiceRemotingConstants.ServiceRemotingTypeName; operation.Telemetry.Data = serviceUri.AbsoluteUri + "/" + methodName; operation.Telemetry.Target = serviceUri.AbsoluteUri; pendingTelemetry.Store(request, operation); if (!messageHeaders.TryGetHeaderValue(ServiceRemotingConstants.ParentIdHeaderName, out byte[] parentIdHeaderValue) &&
private void ServiceRemotingServiceEvents_ReceiveRequest(object sender, EventArgs e) { try { ServiceRemotingRequestEventArgs eventArgs = e as ServiceRemotingRequestEventArgs; if (eventArgs == null) { ServiceFabricSDKEventSource.Log.InvalidEventArgument((typeof(ServiceRemotingRequestEventArgs)).Name, e.GetType().Name); return; } var request = eventArgs.Request; var messageHeaders = request?.GetHeader(); // If there are no header objects passed in, we don't do anything. if (messageHeaders == null) { ServiceFabricSDKEventSource.Log.HeadersNotFound(); return; } string methodName = eventArgs.MethodName; // Create and prepare activity and RequestTelemetry objects to track this request. RequestTelemetry rt = new RequestTelemetry(); messageHeaders.TryGetHeaderValue(ServiceRemotingConstants.ParentIdHeaderName, out string parentId); messageHeaders.TryGetHeaderValue(ServiceRemotingConstants.CorrelationContextHeaderName, out byte[] correlationBytes); var baggage = RequestTrackingUtils.DeserializeBaggage(correlationBytes); RequestTrackingUtils.UpdateTelemetryBasedOnCorrelationContext(rt, methodName, parentId, baggage); // Call StartOperation, this will create a new activity with the current activity being the parent. // Since service remoting doesn't really have an URL like HTTP URL, we will do our best approximate that for // the Name, Type, Data, and Target properties var operation = this.client.StartOperation <RequestTelemetry>(rt); RequestTrackingUtils.UpdateCurrentActivityBaggage(baggage); // Handle x-component correlation. if (string.IsNullOrEmpty(rt.Source) && messageHeaders != null) { string telemetrySource = string.Empty; string sourceAppId = null; try { sourceAppId = ServiceRemotingHeaderUtilities.GetRequestContextKeyValue(messageHeaders, ServiceRemotingConstants.RequestContextCorrelationSourceKey); } catch (Exception ex) { ServiceFabricSDKEventSource.Log.GetCrossComponentCorrelationHeaderFailed(ex.ToInvariantString()); } string currentComponentAppId = string.Empty; bool foundMyAppId = false; if (!string.IsNullOrEmpty(rt.Context.InstrumentationKey)) { foundMyAppId = this.correlationIdLookupHelper.TryGetXComponentCorrelationId(rt.Context.InstrumentationKey, out currentComponentAppId); // The startOperation above takes care of setting the right instrumentation key by calling client.Initialize(rt); } // If the source header is present on the incoming request, // and it is an external component (not the same ikey as the one used by the current component), // then populate the source field. if (!string.IsNullOrEmpty(sourceAppId) && foundMyAppId && sourceAppId != currentComponentAppId) { telemetrySource = sourceAppId; } string sourceRoleName = null; try { sourceRoleName = ServiceRemotingHeaderUtilities.GetRequestContextKeyValue(messageHeaders, ServiceRemotingConstants.RequestContextSourceRoleNameKey); } catch (Exception ex) { ServiceFabricSDKEventSource.Log.GetComponentRoleNameHeaderFailed(ex.ToInvariantString()); } if (!string.IsNullOrEmpty(sourceRoleName)) { if (string.IsNullOrEmpty(telemetrySource)) { telemetrySource = "roleName:" + sourceRoleName; } else { telemetrySource += " | roleName:" + sourceRoleName; } } rt.Source = telemetrySource; } pendingTelemetry.Store(request, operation); } catch (Exception ex) { // We failed to read the header or generate activity, let's move on, let's not crash user's application because of that. ServiceFabricSDKEventSource.Log.FailedToHandleEvent("ServiceRemotingServiceEvents.ReceiveRequest", ex.ToInvariantString()); } }