-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support access to ITracingContext through static property. (#540)
* Support access to ITracingContext through static property. * SkyApm.Utilities.StaticAccessor support net7.0 * Add license to code files of project SkyApm.Utilities.StaticAccessor. * Try resolve ITracingContext from IServiceProvider not necessary in StaticAccessorHostedService.
- Loading branch information
1 parent
30366b7
commit 7e7693d
Showing
8 changed files
with
254 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/SkyApm.Utilities.StaticAccessor/DependencyInjectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Licensed to the SkyAPM under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The SkyAPM 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 SkyApm.Utilities.StaticAccessor; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class DependencyInjectionExtensions | ||
{ | ||
public static IServiceCollection AddSkyAPMStaticAccessor(this IServiceCollection services) | ||
{ | ||
services.AddHostedService<StaticAccessorHostedService>(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Licensed to the SkyAPM under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The SkyAPM 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 SkyApm.Config; | ||
|
||
namespace SkyApm.Utilities.StaticAccessor | ||
{ | ||
internal class NullConfigAccessor : IConfigAccessor | ||
{ | ||
public T Get<T>() where T : class, new() => new T(); | ||
|
||
public T Value<T>(string key, params string[] sections) => default; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Licensed to the SkyAPM under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The SkyAPM 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 SkyApm.Tracing.Segments; | ||
|
||
namespace SkyApm.Utilities.StaticAccessor | ||
{ | ||
public static class NullInstances | ||
{ | ||
public static readonly SegmentSpan SegmentSpan = new SegmentSpan("NULL", SpanType.Local); | ||
|
||
public static readonly SegmentContext SegmentContext = new SegmentContext(string.Empty, string.Empty, false, string.Empty, string.Empty, "NULL", SpanType.Local); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Licensed to the SkyAPM under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The SkyAPM 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 SkyApm.Tracing; | ||
using SkyApm.Tracing.Segments; | ||
|
||
namespace SkyApm.Utilities.StaticAccessor | ||
{ | ||
internal class NullTracingContext : ITracingContext | ||
{ | ||
public SegmentContext CreateEntrySegmentContext(string operationName, ICarrierHeaderCollection carrierHeader, long startTimeMilliseconds = 0) => NullInstances.SegmentContext; | ||
|
||
public SegmentContext CreateExitSegmentContext(string operationName, string networkAddress, ICarrierHeaderCollection carrierHeader = null, long startTimeMilliseconds = 0) => NullInstances.SegmentContext; | ||
|
||
public SegmentContext CreateLocalSegmentContext(string operationName, long startTimeMilliseconds = 0) => NullInstances.SegmentContext; | ||
|
||
public void Release(SegmentContext segmentContext, long endTimeMilliseconds = 0) | ||
{ | ||
|
||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/SkyApm.Utilities.StaticAccessor/SkyApm.Utilities.StaticAccessor.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\..\build\common.props" /> | ||
<PropertyGroup> | ||
<Description>$(Product) instances accessor.</Description> | ||
<AssemblyTitle>$(PackagePrefix).Utilities.StaticAccessor</AssemblyTitle> | ||
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks> | ||
<AssemblyName>$(PackagePrefix).Utilities.StaticAccessor</AssemblyName> | ||
<PackageId>$(PackagePrefix).Utilities.StaticAccessor</PackageId> | ||
<PackageTags>SkyWalking;APM;Diagnostics</PackageTags> | ||
<PackageReleaseNotes> | ||
</PackageReleaseNotes> | ||
<RootNamespace>SkyApm.Utilities.StaticAccessor</RootNamespace> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'"> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.1.32" PrivateAssets="All"/> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'"> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0"/> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'"> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0"/> | ||
</ItemGroup> | ||
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'"> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0"/> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\SkyApm.Abstractions\SkyApm.Abstractions.csproj" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Licensed to the SkyAPM under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The SkyAPM 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 SkyApm.Config; | ||
using SkyApm.Tracing; | ||
|
||
namespace SkyApm.Utilities.StaticAccessor | ||
{ | ||
public static class SkyApmInstances | ||
{ | ||
public static ITracingContext TracingContext { get; internal set; } = new NullTracingContext(); | ||
|
||
public static IConfigAccessor ConfigAccessor { get; internal set; } = new NullConfigAccessor(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/SkyApm.Utilities.StaticAccessor/StaticAccessorHostedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to the SkyAPM under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The SkyAPM 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 Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using SkyApm.Config; | ||
using SkyApm.Tracing; | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SkyApm.Utilities.StaticAccessor | ||
{ | ||
internal class StaticAccessorHostedService : IHostedService | ||
{ | ||
private readonly ITracingContext _tracingContext; | ||
private readonly IConfigAccessor _configAccessor; | ||
|
||
public StaticAccessorHostedService(IServiceProvider provider) | ||
{ | ||
_tracingContext = provider.GetService<ITracingContext>(); | ||
_configAccessor = provider.GetService<IConfigAccessor>(); | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
if (_tracingContext != null && _configAccessor != null) | ||
{ | ||
SkyApmInstances.TracingContext = _tracingContext; | ||
SkyApmInstances.ConfigAccessor = _configAccessor; | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} | ||
} |