Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove internal newtonsoft json and move over to system.text.json #2470

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 6 additions & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ csharp_style_var_for_built_in_types = true:error
csharp_style_var_when_type_is_apparent = true:error
csharp_style_var_elsewhere = true:error

csharp_style_expression_bodied_methods = true:error
csharp_style_expression_bodied_constructors = true:error
csharp_style_expression_bodied_operators = true:error
csharp_style_expression_bodied_properties = true:error
csharp_style_expression_bodied_indexers = true:error
csharp_style_expression_bodied_accessors = true:error
csharp_style_expression_bodied_methods = true:warning
csharp_style_expression_bodied_constructors = true:warning
csharp_style_expression_bodied_operators = true:warning
csharp_style_expression_bodied_properties = true:warning
csharp_style_expression_bodied_indexers = true:warning
csharp_style_expression_bodied_accessors = true:warning

# Suggest more modern language features when available
csharp_style_pattern_matching_over_is_with_cast_check = true:error
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ jobs:
runs-on: ubuntu-latest
needs: [ 'format' ]
timeout-minutes: 30

steps:
- uses: actions/checkout@v4
- name: Bootstrap Action Workspace
Expand Down
29 changes: 13 additions & 16 deletions .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,19 @@ env:

jobs:

format:
runs-on: windows-2022
steps:
- uses: actions/checkout@v4
- name: Bootstrap Action Workspace
uses: ./.github/workflows/bootstrap
- name: Format
run: ./build.bat format
# format:
# runs-on: windows-2022
# steps:
# - uses: actions/checkout@v4
# - name: Bootstrap Action Workspace
# uses: ./.github/workflows/bootstrap
# - name: Format
# run: ./build.bat format

#required step
tests:
runs-on: windows-2022
needs: [ 'format' ]
timeout-minutes: 30

steps:
- uses: actions/checkout@v4
- name: Bootstrap Action Workspace
Expand All @@ -61,9 +59,9 @@ jobs:
- name: 'Tests: Unit'
run: ./build.bat test --test-suite unit

integrations-tests:
integration-tests:
runs-on: windows-2022
needs: [ 'format', 'tests' ]
needs: [ 'tests' ]
steps:
- uses: actions/checkout@v4
- name: Bootstrap Action Workspace
Expand All @@ -84,7 +82,7 @@ jobs:
startup-hook-tests:
runs-on: windows-2022
needs: [ 'format', 'tests' ]
needs: [ 'tests' ]
steps:
- uses: actions/checkout@v4
- name: Bootstrap Action Workspace
Expand All @@ -95,7 +93,7 @@ jobs:

profiler-tests:
runs-on: windows-2022
needs: [ 'format', 'tests' ]
needs: [ 'tests' ]
steps:
- uses: actions/checkout@v4
- name: Bootstrap Action Workspace
Expand All @@ -109,8 +107,7 @@ jobs:

test-iis:
runs-on: windows-latest
needs: [ 'format', 'tests' ]

needs: [ 'tests' ]
steps:
- uses: actions/checkout@v4
- name: Bootstrap Action Workspace
Expand Down
20 changes: 13 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageVersion Include="Elastic.CommonSchema.BenchmarkDotNetExporter" Version="1.5.0" />
<PackageVersion Include="Elasticsearch.Net" Version="7.17.5" />
<PackageVersion Include="Elasticsearch.Net.VirtualizedCluster" Version="7.6.1" />
<PackageVersion Include="EntityFramework" Version="6.3.0" PrivateAssets="All" />
<PackageVersion Include="EntityFramework" Version="6.5.1" PrivateAssets="All" />
<PackageVersion Include="Fake.Core.Environment" Version="6.0.0" />
<PackageVersion Include="Fake.Core.SemVer" Version="6.0.0" />
<PackageVersion Include="Fake.DotNet.MsBuild" Version="6.0.0" />
Expand All @@ -38,13 +38,16 @@
<PackageVersion Include="Grpc.Tools" Version="2.62.0" PrivateAssets="All" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageVersion Include="JetBrains.Profiler.Api" Version="1.4.0" />
<PackageVersion Include="JunitXml.TestLogger" Version="4.1.0" PrivateAssets="All" />
<PackageVersion Include="Microsoft.Owin" Version="4.2.2" />
<PackageVersion Include="Microsoft.Owin.Security.Cookies" Version="4.2.2" />
<PackageVersion Include="Microsoft.Web.Infrastructure" Version="2.0.0" />
<PackageVersion Include="MongoDB.Driver" Version="2.28.0" />
<PackageVersion Include="MongoDB.Driver.Core" Version="2.28.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="MySql.Data" Version="8.0.32.1" />
<PackageVersion Include="MySql.Data.EntityFrameworkCore" Version="8.0.22" />
<PackageVersion Include="NEST" Version="7.17.5" />
<PackageVersion Include="NJsonSchema" Version="10.2.2" />
<PackageVersion Include="NJsonSchema" Version="11.1.0" />
<PackageVersion Include="NLog" Version="4.6.8" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Nullean.VsTest.Pretty.TestLogger" Version="0.4.0" PrivateAssets="All" />
Expand All @@ -63,6 +66,8 @@
<PackageVersion Include="SpecFlow.xUnit" Version="3.5.5" />
<PackageVersion Include="StackExchange.Redis" Version="2.7.20" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="System.Memory" Version="4.6.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="Testcontainers.Elasticsearch" Version="4.0.0" />
<PackageVersion Include="Testcontainers.Kafka" Version="4.0.0" />
<PackageVersion Include="Testcontainers.MongoDb" Version="4.0.0" />
Expand All @@ -72,18 +77,19 @@
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.0.0" />
<PackageVersion Include="Testcontainers.RabbitMq" Version="4.0.0" />
<PackageVersion Include="Testcontainers.Redis" Version="4.0.0" />
<PackageVersion Include="WebGrease" Version="1.6.0" />
<PackageVersion Include="YamlDotNet" Version="11.2.1" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="All" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
</ItemGroup>
<!-- Microsoft/System packages -->
<ItemGroup>
<PackageVersion Include="Microsoft.AspNet.Identity.EntityFramework" Version="2.2.3" />
<PackageVersion Include="Microsoft.AspNet.Identity.EntityFramework" Version="2.2.4" />
<PackageVersion Include="Microsoft.AspNet.Identity.Owin" Version="2.2.4" />
<PackageVersion Include="Microsoft.AspNet.Mvc" Version="5.2.4" />
<PackageVersion Include="Microsoft.AspNet.TelemetryCorrelation" Version="1.0.7" />
<PackageVersion Include="Microsoft.AspNet.Mvc" Version="5.3.0" />
<PackageVersion Include="Microsoft.AspNet.TelemetryCorrelation" Version="1.0.8" />
<PackageVersion Include="Microsoft.AspNet.Web.Optimization" Version="1.1.3" />
<PackageVersion Include="Microsoft.AspNet.WebApi" Version="5.2.4" />
<PackageVersion Include="Microsoft.AspNet.WebApi" Version="5.3.0" />
<PackageVersion Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.1.1" />
<PackageVersion Include="Microsoft.AspNetCore.Http" Version="2.1.34" />
<PackageVersion Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
Expand Down Expand Up @@ -118,7 +124,7 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.1" />
<PackageVersion Include="Microsoft.NET.Sdk.Functions" Version="4.6.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Microsoft.Owin.Host.SystemWeb" Version="4.1.1" />
<PackageVersion Include="Microsoft.Owin.Host.SystemWeb" Version="4.2.2" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageVersion Include="Microsoft.Web.Administration" Version="11.1.0" />
<PackageVersion Include="MicrosoftAspNetCore.Http" Version="2.1.22" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Elastic.CommonSchema.BenchmarkDotNetExporter"/>
<PackageReference Include="Newtonsoft.Json" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 3 additions & 2 deletions benchmarks/Elastic.Apm.Benchmarks/SignatureParserBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
using System.Reflection;
using System.Text;
using BenchmarkDotNet.Attributes;
using Elastic.Apm.Libraries.Newtonsoft.Json;
using Elastic.Apm.Libraries.Newtonsoft.Json.Linq;

using Elastic.Apm.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Elastic.Apm.Benchmarks
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<ItemGroup>
<PackageReference Include="NJsonSchema"/>
<PackageReference Include="SharpZipLib"/>
<PackageReference Include="Newtonsoft.Json"/>
</ItemGroup>

<ItemGroup>
Expand Down
24 changes: 5 additions & 19 deletions src/Elastic.Apm.Specification/Validator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Elastic.Apm.Api;
using Elastic.Apm.Api.Constraints;
using Elastic.Apm.Report.Serialization;
using ICSharpCode.SharpZipLib.Tar;
using Elastic.Apm.Libraries.Newtonsoft.Json.Serialization;
using NJsonSchema;

namespace Elastic.Apm.Specification
Expand Down Expand Up @@ -261,30 +261,16 @@ private async Task<TypeValidationResult> ValidateAsync(Type type, string specifi
/// <returns></returns>
private static ImplementationProperty[] GetProperties(Type specType)
{
var resolver = new ElasticApmContractResolver();
JsonObjectContract contract;

try
{
// the json schema may indicate a type is an "object", but the agent may model it in some other way
// e.g. samples on metricset is modelled as a collection. In these scenarios, we won't be dealing with
// an object contract and won't be able to statically determine validity of the type to the schema through reflection.
// The only way to validate these against the schema is to serialize the types.
contract = (JsonObjectContract)resolver.ResolveContract(specType);
}
catch (InvalidCastException e)
{
throw new ContractResolveException(e.Message);
}
var contract = PayloadItemSerializer.Default.GetTypeInfo(specType);

var specProperties = new List<ImplementationProperty>(contract.Properties.Count);
foreach (var jsonProperty in contract.Properties)
{
if (jsonProperty.Ignored)
if (jsonProperty.AttributeProvider!.GetCustomAttributes(typeof(JsonIgnoreAttribute), true).Any())
continue;

var implementationProperty = new ImplementationProperty(jsonProperty.PropertyName, jsonProperty.PropertyType, specType);
var maxLength = (MaxLengthAttribute)jsonProperty.AttributeProvider?.GetAttributes(typeof(MaxLengthAttribute), true).FirstOrDefault();
var implementationProperty = new ImplementationProperty(jsonProperty.Name, jsonProperty.PropertyType, specType);
var maxLength = (MaxLengthAttribute)jsonProperty.AttributeProvider?.GetCustomAttributes(typeof(MaxLengthAttribute), true).FirstOrDefault();

if (maxLength != null)
implementationProperty.MaxLength = maxLength.Length;
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Apm/Api/CapturedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
// See the LICENSE file in the project root for more information

using System.Collections.Generic;
using System.Text.Json.Serialization;
using Elastic.Apm.Api.Constraints;
using Elastic.Apm.Helpers;
using Elastic.Apm.Libraries.Newtonsoft.Json;

namespace Elastic.Apm.Api
{
Expand Down Expand Up @@ -40,7 +40,7 @@ public class CapturedException
/// <summary>
/// Stacktrace information of the captured exception.
/// </summary>
[JsonProperty("stacktrace")]
[JsonPropertyName("stacktrace")]
public List<CapturedStackFrame> StackTrace { get; set; }

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/Elastic.Apm/Api/CapturedStackFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elastic.Apm.Libraries.Newtonsoft.Json;
using System.Text.Json.Serialization;

namespace Elastic.Apm.Api
{
Expand All @@ -11,13 +11,13 @@ public class CapturedStackFrame
/// <summary>
/// The absolute path of the file involved in the stack frame.
/// </summary>
[JsonProperty("abs_path")]
[JsonPropertyName("abs_path")]
public string AbsPath { get; set; }

[JsonProperty("classname")]
[JsonPropertyName("classname")]
public string ClassName { get; set; }

[JsonProperty("filename")]
[JsonPropertyName("filename")]
public string FileName { get; set; }

public string Function { get; set; }
Expand All @@ -26,7 +26,7 @@ public class CapturedStackFrame
/// The line number of code part of the stack frame.
/// Zero value means the actual line number could not have been obtained.
/// </summary>
[JsonProperty("lineno")]
[JsonPropertyName("lineno")]
public int LineNo { get; set; }

public string Module { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/Elastic.Apm/Api/Cloud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Text.Json.Serialization;
using Elastic.Apm.Api.Constraints;
using Elastic.Apm.Libraries.Newtonsoft.Json;

namespace Elastic.Apm.Api
{
Expand All @@ -19,7 +19,7 @@ public class Cloud
public CloudInstance Instance { get; set; }

[MaxLength]
[JsonProperty("availability_zone")]
[JsonPropertyName("availability_zone")]
public string AvailabilityZone { get; set; }

public CloudMachine Machine { get; set; }
Expand Down
32 changes: 17 additions & 15 deletions src/Elastic.Apm/Api/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@

using System;
using System.Collections.Generic;
using Elastic.Apm.Libraries.Newtonsoft.Json;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading;
using Elastic.Apm.Helpers;
using Elastic.Apm.Model;
using Elastic.Apm.Report.Serialization;

namespace Elastic.Apm.Api
{
public class Context
{
private Lazy<Dictionary<string, string>> _custom = new();
private Lazy<Dictionary<string, string>> _custom = new(LazyThreadSafetyMode.ExecutionAndPublication);

[JsonConverter(typeof(CustomJsonConverter))]
public Dictionary<string, string> Custom => _custom.Value;

internal Lazy<LabelsDictionary> InternalLabels = new();
[JsonPropertyName("tags"), JsonInclude]
[JsonConverter(typeof(LabelsJsonConverter))]
internal LabelsDictionary InternalLabels { get; set; } = new();

/// <summary>
/// <seealso cref="ShouldSerializeLabels" />
/// </summary>
[Obsolete(
"Instead of this dictionary, use the `SetLabel` method which supports more types than just string. This property will be removed in a future release.")]
[JsonProperty("tags")]
public Dictionary<string, string> Labels => InternalLabels.Value;
[JsonIgnore]
public Dictionary<string, string> Labels => InternalLabels;

/// <summary>
/// Holds details related to message receiving and publishing if the captured event integrates with a messaging system
Expand Down Expand Up @@ -53,7 +58,7 @@ public class Context
/// Service related information can be sent per event. Provided information will override the more generic information from
/// metadata, non provided fields will be set according to the metadata information.
/// </summary>
[JsonProperty("service")]
[JsonPropertyName("service"), JsonInclude]
internal Service Service { get; set; }

public User User { get; set; }
Expand All @@ -69,15 +74,12 @@ internal Context DeepCopy()
newItem._custom.Value[item.Key] = item.Value;
}

newItem.InternalLabels = new Lazy<LabelsDictionary>();
if (InternalLabels.IsValueCreated)
{
foreach (var item in InternalLabels.Value.InnerDictionary)
newItem.InternalLabels.Value.InnerDictionary[item.Key] = item.Value;
newItem.InternalLabels = new LabelsDictionary();
foreach (var item in InternalLabels.InnerDictionary)
newItem.InternalLabels.InnerDictionary[item.Key] = item.Value;

foreach (var item in InternalLabels.Value)
newItem.InternalLabels.Value[item.Key] = item.Value;
}
foreach (var item in InternalLabels)
newItem.InternalLabels[item.Key] = item.Value;

newItem.Request = Request?.DeepCopy();
newItem.Response = Response?.DeepCopy();
Expand All @@ -90,7 +92,7 @@ internal Context DeepCopy()
/// See
/// <a href="https://www.newtonsoft.com/json/help/html/ConditionalProperties.htm">the relevant Json.NET Documentation</a>
/// </summary>
public bool ShouldSerializeLabels() => InternalLabels.IsValueCreated && InternalLabels.Value.MergedDictionary.Count > 0;
public bool ShouldSerializeLabels() => InternalLabels.MergedDictionary.Count > 0;

public bool ShouldSerializeCustom() => _custom.IsValueCreated && Custom.Count > 0;
}
Expand Down
Loading
Loading