Skip to content

Commit

Permalink
trace context (#65)
Browse files Browse the repository at this point in the history
* trace context

* fix git-secrets

* use opentelemetry version 1.3.1

* update
  • Loading branch information
ShiranAvidov authored Jan 31, 2023
1 parent ad44aab commit 7a2eeb2
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 16 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/git-secrets.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
name: git-secrets

# Controls when the workflow will run
# Triggers the workflow on push or pull request events but only for the main branch
on: [push]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "main"
git-secrets:
# The type of runner that the job will run on
runs-on: ubuntu-18.04

runs-on: ubuntu-22.04
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
- name: Check Out Source Code
uses: actions/checkout@v2

uses: actions/checkout@v3
- name: Set up Python 3.8
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Installing dependencies
run:
sudo apt-get install git less openssh-server
sudo apt-get install less openssh-server
- name: Installing scanning tool
run: |
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
ln -s "$(which echo)" /usr/local/bin/say
brew install git-secrets
git secrets --install
git secrets --register-aws
- name: Running scanning tool
run:
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
git secrets --scan
17 changes: 15 additions & 2 deletions docs/log4net.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Code](#code)
- [Custom Fields](#custom-fields)
- [Extensibility](#extensibility)
- [Trace Context](#trace-context)
- [Code Samples](#code-samples)


Expand Down Expand Up @@ -55,6 +56,8 @@ If you configure your logging in an XML file, simply add a reference to the Logz
with capital letter in Logz.io, set this field to true. The default is false
(first letter will be small letter). -->
<jsonKeysCamelCase>false</jsonKeysCamelCase>
<!-- Add trace context (traceId and spanId) to each log. The default is false -->
<addTraceContext>false</addTraceContext>
</appender>

<root>
Expand All @@ -79,8 +82,9 @@ logzioAppender.AddListenerUrl("<<LISTENER-HOST>>");
// <-- Uncomment these lines to enable gzip compression -->
// logzioAppender.AddGzip(true);
// logzioAppender.ActivateOptions();
// logzioAppender.JsonKeysCamelCase(false)
logzioAppender.ActiveOptions();
// logzioAppender.JsonKeysCamelCase(false);
// logzioAppender.AddTraceContext(false);
logzioAppender.ActivateOptions();
hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Root.Level = Level.All;
hierarchy.Configured = true;
Expand Down Expand Up @@ -120,6 +124,15 @@ public class MyAppLogzioAppender : LogzioAppender

You will then have to change your configuration in order to use your own appender.

## Trace Context

**WARNING**: Does not support .NET Standard 1.3

If you’re sending traces with OpenTelemetry instrumentation (auto or manual), you can correlate your logs with the trace context.
In this way, your logs will have traces data in it: span id and trace id.
To enable this feature, set `<addTraceContext>true</addTraceContext>` in your configuration or `logzioAppender.AddTraceContext(true);`
in your code (as shown in the previews sections).

## Code Samples

### XML
Expand Down
15 changes: 14 additions & 1 deletion docs/loggerFactory.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Code](#code)
- [Custom Fields](#custom-fields)
- [Extensibility](#extensibility)
- [Trace Context](#trace-context)
- [Code Sample](#code-sample)
- [ASP.NET Core](#aspnet-core)
- [.NET Core Desktop Application](#net-core-desktop-application)
Expand Down Expand Up @@ -57,6 +58,8 @@ If you configure your logging in an XML file, simply add a reference to the Logz
<debug>false</debug>
<!-- Set to true if you want json keys in Logz.io to be in camel case. The default is false. -->
<jsonKeysCamelCase>false</jsonKeysCamelCase>
<!-- Add trace context (traceId and spanId) to each log. The default is false -->
<addTraceContext>false</addTraceContext>
</appender>

<root>
Expand All @@ -78,7 +81,8 @@ logzioAppender.AddListenerUrl("<<LISTENER-HOST>>");
// Uncomment these lines to enable gzip compression
// logzioAppender.AddGzip(true);
// logzioAppender.ActivateOptions();
// logzioAppender.JsonKeysCamelCase(false)
// logzioAppender.JsonKeysCamelCase(false);
// logzioAppender.AddTraceContext(false);
logzioAppender.ActiveOptions();
hierarchy.Root.AddAppender(logzioAppender);
hierarchy.Root.Level = Level.All;
Expand Down Expand Up @@ -119,6 +123,15 @@ public class MyAppLogzioAppender : LogzioAppender

You will then have to change your configuration in order to use your own appender.

## Trace Context

**WARNING**: Does not support .NET Standard 1.3

If you’re sending traces with OpenTelemetry instrumentation (auto or manual), you can correlate your logs with the trace context.
In this way, your logs will have traces data in it: span id and trace id.
To enable this feature, set `<addTraceContext>true</addTraceContext>` in your configuration or `logzioAppender.AddTraceContext(true);`
in your code (as shown in the previews sections).

## Code Sample

### ASP.NET Core
Expand Down
13 changes: 12 additions & 1 deletion docs/nlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [Code](#code)
- [Variables](#variables)
- [Extensibility](#extensibility)
- [Trace Context](#trace-context)


Install the NLog target from the Package Manager Console:
Expand Down Expand Up @@ -41,6 +42,7 @@ If you configure your logging in an XML file, you need to register the assembly
useGzip="false"
debug="false"
jsonKeysCamelCase="false"
addTraceContext="false"
<!-- parseJsonMessage="true"-->
>
<contextproperty name="host" layout="${machinename}" />
Expand Down Expand Up @@ -71,6 +73,7 @@ var logzioTarget = new LogzioTarget {
RetriesInterval = TimeSpan.Parse("00:00:02"),
Debug = false,
JsonKeysCamelCase = false,
AddTraceContext = false,
// ParseJsonMessage = true,
// ProxyAddress = "http://your.proxy.com:port"
};
Expand All @@ -91,7 +94,6 @@ for example:
````
Click here for more information about [JsonLayout](https://github.com/NLog/NLog/wiki/JsonLayout).


## Context Properties

You can configure the target to include your own custom values when forwarding to Logzio. For example:
Expand Down Expand Up @@ -129,3 +131,12 @@ public class MyAppLogzioTarget : LogzioTarget
```

You will then have to change your configuration in order to use your own target.

## Trace Context

**WARNING**: Does not support .NET Standard 1.3

If you’re sending traces with OpenTelemetry instrumentation (auto or manual), you can correlate your logs with the trace context.
In this way, your logs will have traces data in it: span id and trace id.
To enable this feature, set `addTraceContext="true"` in your configuration or `AddTraceContext = true`
in your code (as shown in the previews sections).
1 change: 1 addition & 0 deletions src/Core/Shipping/BulkSenderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public class BulkSenderOptions
public string ProxyAddress { get; set; } = String.Empty;
public bool ParseJsonMessage { get; set; } = false;
public bool JsonKeysCamelCase { get; set; } = false;
public bool AddTraceContext { get; set; } = false;
}
}
16 changes: 14 additions & 2 deletions src/Log4netShipper/Log4netShipper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

<Description>log4net appender that sends logs to the logz.io bulk HTTP service</Description>
<Company>Logz.io</Company>
<Authors>Maor Rosenfeld</Authors>
<Authors>Maor Rosenfeld;Shiran Avidov</Authors>
<CurrentYear>$([System.DateTime]::Now.ToString(yyyy))</CurrentYear>
<Copyright>Copyright (c) $(CurrentYear) - Logz.io</Copyright>
<PackageTags>logging;log;logzio;log4net</PackageTags>
<PackageProjectUrl>https://github.com/logzio/logzio-dotnet</PackageProjectUrl>
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/logzio/logzio-dotnet</RepositoryUrl>
<PackageVersion>1.0.13</PackageVersion>
<PackageVersion>1.0.14</PackageVersion>
<PackageReleaseNotes>Option to format message as json</PackageReleaseNotes>
</PropertyGroup>

Expand All @@ -28,6 +28,18 @@
<ProjectReference Include="..\Core\Core.csproj" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="OpenTelemetry" Version="1.3.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="OpenTelemetry" Version="1.3.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="OpenTelemetry" Version="1.3.1" />
</ItemGroup>

<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>
$(TargetsForTfmSpecificBuildOutput);PackReferencedProjectOutputs
Expand Down
20 changes: 20 additions & 0 deletions src/Log4netShipper/LogzioAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using Logzio.DotNet.Core.Shipping;
using Newtonsoft.Json.Linq;

#if !NETSTANDARD1_3
using OpenTelemetry.Trace;
#endif

namespace Logzio.DotNet.Log4net
{
public class LogzioAppender : AppenderSkeleton
Expand Down Expand Up @@ -78,6 +82,17 @@ private void WriteImpl(LoggingEvent loggingEvent)
}
}

#if !NETSTANDARD1_3
if (_shipperOptions.BulkSenderOptions.AddTraceContext)
{
var traceId = Tracer.CurrentSpan.Context.TraceId;
var spanId = Tracer.CurrentSpan.Context.SpanId;

values.Add("traceId", traceId.ToString());
values.Add("spanId", spanId.ToString());
}
#endif

foreach (var customField in _customFields)
{
values[customField.Key] = customField.Value;
Expand Down Expand Up @@ -163,6 +178,11 @@ public void JsonKeysCamelCase(bool value)
_shipperOptions.BulkSenderOptions.JsonKeysCamelCase = value;
}

public void AddTraceContext(bool value)
{
_shipperOptions.BulkSenderOptions.AddTraceContext = value;
}

public void AddBufferTimeout(TimeSpan value)
{
_shipperOptions.BufferTimeLimit = value;
Expand Down
16 changes: 16 additions & 0 deletions src/NLogShipper/LogzioTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using NLog.Config;
using NLog.Targets;

#if !NETSTANDARD1_3
using OpenTelemetry.Trace;
#endif

namespace Logzio.DotNet.NLog
{
[Target("Logzio")]
Expand All @@ -33,6 +37,7 @@ public class LogzioTarget : TargetWithContext

public bool ParseJsonMessage { get { return _shipperOptions.BulkSenderOptions.ParseJsonMessage; } set { _shipperOptions.BulkSenderOptions.ParseJsonMessage = value; } }
public bool JsonKeysCamelCase { get { return _shipperOptions.BulkSenderOptions.JsonKeysCamelCase; } set { _shipperOptions.BulkSenderOptions.JsonKeysCamelCase = value; } }
public bool AddTraceContext { get { return _shipperOptions.BulkSenderOptions.AddTraceContext; } set { _shipperOptions.BulkSenderOptions.AddTraceContext = value; } }

/// <summary>
/// Configuration of additional properties to include with each LogEvent (Ex. ${logger}, ${machinename}, ${threadid} etc.)
Expand Down Expand Up @@ -135,6 +140,17 @@ protected override void Write(LogEventInfo logEvent)
}
}
}

#if !NETSTANDARD1_3
if (AddTraceContext)
{
var traceId = Tracer.CurrentSpan.Context.TraceId;
var spanId = Tracer.CurrentSpan.Context.SpanId;

values.Add("traceId", traceId.ToString());
values.Add("spanId", spanId.ToString());
}
#endif

ExtendValues(logEvent, values);

Expand Down
16 changes: 14 additions & 2 deletions src/NLogShipper/NLogShipper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

<Description>NLog target that sends logs to the logz.io bulk HTTP service</Description>
<Company>Logz.io</Company>
<Authors>Maor Rosenfeld</Authors>
<Authors>Maor Rosenfeld;Shiran Avidov</Authors>
<CurrentYear>$([System.DateTime]::Now.ToString(yyyy))</CurrentYear>
<Copyright>Copyright (c) $(CurrentYear) - Logz.io</Copyright>
<PackageTags>logging;log;logzio;nlog</PackageTags>
<PackageProjectUrl>https://github.com/logzio/logzio-dotnet</PackageProjectUrl>
<PackageLicenseUrl>https://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/logzio/logzio-dotnet</RepositoryUrl>
<PackageVersion>1.0.13</PackageVersion>
<PackageVersion>1.0.14</PackageVersion>
<PackageReleaseNotes>Option to format message as json</PackageReleaseNotes>
</PropertyGroup>

Expand All @@ -28,6 +28,18 @@
<ProjectReference Include="..\Core\Core.csproj" PrivateAssets="all" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="OpenTelemetry" Version="1.3.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="OpenTelemetry" Version="1.3.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="OpenTelemetry" Version="1.3.1" />
</ItemGroup>

<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>
$(TargetsForTfmSpecificBuildOutput);PackReferencedProjectOutputs
Expand Down

0 comments on commit 7a2eeb2

Please sign in to comment.