public static Google.Cloud.Trace.V2.AttributeValue ToAttributeValue(this IAttributeValue av)
        {
            var ret = av.Match(
                (s) => new Google.Cloud.Trace.V2.AttributeValue()
            {
                StringValue = new TruncatableString()
                {
                    Value = s
                }
            },
                (b) => new Google.Cloud.Trace.V2.AttributeValue()
            {
                BoolValue = b
            },
                (l) => new Google.Cloud.Trace.V2.AttributeValue()
            {
                IntValue = l
            },
                (d) => new Google.Cloud.Trace.V2.AttributeValue()
            {
                StringValue = new TruncatableString()
                {
                    Value = d.ToString()
                }
            },
                (obj) => new Google.Cloud.Trace.V2.AttributeValue()
            {
                StringValue = new TruncatableString()
                {
                    Value = obj.ToString()
                }
            });

            return(ret);
        }
Exemplo n.º 2
0
 /// <inheritdoc/>
 public virtual void SetAttribute(string key, IAttributeValue value)
 {
     this.SetAttributes(new Dictionary <string, IAttributeValue>()
     {
         { key, value }
     });
 }
Exemplo n.º 3
0
        private void OverwriteSpanKindFromAttribute(IAttributeValue spanKindAttr, ref SpanKind resultKind)
        {
            // override span kind with attribute named span.kind
            if (spanKindAttr != null)
            {
                var kind = spanKindAttr.Match((s) => s, null, null, null, null);

                switch (kind.ToLower(CultureInfo.InvariantCulture))
                {
                case "server":
                    resultKind = SpanKind.Server;
                    break;

                case "client":
                    resultKind = SpanKind.Client;
                    break;

                case "producer":
                    resultKind = SpanKind.Producer;
                    break;

                case "consumer":
                    resultKind = SpanKind.Consumer;
                    break;

                default:
                    resultKind = SpanKind.Internal;
                    break;
                }
            }
        }
Exemplo n.º 4
0
 public virtual void PutAttribute(String key, IAttributeValue value)
 {
     PutAttributes(new Dictionary <string, IAttributeValue>()
     {
         { key, value }
     });
 }
Exemplo n.º 5
0
 private void OverwriteErrorAttribute(IAttributeValue errorAttr, ref bool?success)
 {
     if (errorAttr != null)
     {
         success = errorAttr.Match((s) => !(s == "true"), (b) => !b, null, null, null);
     }
 }
Exemplo n.º 6
0
        /// <inheritdoc/>
        public void SetAttribute(string key, IAttributeValue value)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (!this.IsRecordingEvents)
            {
                return;
            }

            lock (this.@lock)
            {
                if (this.hasBeenEnded)
                {
                    // logger.log(Level.FINE, "Calling putAttributes() on an ended Span.");
                    return;
                }

                this.InitializedAttributes.PutAttribute(key, value);
            }
        }
Exemplo n.º 7
0
        protected override bool SetValueImpl(IAttributeValue value)
        {
            var attribVal = value is AttributeValue
                        ? (AttributeValue)value
                        : new AttributeValue(value);

            return(SetValueInternal(attribVal));
        }
Exemplo n.º 8
0
        private static void VerifyTypeOfAttributeValue(IAttributeValue current, string name)
        {
            Assert.AreEqual(LiteralKind.Type, current.ValueType);
            var refType = current.Value as RDomReferencedType;

            Assert.IsNotNull(refType);
            Assert.AreEqual(name, refType.Name);
        }
Exemplo n.º 9
0
 private static void PutHeadersAttribute(ISpan span, string key, NameValueCollection headers)
 {
     foreach (var header in headers.AllKeys)
     {
         IAttributeValue values = ToCommaDelimitedStringAttribute(headers.GetValues(header));
         span.PutAttribute(key + header, values);
     }
 }
 private string AttributeValueToString(IAttributeValue attributeValue)
 {
     return(attributeValue.Match(
                (arg) => { return arg; },
                (arg) => { return arg.ToString(); },
                (arg) => { return arg.ToString(); },
                (arg) => { return null; }));
 }
        public void LongAttributeValue()
        {
            IAttributeValue <long> attribute = AttributeValue <long> .Create(123456L);

            attribute.Apply <object>((longValue) =>
            {
                Assert.Equal(123456L, longValue);
                return(null);
            });
        }
        public void BooleanAttributeValue()
        {
            IAttributeValue <bool> attribute = AttributeValue <bool> .Create(true);

            attribute.Apply <object>((boolValue) =>
            {
                Assert.True(boolValue);
                return(null);
            });
        }
        public void StringAttributeValue()
        {
            IAttributeValue <string> attribute = AttributeValue <string> .Create("MyStringAttributeValue");

            attribute.Apply <object>((stringValue) =>
            {
                Assert.Equal("MyStringAttributeValue", stringValue);
                return(null);
            });
        }
Exemplo n.º 14
0
 private string AttributeToSimpleString(IAttributeValue value)
 {
     return(value.Match <string>(
                x => x.ToString(),
                x => x ? "true" : "false",
                x => x.ToString(),
                x => x.ToString(),
                x => x.ToString()
                ));
 }
Exemplo n.º 15
0
 public AttributeValue(IAttributeValue attribValue) : this(attribValue.AttributeDefinition)
 {
     if (attribValue.Value != null)
     {
         SetValue(attribValue.Value);
     }
     else if (attribValue.LazyValue != null)
     {
         SetValue(attribValue.LazyValue);
     }
 }
        public void AttributeValue_ToString()
        {
            IAttributeValue <string> attribute = AttributeValue <string> .Create("MyStringAttributeValue");

            Assert.Contains("MyStringAttributeValue", attribute.ToString());
            IAttributeValue <bool> attribute2 = AttributeValue <bool> .Create(true);

            Assert.Contains("True", attribute2.ToString());
            IAttributeValue <long> attribute3 = AttributeValue <long> .Create(123456L);

            Assert.Contains("123456", attribute3.ToString());
        }
Exemplo n.º 17
0
        public void PutAttributeCallsAddAttributesByDefault()
        {
            var mockSpan = new Mock <NoopSpan>(spanContext, spanOptions)
            {
                CallBase = true
            };
            NoopSpan        span = mockSpan.Object;
            IAttributeValue val  = AttributeValue <bool> .Create(true);

            span.PutAttribute("MyKey", val);
            span.End();
            mockSpan.Verify((s) => s.PutAttributes(It.Is <IDictionary <string, IAttributeValue> >((d) => d.ContainsKey("MyKey"))));
        }
        public void PutAttributeCallsAddAttributeByDefault()
        {
            var mockSpan = new Mock <TestSpan>(spanContext, spanOptions)
            {
                CallBase = true
            };
            TestSpan        span = mockSpan.Object;
            IAttributeValue val  = AttributeValue <bool> .Create(true);

            span.SetAttribute("MyKey", val);
            span.End();
            mockSpan.Verify((s) => s.SetAttribute(It.Is <string>((arg) => arg == "MyKey"), It.Is <IAttributeValue>((v) => v == val)));
        }
Exemplo n.º 19
0
        private void InitializeAttributeValue(IAttributeValue newItem,
                                              AttributeArgumentSyntax rawItem, SemanticModel model)
        {
            var tuple = GetAttributeValueName(rawItem);

            newItem.Name  = tuple.Item1;
            newItem.Style = tuple.Item2;
            var tuple2 = GetAttributeValueValue(rawItem, newItem, model);

            newItem.Value = tuple2.Item1;
            newItem.ValueConstantIdentifier = tuple2.Item2;
            newItem.ValueType = tuple2.Item3;
            newItem.Type      = newItem.Value.GetType();
        }
        public bool TrySetValue(IAttributeValue attribValue)
        {
            if (!ValidateCollectionLimits(attribValue.AttributeDefinition.Classification, attribValue.AttributeDefinition.Name))
            {
                return(false);
            }

            if (!SetValueImpl(attribValue))
            {
                return(false);
            }

            IncrementAttribCount(attribValue.AttributeDefinition.Classification);

            return(true);
        }
Exemplo n.º 21
0
        private void OverwriteSpanKindFromAttribute(IAttributeValue spanKindAttr, ref SpanKind resultKind)
        {
            // override span kind with attribute named span.kind
            if (spanKindAttr != null)
            {
                var kind = spanKindAttr.Match((s) => s, null, null, null, null);

                if (kind == "server")
                {
                    resultKind = SpanKind.Server;
                }
                else
                {
                    resultKind = SpanKind.Client;
                }
            }
        }
Exemplo n.º 22
0
        public override void PutAttribute(string key, IAttributeValue value)
        {
            if (!Options.HasFlag(SpanOptions.RECORD_EVENTS))
            {
                return;
            }

            lock (_lock)
            {
                if (hasBeenEnded)
                {
                    //logger.log(Level.FINE, "Calling putAttributes() on an ended Span.");
                    return;
                }
                InitializedAttributes.PutAttribute(key, value);
            }
        }
Exemplo n.º 23
0
        /// <inheritdoc/>
        public override void SetAttribute(string key, IAttributeValue value)
        {
            if (!this.Options.HasFlag(SpanOptions.RecordEvents))
            {
                return;
            }

            lock (this.@lock)
            {
                if (this.hasBeenEnded)
                {
                    // logger.log(Level.FINE, "Calling putAttributes() on an ended Span.");
                    return;
                }

                this.InitializedAttributes.PutAttribute(key, value);
            }
        }
 private bool IsServerSpanKind(IAttributeValue value)
 {
     return(value.Match(
                (arg) =>
     {
         return arg.Equals(SpanAttributeConstants.ServerSpanKind);
     },
                (arg) =>
     {
         return false;
     },
                (arg) =>
     {
         return false;
     },
                (arg) =>
     {
         return false;
     }));
 }
        protected override bool SetValueImpl(IAttributeValue attribVal)
        {
            var attribValTyped = attribVal as AttributeValue;

            if (attribValTyped != null)
            {
                return(SetValueImplInternal(attribValTyped));
            }

            if (attribVal.Value != null)
            {
                return(SetValueImpl(attribVal.AttributeDefinition, attribVal.Value));
            }

            if (attribVal.LazyValue != null)
            {
                return(SetValueImpl(attribVal.AttributeDefinition, attribVal.LazyValue));
            }

            return(false);
        }
Exemplo n.º 26
0
 private static void DrawLuaTable(this IAttributeValue <ILuaTable> props)
 {
     GUILayout.Box(TypeName(props.GetType()) + " " + props.Value.ToString());
     try
     {
         foreach (var key in props.Value.GetKeys())
         {
             GUILayout.BeginHorizontal();
             {
                 var value = props.Value[key];
                 GUILayout.Label(key.ToString(), GUILayout.Width(80));
                 GUILayout.Label(TypeName(value.GetType()), GUILayout.Width(100));
                 GUILayout.Label(value.ToString());
             }
             GUILayout.EndHorizontal();
         }
     }
     catch (Exception e)
     {
         GUILayout.Label(e.ToString());
     }
 }
Exemplo n.º 27
0
 private void VerifyAttributeValue(IAttributeValue attributeValue, string name, object value, LiteralKind kind)
 {
     Assert.AreEqual(name, attributeValue.Name);
     Assert.AreEqual(value, attributeValue.Value);
     Assert.AreEqual(kind, attributeValue.ValueType);
 }
Exemplo n.º 28
0
        public async Task ExportAsync(IEnumerable <ISpanData> spanDataList)
        {
            await Task.Run(async() =>
            {
                foreach (var span in spanDataList)
                {
                    this.ExtractGenericProperties(
                        span,
                        out var resultKind,
                        out var timestamp,
                        out var name,
                        out var resultCode,
                        out var props,
                        out var traceId,
                        out var spanId,
                        out var parentId,
                        out var tracestate,
                        out var success,
                        out var duration);

                    string data      = null;
                    string target    = null;
                    string type      = null;
                    string userAgent = null;

                    IAttributeValue spanKindAttr       = null;
                    IAttributeValue errorAttr          = null;
                    IAttributeValue httpStatusCodeAttr = null;
                    IAttributeValue httpMethodAttr     = null;
                    IAttributeValue httpPathAttr       = null;
                    IAttributeValue httpHostAttr       = null;
                    IAttributeValue httpUrlAttr        = null;
                    IAttributeValue httpUserAgentAttr  = null;
                    IAttributeValue httpRouteAttr      = null;
                    IAttributeValue httpPortAttr       = null;

                    foreach (var attr in span.Attributes.AttributeMap)
                    {
                        var key = attr.Key;

                        switch (attr.Key)
                        {
                        case "span.kind":
                            spanKindAttr = attr.Value;
                            break;

                        case "error":
                            errorAttr = attr.Value;
                            break;

                        case "http.method":
                            httpMethodAttr = attr.Value;
                            break;

                        case "http.path":
                            httpPathAttr = attr.Value;
                            break;

                        case "http.host":
                            httpHostAttr = attr.Value;
                            break;

                        case "http.url":
                            httpUrlAttr = attr.Value;
                            break;

                        case "http.status_code":
                            httpStatusCodeAttr = attr.Value;
                            break;

                        case "http.user_agent":
                            httpUserAgentAttr = attr.Value;
                            break;

                        case "http.route":
                            httpRouteAttr = attr.Value;
                            break;

                        case "http.port":
                            httpPortAttr = attr.Value;
                            break;

                        default:
                            var value = attr.Value.Match <string>(
                                (s) => { return(s); },
                                (b) => { return(b.ToString()); },
                                (l) => { return(l.ToString()); },
                                (d) => { return(d.ToString()); },
                                (obj) => { return(obj.ToString()); });

                            AddPropertyWithAdjustedName(props, attr.Key, value);

                            break;
                        }
                    }

                    var linkId = 0;
                    foreach (var link in span.Links.Links)
                    {
                        AddPropertyWithAdjustedName(props, "link" + linkId + "_traceId", link.TraceId.ToLowerBase16());
                        AddPropertyWithAdjustedName(props, "link" + linkId + "_spanId", link.SpanId.ToLowerBase16());
                        AddPropertyWithAdjustedName(props, "link" + linkId + "_type", link.Type.ToString());

                        foreach (var attr in link.Attributes)
                        {
                            AddPropertyWithAdjustedName(props, "link" + linkId + "_" + attr.Key, attr.Value.Match((s) => s, (b) => b.ToString(), (l) => l.ToString(), (d) => d.ToString(), (obj) => obj.ToString()));
                        }

                        ++linkId;
                    }

                    foreach (var t in span.Events.Events)
                    {
                        var log = new TraceTelemetry(t.Event.Name);

                        if (t.Timestamp != null)
                        {
                            var logTimestamp = DateTimeOffset.FromUnixTimeSeconds(t.Timestamp.Seconds);
                            logTimestamp     = logTimestamp.Add(TimeSpan.FromTicks(t.Timestamp.Nanos / 100));
                            log.Timestamp    = logTimestamp;
                        }

                        foreach (var attr in t.Event.Attributes)
                        {
                            var value = attr.Value.Match <string>(
                                (s) => { return(s); },
                                (b) => { return(b.ToString()); },
                                (l) => { return(l.ToString()); },
                                (d) => { return(d.ToString()); },
                                (obj) => { return(obj.ToString()); });

                            AddPropertyWithAdjustedName(log.Properties, attr.Key, value);
                        }

                        log.Context.Operation.Id       = traceId;
                        log.Context.Operation.ParentId = string.Concat("|", traceId, ".", spanId, ".");

                        this.telemetryClient.Track(log);
                    }

                    this.OverwriteSpanKindFromAttribute(spanKindAttr, ref resultKind);
                    this.OverwriteErrorAttribute(errorAttr, ref success);
                    this.OverwriteFieldsForHttpSpans(
                        httpMethodAttr,
                        httpUrlAttr,
                        httpHostAttr,
                        httpPathAttr,
                        httpStatusCodeAttr,
                        httpUserAgentAttr,
                        httpRouteAttr,
                        httpPortAttr,
                        ref name,
                        ref resultCode,
                        ref data,
                        ref target,
                        ref type,
                        ref userAgent);

                    // BUILDING resulting telemetry
                    OperationTelemetry result;
                    if (resultKind == SpanKind.Client || resultKind == SpanKind.Producer)
                    {
                        var resultD        = new DependencyTelemetry();
                        resultD.ResultCode = resultCode;
                        resultD.Data       = data;
                        resultD.Target     = target;
                        resultD.Type       = type;

                        result = resultD;
                    }
                    else
                    {
                        var resultR          = new RequestTelemetry();
                        resultR.ResponseCode = resultCode;
                        Uri.TryCreate(data, UriKind.RelativeOrAbsolute, out var url);
                        resultR.Url = url;
                        result      = resultR;
                    }

                    result.Success = success;

                    result.Timestamp              = timestamp;
                    result.Name                   = name;
                    result.Context.Operation.Id   = traceId;
                    result.Context.User.UserAgent = userAgent;

                    foreach (var prop in props)
                    {
                        AddPropertyWithAdjustedName(result.Properties, prop.Key, prop.Value);
                    }

                    if (parentId != null)
                    {
                        result.Context.Operation.ParentId = string.Concat("|", traceId, ".", parentId, ".");
                    }

                    // TODO: I don't understant why this concatanation is required
                    result.Id = string.Concat("|", traceId, ".", spanId, ".");

                    foreach (var ts in tracestate.Entries)
                    {
                        result.Properties[ts.Key] = ts.Value;
                    }

                    result.Duration = duration;

                    // TODO: deal with those:
                    // span.ChildSpanCount
                    // span.Context.IsValid;
                    // span.Context.TraceOptions;

                    this.telemetryClient.Track(result);
                }
            });
        }
Exemplo n.º 29
0
        private void OverwriteFieldsForHttpSpans(
            IAttributeValue httpMethodAttr,
            IAttributeValue httpUrlAttr,
            IAttributeValue httpHostAttr,
            IAttributeValue httpPathAttr,
            IAttributeValue httpStatusCodeAttr,
            IAttributeValue httpUserAgentAttr,
            IAttributeValue httpRouteAttr,
            IAttributeValue httpPortAttr,
            ref string name,
            ref string resultCode,
            ref string data,
            ref string target,
            ref string type,
            ref string userAgent)
        {
            if (httpStatusCodeAttr != null)
            {
                resultCode = httpStatusCodeAttr.Match((s) => s, null, (l) => l.ToString(CultureInfo.InvariantCulture), null, null);
                type       = "Http";
            }

            Uri url = null;

            if (httpUrlAttr != null)
            {
                var urlString = httpUrlAttr.Match((s) => s, null, null, null, null);
                Uri.TryCreate(urlString, UriKind.RelativeOrAbsolute, out url);
            }

            string httpMethod = null;
            string httpPath   = null;
            string httpHost   = null;
            string httpRoute  = null;
            string httpPort   = null;

            if (httpMethodAttr != null)
            {
                httpMethod = httpMethodAttr.Match((s) => s, null, null, null, null);
                type       = "Http";
            }

            if (httpPathAttr != null)
            {
                httpPath = httpPathAttr.Match((s) => s, null, null, null, null);
                type     = "Http";
            }

            if (httpHostAttr != null)
            {
                httpHost = httpHostAttr.Match((s) => s, null, null, null, null);
                type     = "Http";
            }

            if (httpUserAgentAttr != null)
            {
                userAgent = httpUserAgentAttr.Match((s) => s, null, null, null, null);
                type      = "Http";
            }

            if (httpRouteAttr != null)
            {
                httpRoute = httpRouteAttr.Match((s) => s, null, null, null, null);
                type      = "Http";
            }

            if (httpRouteAttr != null)
            {
                httpRoute = httpRouteAttr.Match((s) => s, null, null, null, null);
                type      = "Http";
            }

            if (httpPortAttr != null)
            {
                httpPort = httpPortAttr.Match((s) => s, null, (l) => l.ToString(), null, null);
                type     = "Http";
            }

            // restore optional fields when possible
            if ((httpPathAttr == null) && (url != null))
            {
                if (url.IsAbsoluteUri)
                {
                    httpPath = url.LocalPath;
                }
                else
                {
                    int idx = url.OriginalString.IndexOf('?');
                    if (idx != -1)
                    {
                        httpPath = url.OriginalString.Substring(0, idx);
                    }
                    else
                    {
                        httpPath = url.OriginalString;
                    }
                }
            }

            if (url == null)
            {
                string urlString = string.Empty;
                if (!string.IsNullOrEmpty(httpHost))
                {
                    urlString += "https://" + httpHost;

                    if (!string.IsNullOrEmpty(httpPort))
                    {
                        urlString += ":" + httpPort;
                    }
                }

                if (!string.IsNullOrEmpty(httpPath))
                {
                    if (httpPath[0] != '/')
                    {
                        urlString += '/';
                    }

                    urlString += httpPath;
                }

                if (!string.IsNullOrEmpty(urlString))
                {
                    Uri.TryCreate(urlString, UriKind.RelativeOrAbsolute, out url);
                }
            }

            // overwriting
            if (httpPath != null || httpMethod != null || httpRoute != null)
            {
                if (httpRoute != null)
                {
                    name = (httpMethod + " " + httpRoute).Trim();
                }
                else
                {
                    name = (httpMethod + " " + httpPath).Trim();
                }
            }

            if (url != null)
            {
                data = url.ToString();
            }

            if ((url != null) && url.IsAbsoluteUri)
            {
                target = url.Host;
            }
        }
Exemplo n.º 30
0
 public override void PutAttribute(string key, IAttributeValue value)
 {
 }
Exemplo n.º 31
0
 private static void VerifyTypeOfAttributeValue(IAttributeValue current, string name)
 {
     Assert.AreEqual(LiteralKind.Type, current.ValueType);
      var refType = current.Value as RDomReferencedType;
      Assert.IsNotNull(refType);
      Assert.AreEqual(name, refType.Name);
 }
Exemplo n.º 32
0
 private void VerifyAttributeValue(IAttributeValue attributeValue, string name, object value, LiteralKind kind)
 {
     Assert.AreEqual(name, attributeValue.Name);
      Assert.AreEqual(value, attributeValue.Value);
      Assert.AreEqual(kind, attributeValue.ValueType);
 }