コード例 #1
0
        public void Dispose()
        {
            tagsBuilder.Dispose();

            var timestamp = timestampInternal != default
                ? timestampInternal
                : timeProvider();

            using (binaryWriter.JumpTo(timestampPosition))
                binaryWriter.Write(EpochHelper.ToUnixTimeUtcTicks(timestamp.UtcDateTime));
        }
コード例 #2
0
        /// <inheritdoc />
        public async Task <ReadTimelineResult <T> > ReadAsync(ReadTimelineQuery query, TimeSpan timeout, CancellationToken cancellationToken = default)
        {
            try
            {
                var url = new RequestUrlBuilder("timeline/read")
                {
                    { Constants.QueryParameters.Timeline, query.Name },
                    { Constants.QueryParameters.Limit, query.Limit },
                    { Constants.QueryParameters.ClientShard, query.ClientShard },
                    { Constants.QueryParameters.ClientShardCount, query.ClientShardCount },
                    { "from", EpochHelper.ToUnixTimeUtcTicks(query.From.UtcDateTime) },
                    { "to", EpochHelper.ToUnixTimeUtcTicks(query.To.UtcDateTime) }
                }
                .Build();

                var body = CreateRequestBody(query.Coordinates ?? TimelineCoordinates.Empty);

                var request = Request
                              .Post(url)
                              .WithContentTypeHeader(Constants.ContentTypes.OctetStream)
                              .WithContent(body);

                var result = await client
                             .SendAsync(request, timeout, cancellationToken : cancellationToken)
                             .ConfigureAwait(false);

                try
                {
                    var operationStatus = responseAnalyzer.Analyze(result.Response, out var errorMessage);
                    if (operationStatus != HerculesStatus.Success)
                    {
                        return(new ReadTimelineResult <T>(operationStatus, null, errorMessage));
                    }

                    return(new ReadTimelineResult <T>(operationStatus, ParseResponseBody(result.Response)));
                }
                finally
                {
                    if (result.Response.HasContent)
                    {
                        bufferPool.Return(result.Response.Content.Buffer);
                    }
                }
            }
            catch (Exception error)
            {
                log.Error(error);

                return(new ReadTimelineResult <T>(HerculesStatus.UnknownError, null, error.Message));
            }
        }
コード例 #3
0
        public static void Build([NotNull] ISpan span, [NotNull] IHerculesEventBuilder builder, [CanBeNull] IFormatProvider formatProvider = null)
        {
            builder.SetTimestamp(span.EndTimestamp ?? span.BeginTimestamp);

            builder
            .AddValue(TagNames.TraceId, span.TraceId)
            .AddValue(TagNames.SpanId, span.SpanId)
            .AddValue(TagNames.BeginTimestampUtc, EpochHelper.ToUnixTimeUtcTicks(span.BeginTimestamp.UtcDateTime))
            .AddValue(TagNames.BeginTimestampUtcOffset, span.BeginTimestamp.Offset.Ticks);

            if (span.ParentSpanId.HasValue)
            {
                builder.AddValue(TagNames.ParentSpanId, span.ParentSpanId.Value);
            }

            if (span.EndTimestamp.HasValue)
            {
                builder.AddValue(TagNames.EndTimestampUtc, EpochHelper.ToUnixTimeUtcTicks(span.EndTimestamp.Value.UtcDateTime));
                builder.AddValue(TagNames.EndTimestampUtcOffset, span.EndTimestamp.Value.Offset.Ticks);
            }

            builder.AddContainer(TagNames.Annotations, tagBuilder => BuildAnnotationsContainer(tagBuilder, span, formatProvider));
        }
コード例 #4
0
        public void Should_send_correct_binary_data_to_gateway()
        {
            var key1          = "intValue";
            var key2          = "longValue";
            var byteKey1      = Encoding.UTF8.GetBytes(key1);
            var byteKey2      = Encoding.UTF8.GetBytes(key2);
            var value1        = 100500;
            var value2        = (long)1e16 + 5;
            var timestamp     = DateTime.UtcNow;
            var unixTimestamp = EpochHelper.ToUnixTimeUtcTicks(timestamp);

            var size =
                +sizeof(byte)     // protocol version
                + sizeof(long)    // timestamp
                + GuidSize        // guid
                + sizeof(ushort)  // tag count
                + sizeof(byte)    // key length
                + byteKey1.Length // key
                + sizeof(byte)    // type tag
                + sizeof(int)     // value
                + sizeof(byte)    // key length
                + byteKey2.Length // key
                + sizeof(byte)    // type tag
                + sizeof(long);   // value

            const byte EventProtocolVersion = 1;
            const byte IntegerTag           = 4;
            const byte LongTag = 5;

            var writer = new BinaryBufferWriter(size)
            {
                Endianness = Endianness.Big
            };

            writer.Write(EventProtocolVersion);
            writer.Write(unixTimestamp);
            writer.Write(Guid.Empty);

            writer.Write((ushort)2);

            // first tag
            writer.Write((byte)byteKey1.Length);
            writer.WriteWithoutLength(byteKey1);
            writer.Write(IntegerTag);
            writer.Write(value1);

            // second tag
            writer.Write((byte)byteKey2.Length);
            writer.WriteWithoutLength(byteKey2);
            writer.Write(LongTag);
            writer.Write(value2);

            var recordCountContent = new byte[] { 0, 0, 0, 1 };
            var recordContent      = writer.FilledSegment;

            sink.Put(
                "stream",
                builder => builder
                .SetTimestamp(timestamp)
                .AddValue(key1, value1)
                .AddValue(key2, value2));

            new Action(
                () =>
            {
                var content = lastRequest?.Content?.ToArray();
                if (content != null)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    // ReSharper disable once AssignNullToNotNullAttribute
                    var targetLength = int.Parse(lastRequest.Headers[Constants.Compression.OriginalContentLengthHeaderName]);
                    var target       = new byte[targetLength];
                    LZ4Codec.Decode(content, 0, content.Length, target, 0, targetLength);
                    content = target;
                }

                var actualRecordsCountContent = content?.Take(sizeof(int)).ToArray();
                var actualRecordContent       = content?.Skip(sizeof(int)).ToArray();
                actualRecordContent.Should().NotBeNull();
                EraseEventId(actualRecordContent);

                actualRecordsCountContent.Should().BeEquivalentTo(recordCountContent, c => c.WithStrictOrdering());
                actualRecordContent.Should().BeEquivalentTo(recordContent, c => c.WithStrictOrdering());
            }).ShouldPassIn(5.Seconds());
        }