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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 13 additions & 16 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
<PropertyGroup>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>

<ItemGroup>
<GlobalPackageReference Include="MinVer" Version="4.3.0" PrivateAssets="All" />
<GlobalPackageReference Condition="'$(TargetFramework)' == 'net462'" Include="Microsoft.NETFramework.ReferenceAssemblies.net462" Version="1.0.3" PrivateAssets="all"/>
<GlobalPackageReference Condition="'$(TargetFramework)' == 'net462'" Include="Microsoft.NETFramework.ReferenceAssemblies.net462" Version="1.0.3" PrivateAssets="all" />
</ItemGroup>
<!-- Community Packages -->
<ItemGroup>
Expand Down Expand Up @@ -53,17 +52,18 @@
<PackageVersion Include="OpenTelemetry.Extensions.Hosting" Version="1.7.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageVersion Include="Oracle.ManagedDataAccess" Version="21.13.0" />
<PackageVersion Include="Oracle.ManagedDataAccess.Core" Version="3.21.90"/>
<PackageVersion Include="Oracle.ManagedDataAccess.Core" Version="3.21.90" />
<PackageVersion Include="Polly" Version="7.2.1" />
<PackageVersion Include="Proc" Version="0.6.2"/>
<PackageVersion Include="Proc" Version="0.6.2" />
<PackageVersion Include="RabbitMQ.Client" Version="6.8.1" />
<PackageVersion Include="RichardSzalay.MockHttp" Version="6.0.0"/>
<PackageVersion Include="RichardSzalay.MockHttp" Version="6.0.0" />
<PackageVersion Include="SQLite.CodeFirst" Version="1.5.3.29" />
<PackageVersion Include="SharpZipLib" Version="1.3.3" />
<PackageVersion Include="SpecFlow.Tools.MsBuild.Generation" Version="3.5.5"/>
<PackageVersion Include="SpecFlow.xUnit" Version="3.5.5"/>
<PackageVersion Include="SpecFlow.Tools.MsBuild.Generation" Version="3.5.5" />
<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.Text.Json" Version="8.0.5" />
<PackageVersion Include="Testcontainers.Elasticsearch" Version="3.7.0" />
<PackageVersion Include="Testcontainers.Kafka" Version="3.10.0" />
<PackageVersion Include="Testcontainers.MongoDb" Version="3.10.0" />
Expand All @@ -77,7 +77,6 @@
<PackageVersion Include="xunit" Version="2.7.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.5.7" PrivateAssets="All" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
</ItemGroup>

<!-- Microsoft/System packages -->
<ItemGroup>
<PackageVersion Include="Microsoft.AspNet.Identity.EntityFramework" Version="2.2.3" />
Expand All @@ -98,18 +97,17 @@
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.0.0" />
<PackageVersion Include="Microsoft.Azure.DocumentDB.Core" Version="2.22.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker" Version="1.20.0"/>
<PackageVersion Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Core" Version="1.6.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0"/>
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.1"/>
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageVersion Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.1" />
<PackageVersion Include="Microsoft.Azure.ServiceBus" Version="3.0.0" />
<PackageVersion Include="Microsoft.Azure.Storage.Blob" Version="11.2.2" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.1.5" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" PrivateAssets="All"/>
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" PrivateAssets="All" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />

<PackageVersion Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.UserSecrets" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
Expand All @@ -118,7 +116,6 @@
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.0" />

<PackageVersion Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="Microsoft.Owin.Host.SystemWeb" Version="4.1.1" />
Expand All @@ -127,14 +124,14 @@
<PackageVersion Include="MicrosoftAspNetCore.Http" Version="2.1.22" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta1.20427.1" />
<PackageVersion Include="System.Data.SQLite" Version="1.0.112" />
<PackageVersion Include="System.Data.SqlClient" Version="4.8.6"/>
<PackageVersion Include="System.Data.SqlClient" Version="4.8.6" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="5.0.0" />
<PackageVersion Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
<PackageVersion Include="System.IO.FileSystem.AccessControl" Version="4.7.0" />
<PackageVersion Include="System.Reflection.Emit" Version="4.3.0" />
<PackageVersion Include="System.Reflection.Emit.Lightweight" Version="4.3.0" />
<PackageVersion Include="System.Reflection.Metadata" Version="5.0.0" />
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0"/>
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0" />
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="4.9.0" />
<PackageVersion Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>
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
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
4 changes: 2 additions & 2 deletions src/Elastic.Apm/Api/Http.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;
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 All @@ -26,7 +26,7 @@ public class Http
/// </summary>
internal Uri OriginalUrl => _originalUrl;

[JsonProperty("status_code")]
[JsonPropertyName("status_code")]
public int StatusCode { get; set; }

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Elastic.Apm/Api/IError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// 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.Libraries.Newtonsoft.Json;

namespace Elastic.Apm.Api
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public ErrorLog(string message)
/// <summary>
/// The name of the logger instance used.
/// </summary>
[JsonProperty("logger_name")]
[JsonPropertyName("logger_name")]
[MaxLength]
public string LoggerName { get; set; }

Expand All @@ -83,11 +83,11 @@ public ErrorLog(string message)
/// together.
/// The string is not interpreted, so feel free to use whichever placeholders makes sense in the client language."
/// </summary>
[JsonProperty("param_message")]
[JsonPropertyName("param_message")]
[MaxLength]
public string ParamMessage { get; set; }

[JsonProperty("stacktrace")]
[JsonPropertyName("stacktrace")]
public List<CapturedStackFrame> StackTrace { get; set; }
}
}
Loading
Loading