예제 #1
0
        private void StartSpan <TState>(IExecutionSegment currentexecutionSegment, TState state)
        {
            if (state is string)
            {
                currentexecutionSegment.StartSpan(state.ToString(), DefaultSpanType);
            }
            else if (state is IEnumerable <KeyValuePair <string, object> > Properties)
            {
                var propDic = new Dictionary <string, object>();
                foreach (var item in Properties)
                {
                    if (item.Key != "{OriginalFormat}" && !propDic.ContainsKey(item.Key))
                    {
                        propDic.Add(item.Key, item.Value);
                    }
                }

                string apmSpanType = propDic.ContainsKey(nameof(apmSpanType)) ? propDic[nameof(apmSpanType)].ToString() : DefaultSpanType;
                string apmSpanName = propDic.ContainsKey(nameof(apmSpanName)) ? propDic[nameof(apmSpanName)].ToString() : DefaultSpanName;
                currentexecutionSegment.StartSpan(apmSpanName, apmSpanType);

                foreach (var item in Properties)
                {
                    currentexecutionSegment.Labels.Add(item.Key, item.Value.ToString());
                }
            }
        }
        private static async void SendReadToElastic(IExecutionSegment transaction, ElasticDocument message)
        {
            var span =
                transaction.StartSpan("Send message to Elastic", "Elastic");

            try
            {
                var indexSpan = span.StartSpan("Index to Elastic", ApiConstants.TypeDb);
                try
                {
                    var res = await Policy
                              .Handle <ElasticsearchClientException>()
                              .WaitAndRetryAsync(5, i => TimeSpan.FromSeconds(3))
                              .ExecuteAsync(() => ElasticClient.IndexAsync(message,
                                                                           idx => idx.Index(Environment.GetEnvironmentVariable("Elastic_Index_Name"))));
                }
                catch (Exception e)
                {
                    ElasticClient.Index(message, idx => idx.Index($"proof-reads-{Today}"));
                    indexSpan.CaptureException(e, "Index to Elastic");
                }
                finally
                {
                    indexSpan.End();
                }
            }
            catch (Exception e)
            {
                span.CaptureException(e, "SendDataToElastic");
            }
            finally
            {
                span.End();
            }
        }
예제 #3
0
        private void StartSpan <TState>(IExecutionSegment currentExecutionSegment, TState state)
        {
            if (state is string)
            {
                currentExecutionSegment.StartSpan(state.ToString(), ApmOptions.DefaultSpanType);
            }
            else if (state is IEnumerable <KeyValuePair <string, object> > Properties)
            {
                var propDic = new Dictionary <string, object>();
                foreach (var item in Properties)
                {
                    if (!propDic.ContainsKey(item.Key))
                    {
                        propDic.Add(item.Key, item.Value);
                    }
                }

                string apmSpanType = propDic.ContainsKey(nameof(apmSpanType)) ? propDic[nameof(apmSpanType)].ToString() : ApmOptions.DefaultSpanType;
                string apmSpanName = propDic.ContainsKey(nameof(apmSpanName)) ? propDic[nameof(apmSpanName)].ToString() : ApmOptions.DefaultSpanName;
                var    span        = currentExecutionSegment.StartSpan(apmSpanName, apmSpanType);

                AddMetadata(span, propDic);
            }
        }
        private static ElasticDocument DeserializeAndMapData(IExecutionSegment transaction, EventData message)
        {
            var span =
                transaction.StartSpan("Deserialize and Map Data in Message", "Data Transformation");

            try
            {
                var readData = JsonConvert.DeserializeObject <ElasticDocument>(Encoding.UTF8.GetString(message.Body));
                return(readData);
            }
            catch (Exception e)
            {
                span.CaptureException(e, "DeserializeAndMapData");
                return(null);
            }
            finally
            {
                span.End();
            }
        }
        private static async Task <T> PublishMessageAndWait <T>(IMessage message, string destination, bool forceDestination,
                                                                int milliSecondsTimeout, Func <IMessage, string, bool, int, Task <T> > publishAndWait, IExecutionSegment transaction, string name) where T : IMessage
        {
            var span = transaction.StartSpan(name, SpanType);

            try
            {
                var traceParent = $"00-{transaction.TraceId}-{span.Id}-01";
                message.Headers[ApmConstants.TraceParent] = traceParent;
                message.Headers[ApmConstants.ApmTraceAsyncTransaction] = true;
                return(await publishAndWait(message, destination, forceDestination, milliSecondsTimeout));
            }
            catch (Exception ex)
            {
                span.CaptureException(ex);
                throw;
            }
            finally
            {
                span.End();
            }
        }
        private void PublishMessage(IMessage message, string destination, bool forceDestination, Action <IMessage, string, bool> publish,
                                    IExecutionSegment transaction, string name)
        {
            var span = transaction.StartSpan(name, SpanType);

            try
            {
                var traceParent           = $"00-{transaction.TraceId}-{span.Id}-01";
                var traceAsyncTransaction = _config.GetValue <bool>(EnvironmentConstants.ApmTraceAsyncTransaction);
                message.Headers[ApmConstants.TraceParent] = traceParent;
                message.Headers[ApmConstants.ApmTraceAsyncTransaction] = traceAsyncTransaction;
                publish(message, destination, forceDestination);
            }
            catch (Exception ex)
            {
                span.CaptureException(ex);
                throw;
            }
            finally
            {
                span.End();
            }
        }