Commit 386123f9 authored by lemon's avatar lemon Committed by 吴晟 Wu Sheng

Init DistributedTraceId (#11)

* Modify PackageTags

* Init commit

* Remove proto files reference

* Migrating some classes from Java

* Init DistributedTraceId
parent f59c03b8
......@@ -7,19 +7,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyWalking.NetworkProtocol"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{05BF0D4E-C824-4EC8-8330-36C1FC49910E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "protos", "protos", "{39479996-FC5A-4A92-BDA1-498648C743D3}"
ProjectSection(SolutionItems) = preProject
protos\ApplicationRegisterService.proto = protos\ApplicationRegisterService.proto
protos\Common.proto = protos\Common.proto
protos\DiscoveryService.proto = protos\DiscoveryService.proto
protos\Downstream.proto = protos\Downstream.proto
protos\JVMMetricsService.proto = protos\JVMMetricsService.proto
protos\KeyWithIntegerValue.proto = protos\KeyWithIntegerValue.proto
protos\KeyWithStringValue.proto = protos\KeyWithStringValue.proto
protos\NetworkAddressRegisterService.proto = protos\NetworkAddressRegisterService.proto
protos\TraceSegmentService.proto = protos\TraceSegmentService.proto
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution items", "{E38F0F6E-6E10-491D-8786-650F6A4B6698}"
ProjectSection(SolutionItems) = preProject
generate_protos.bat = generate_protos.bat
......@@ -29,11 +16,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyWalking.Abstractions", "src\SkyWalking.Abstractions\SkyWalking.Abstractions.csproj", "{C3860B5F-21D0-429F-8A00-B0C0F4573DF7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyWalking.Core", "src\SkyWalking.Core\SkyWalking.Core.csproj", "{FD84A10C-C962-4AEA-BA06-86F08EB048E6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyWalking.Core", "src\SkyWalking.Core\SkyWalking.Core.csproj", "{FD84A10C-C962-4AEA-BA06-86F08EB048E6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{613F0980-91ED-4064-8E8C-168582EF4AD7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SkyWalking.Core.Tests", "test\SkyWalking.Core.Tests\SkyWalking.Core.Tests.csproj", "{3915A2C2-7174-4651-A4F1-D45CD109916A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SkyWalking.Core.Tests", "test\SkyWalking.Core.Tests\SkyWalking.Core.Tests.csproj", "{3915A2C2-7174-4651-A4F1-D45CD109916A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System.Collections.Generic;
using SkyWalking.Dictionary;
namespace SkyWalking.Config
{
/// <summary>
/// The <code>RemoteDownstreamConfig</code> includes configurations from collector side.
/// All of them initialized null, Null-Value or empty collection.
/// </summary>
public static class RemoteDownstreamConfig
{
public static class Agent
{
public static int ApplicationId { get; set; } = DictionaryUtil.NullValue;
public static int ApplicationInstanceId { get; set; } = DictionaryUtil.NullValue;
}
public static class Collector
{
/// <summary>
/// Collector GRPC-Service address.
/// </summary>
public static IList<string> gRPCServers = new List<string>();
}
}
}
using System;
using System.Threading;
namespace SkyWalking.Context
{
/// <summary>
/// Context manager controls the whole context of tracing. Since .NET server application runs as same as Java,
/// We also provide the CONTEXT propagation based on ThreadLocal mechanism.
/// Meaning, each segment also related to singe thread.
/// </summary>
public static class ContextManager
{
private static readonly ThreadLocal<ITracerContext> CONTEXT = new ThreadLocal<ITracerContext>();
private static ITracerContext GetOrCreate(String operationName, bool forceSampling)
{
if (!CONTEXT.IsValueCreated)
{
return null;
}
else
{
return null;
}
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
namespace SkyWalking.Context
{
public static class DateTimeExtensions
{
private static readonly DateTime dtFrom = new DateTime(1970, 1, 1, 0, 0, 0, 0);
public static long GetTimeMillis(this DateTime dateTime)
{
return (dateTime.Ticks - dtFrom.Ticks) / 10000;
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
namespace SkyWalking.Context
{
public interface ITracerContext
{
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using SkyWalking.NetworkProtocol;
namespace SkyWalking.Context.Ids
{
/// <summary>
/// The <code>DistributedTraceId</code> presents a distributed call chain.
/// This call chain has an unique (service) entrance,
/// such as: Service : http://www.skywalking.com/cust/query, all the remote, called behind this service, rest remote,
/// db executions, are using the same <code>DistributedTraceId</code> even in different CLR process.
/// The <code>DistributedTraceId</code> contains only one string, and can NOT be reset, creating a new instance is the only option.
/// </summary>
public abstract class DistributedTraceId : IEquatable<DistributedTraceId>
{
private readonly ID _id;
public DistributedTraceId(ID id)
{
_id = id;
}
public DistributedTraceId(string id)
{
_id = new ID(id);
}
public string Encode
{
get
{
return _id?.Encode;
}
}
public UniqueId ToUniqueId()
{
return _id?.Transform();
}
public bool Equals(DistributedTraceId other)
{
if (other == null)
return false;
return _id != null ? _id.Equals(other._id) : other._id == null;
}
public override bool Equals(object obj)
{
DistributedTraceId id = obj as DistributedTraceId;
return Equals(id);
}
public override int GetHashCode()
{
return _id != null ? _id.GetHashCode() : 0;
}
public override string ToString()
{
return _id?.ToString();
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System.Collections.Generic;
namespace SkyWalking.Context.Ids
{
public class DistributedTraceIds
{
private readonly List<DistributedTraceId> _relatedGlobalTraces;
public DistributedTraceIds()
{
_relatedGlobalTraces = new List<DistributedTraceId>();
}
public IReadOnlyList<DistributedTraceId> GetRelatedGlobalTraces()
{
return _relatedGlobalTraces.AsReadOnly();
}
public void Append(DistributedTraceId distributedTraceId)
{
if (_relatedGlobalTraces.Count > 0 && _relatedGlobalTraces[0] is NewDistributedTraceId)
{
_relatedGlobalTraces.RemoveAt(0);
}
if (!_relatedGlobalTraces.Contains(distributedTraceId))
{
_relatedGlobalTraces.Add(distributedTraceId);
}
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Threading;
using SkyWalking.Config;
using SkyWalking.Dictionary;
namespace SkyWalking.Context.Ids
{
public static class GlobalIdGenerator
{
private static ThreadLocal<IDContext> threadIdSequence = new ThreadLocal<IDContext>(() => new IDContext(DateTime.Now.GetTimeMillis(), 0));
public static ID Generate()
{
if (RemoteDownstreamConfig.Agent.ApplicationId == DictionaryUtil.NullValue)
{
throw new InvalidOperationException();
}
IDContext context = threadIdSequence.Value;
return new ID(
RemoteDownstreamConfig.Agent.ApplicationId,
Thread.CurrentThread.ManagedThreadId,
context.NextSeq()
);
}
private class IDContext
{
private long _lastTimestamp;
private short _threadSeq;
// Just for considering time-shift-back only.
private long _runRandomTimestamp;
private int _lastRandomValue;
private Random _random;
public IDContext(long lastTimestamp, short threadSeq)
{
_lastTimestamp = lastTimestamp;
_threadSeq = threadSeq;
_random = new Random();
}
public long NextSeq()
{
return GetTimestamp() * 10000 + NextThreadSeq();
}
private long GetTimestamp()
{
long currentTimeMillis = DateTime.Now.GetTimeMillis();
if (currentTimeMillis < _lastTimestamp)
{
// Just for considering time-shift-back by Ops or OS. @hanahmily 's suggestion.
if (_runRandomTimestamp != currentTimeMillis)
{
_lastRandomValue = _random.Next();
_runRandomTimestamp = currentTimeMillis;
}
return _lastRandomValue;
}
else
{
_lastTimestamp = currentTimeMillis;
return _lastTimestamp;
}
}
private short NextThreadSeq()
{
if (_threadSeq == 10000)
{
_threadSeq = 0;
}
return _threadSeq++;
}
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using SkyWalking.NetworkProtocol;
namespace SkyWalking.Context.Ids
{
public class ID : IEquatable<ID>
{
private readonly long _part1;
private readonly long _part2;
private readonly long _part3;
private readonly bool _isValid;
private string _encoding;
public bool IsValid
{
get
{
return _isValid;
}
}
public string Encode
{
get
{
if (_encoding == null)
{
_encoding = ToString();
}
return _encoding;
}
}
public ID(long part1, long part2, long part3)
{
_part1 = part1;
_part2 = part2;
_part3 = part3;
_isValid = true;
}
public ID(string encodingString)
{
if (encodingString == null)
{
throw new ArgumentNullException(nameof(encodingString));
}
string[] idParts = encodingString.Split("\\.".ToCharArray(), 3);
for (int part = 0; part < 3; part++)
{
if (part == 0)
{
_isValid = long.TryParse(idParts[part], out _part1);
}
else if (part == 1)
{
_isValid = long.TryParse(idParts[part], out _part2);
}
else
{
_isValid = long.TryParse(idParts[part], out _part3);
}
if (!_isValid)
{
break;
}
}
}
public override string ToString()
{
return $"{_part1}.{_part2}.{_part3}";
}
public override int GetHashCode()
{
int result = (int)(_part1 ^ (_part1 >> 32));
result = 31 * result + (int)(_part2 ^ (_part2 >> 32));
result = 31 * result + (int)(_part3 ^ (_part3 >> 32));
return result;
}
public override bool Equals(object obj)
{
ID id = obj as ID;
return Equals(id);
}
public bool Equals(ID other)
{
if (other == null)
return false;
if (this == other)
return true;
if (_part1 != other._part1)
return false;
if (_part2 != other._part2)
return false;
return _part3 == other._part3;
}
public UniqueId Transform()
{
UniqueId uniqueId = new UniqueId();
uniqueId.IdParts.Add(_part1);
uniqueId.IdParts.Add(_part2);
uniqueId.IdParts.Add(_part3);
return uniqueId;
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace SkyWalking.Context.Ids
{
public class NewDistributedTraceId : DistributedTraceId
{
public NewDistributedTraceId()
:base(GlobalIdGenerator.Generate())
{
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace SkyWalking.Context.Ids
{
/// <summary>
/// The <code>PropagatedTraceId</code> represents a {@link DistributedTraceId}, which is propagated from the peer.
/// </summary>
public class PropagatedTraceId : DistributedTraceId
{
public PropagatedTraceId(string id)
:base(id)
{
}
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
namespace SkyWalking.Dictionary
{
public static class DictionaryUtil
{
public static readonly int NullValue = 0;
public static bool IsNull(int id)
{
return id == NullValue;
}
}
}
......@@ -8,9 +8,13 @@
<TargetFrameworks>netstandard1.6;net45</TargetFrameworks>
<AssemblyName>SkyWalking.Abstractions</AssemblyName>
<PackageId>SkyWalking.Abstractions</PackageId>
<PackageTags>SkyWalking,APM</PackageTags>
<PackageTags>SkyWalking;APM</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<RootNamespace>SkyWalking</RootNamespace>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SkyWalking.NetworkProtocol\SkyWalking.NetworkProtocol.csproj" />
</ItemGroup>
</Project>
......@@ -8,7 +8,7 @@
<TargetFrameworks>netstandard1.6;net45</TargetFrameworks>
<AssemblyName>SkyWalking.Core</AssemblyName>
<PackageId>SkyWalking.Core</PackageId>
<PackageTags>SkyWalking,APM</PackageTags>
<PackageTags>SkyWalking;APM</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<RootNamespace>SkyWalking</RootNamespace>
</PropertyGroup>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment