|
| 1 | +// <copyright file="ClientChannelInstrumentation.cs" company="OpenTelemetry Authors"> |
| 2 | +// Copyright The OpenTelemetry Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.Diagnostics; |
| 19 | +using System.ServiceModel.Channels; |
| 20 | +using OpenTelemetry.Context.Propagation; |
| 21 | +using OpenTelemetry.Internal; |
| 22 | +using OpenTelemetry.Trace; |
| 23 | + |
| 24 | +namespace OpenTelemetry.Instrumentation.Wcf.Implementation; |
| 25 | + |
| 26 | +internal static class ClientChannelInstrumentation |
| 27 | +{ |
| 28 | + public static RequestTelemetryState BeforeSendRequest(Message request, Uri remoteChannelAddress) |
| 29 | + { |
| 30 | + if (!ShouldInstrumentRequest(request)) |
| 31 | + { |
| 32 | + return new RequestTelemetryState { SuppressionScope = SuppressDownstreamInstrumentation() }; |
| 33 | + } |
| 34 | + |
| 35 | + Activity activity = WcfInstrumentationActivitySource.ActivitySource.StartActivity( |
| 36 | + WcfInstrumentationActivitySource.OutgoingRequestActivityName, |
| 37 | + ActivityKind.Client); |
| 38 | + IDisposable suppressionScope = SuppressDownstreamInstrumentation(); |
| 39 | + |
| 40 | + if (activity != null) |
| 41 | + { |
| 42 | + var action = string.Empty; |
| 43 | + if (!string.IsNullOrEmpty(request.Headers.Action)) |
| 44 | + { |
| 45 | + action = request.Headers.Action; |
| 46 | + activity.DisplayName = action; |
| 47 | + } |
| 48 | + |
| 49 | + Propagators.DefaultTextMapPropagator.Inject( |
| 50 | + new PropagationContext(activity.Context, Baggage.Current), |
| 51 | + request, |
| 52 | + WcfInstrumentationActivitySource.MessageHeaderValueSetter); |
| 53 | + |
| 54 | + if (activity.IsAllDataRequested) |
| 55 | + { |
| 56 | + activity.SetTag(WcfInstrumentationConstants.RpcSystemTag, WcfInstrumentationConstants.WcfSystemValue); |
| 57 | + |
| 58 | + var actionMetadata = GetActionMetadata(request, action); |
| 59 | + activity.SetTag(WcfInstrumentationConstants.RpcServiceTag, actionMetadata.ContractName); |
| 60 | + activity.SetTag(WcfInstrumentationConstants.RpcMethodTag, actionMetadata.OperationName); |
| 61 | + |
| 62 | + if (WcfInstrumentationActivitySource.Options.SetSoapMessageVersion) |
| 63 | + { |
| 64 | + activity.SetTag(WcfInstrumentationConstants.SoapMessageVersionTag, request.Version.ToString()); |
| 65 | + } |
| 66 | + |
| 67 | + var remoteAddressUri = request.Headers.To ?? remoteChannelAddress; |
| 68 | + if (remoteAddressUri != null) |
| 69 | + { |
| 70 | + activity.SetTag(WcfInstrumentationConstants.NetPeerNameTag, remoteAddressUri.Host); |
| 71 | + activity.SetTag(WcfInstrumentationConstants.NetPeerPortTag, remoteAddressUri.Port); |
| 72 | + activity.SetTag(WcfInstrumentationConstants.WcfChannelSchemeTag, remoteAddressUri.Scheme); |
| 73 | + activity.SetTag(WcfInstrumentationConstants.WcfChannelPathTag, remoteAddressUri.LocalPath); |
| 74 | + } |
| 75 | + |
| 76 | + if (request.Properties.Via != null) |
| 77 | + { |
| 78 | + activity.SetTag(WcfInstrumentationConstants.SoapViaTag, request.Properties.Via.ToString()); |
| 79 | + } |
| 80 | + |
| 81 | + try |
| 82 | + { |
| 83 | + WcfInstrumentationActivitySource.Options.Enrich?.Invoke(activity, WcfEnrichEventNames.BeforeSendRequest, request); |
| 84 | + } |
| 85 | + catch (Exception ex) |
| 86 | + { |
| 87 | + WcfInstrumentationEventSource.Log.EnrichmentException(ex); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return new RequestTelemetryState |
| 93 | + { |
| 94 | + SuppressionScope = suppressionScope, |
| 95 | + Activity = activity, |
| 96 | + }; |
| 97 | + } |
| 98 | + |
| 99 | + public static void AfterReceiveReply(Message reply, RequestTelemetryState state) |
| 100 | + { |
| 101 | + Guard.ThrowIfNull(state); |
| 102 | + |
| 103 | + state.SuppressionScope?.Dispose(); |
| 104 | + |
| 105 | + if (state.Activity is Activity activity) |
| 106 | + { |
| 107 | + if (activity.IsAllDataRequested) |
| 108 | + { |
| 109 | + if (reply == null || reply.IsFault) |
| 110 | + { |
| 111 | + activity.SetStatus(Status.Error); |
| 112 | + } |
| 113 | + |
| 114 | + if (reply != null) |
| 115 | + { |
| 116 | + activity.SetTag(WcfInstrumentationConstants.SoapReplyActionTag, reply.Headers.Action); |
| 117 | + try |
| 118 | + { |
| 119 | + WcfInstrumentationActivitySource.Options.Enrich?.Invoke(activity, WcfEnrichEventNames.AfterReceiveReply, reply); |
| 120 | + } |
| 121 | + catch (Exception ex) |
| 122 | + { |
| 123 | + WcfInstrumentationEventSource.Log.EnrichmentException(ex); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + activity.Stop(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static IDisposable SuppressDownstreamInstrumentation() |
| 133 | + { |
| 134 | + return WcfInstrumentationActivitySource.Options?.SuppressDownstreamInstrumentation ?? false |
| 135 | + ? SuppressInstrumentationScope.Begin() |
| 136 | + : null; |
| 137 | + } |
| 138 | + |
| 139 | + private static ActionMetadata GetActionMetadata(Message request, string action) |
| 140 | + { |
| 141 | + ActionMetadata actionMetadata = null; |
| 142 | + if (request.Properties.TryGetValue(TelemetryContextMessageProperty.Name, out object telemetryContextProperty)) |
| 143 | + { |
| 144 | + var actionMappings = (telemetryContextProperty as TelemetryContextMessageProperty)?.ActionMappings; |
| 145 | + if (actionMappings != null && actionMappings.TryGetValue(action, out ActionMetadata metadata)) |
| 146 | + { |
| 147 | + actionMetadata = metadata; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return actionMetadata != null ? actionMetadata : new ActionMetadata |
| 152 | + { |
| 153 | + ContractName = null, |
| 154 | + OperationName = action, |
| 155 | + }; |
| 156 | + } |
| 157 | + |
| 158 | + private static bool ShouldInstrumentRequest(Message request) |
| 159 | + { |
| 160 | + try |
| 161 | + { |
| 162 | + if (WcfInstrumentationActivitySource.Options == null || WcfInstrumentationActivitySource.Options.OutgoingRequestFilter?.Invoke(request) == false) |
| 163 | + { |
| 164 | + WcfInstrumentationEventSource.Log.RequestIsFilteredOut(); |
| 165 | + return false; |
| 166 | + } |
| 167 | + } |
| 168 | + catch (Exception ex) |
| 169 | + { |
| 170 | + WcfInstrumentationEventSource.Log.RequestFilterException(ex); |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + return true; |
| 175 | + } |
| 176 | +} |
0 commit comments