-
Notifications
You must be signed in to change notification settings - Fork 763
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
Add a separate example project for Logs redaction #3744
Changes from 3 commits
7ae01f0
f2fc2c4
08069ae
c48919f
97d01aa
62770f4
6213601
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// <copyright file="MyClassWithRedactionEnumerator.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed 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. | ||
// </copyright> | ||
|
||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Redaction | ||
{ | ||
internal class MyClassWithRedactionEnumerator : IReadOnlyList<KeyValuePair<string, object>> | ||
{ | ||
private readonly IReadOnlyList<KeyValuePair<string, object>> state; | ||
|
||
public MyClassWithRedactionEnumerator(IReadOnlyList<KeyValuePair<string, object>> state) | ||
{ | ||
this.state = state; | ||
} | ||
|
||
public int Count => this.state.Count; | ||
|
||
public KeyValuePair<string, object> this[int index] | ||
{ | ||
get | ||
{ | ||
var item = this.state[index]; | ||
var entryVal = item.Value; | ||
if (entryVal != null && entryVal.ToString() != null && entryVal.ToString().Contains("<secret>")) | ||
{ | ||
return new KeyValuePair<string, object>(item.Key, "newRedactedValueHere"); | ||
} | ||
|
||
return item; | ||
} | ||
} | ||
|
||
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() | ||
{ | ||
for (var i = 0; i < this.Count; i++) | ||
{ | ||
yield return this[i]; | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return this.GetEnumerator(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// <copyright file="Program.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed 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. | ||
// </copyright> | ||
|
||
using Microsoft.Extensions.Logging; | ||
using OpenTelemetry.Logs; | ||
|
||
namespace Redaction; | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
using var loggerFactory = LoggerFactory.Create(builder => | ||
builder.AddOpenTelemetry(options => | ||
{ | ||
options.AddProcessor(new MyRedactionProcessor()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. processors are run sync.. so it might affect latency if executing complex regex etc. But this might be okay for now. Unless we can show how to do this in Exporter (which wraps real exporter). |
||
options.AddConsoleExporter(); | ||
})); | ||
|
||
var logger = loggerFactory.CreateLogger<Program>(); | ||
|
||
// message will be redacted by MyRedactionProcessor | ||
logger.LogInformation("OpenTelemetry {sensitiveString}.", "<secret>"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Redaction | ||
|
||
This program shows an example of how to redact sensitive information from Logs. | ||
utpilla marked this conversation as resolved.
Show resolved
Hide resolved
|
||
In this example, we attach a custom `Processor` called `MyRedactionProcessor` | ||
which is responsible for replacing any instance of the word "<secret>" | ||
with the value "newRedactedValueHere". |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsLoggingPkgVer)" /> | ||
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.Console\OpenTelemetry.Exporter.Console.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,28 @@ public override ExportResult Export(in Batch<LogRecord> batch) | |
|
||
if (logRecord.State != null) | ||
{ | ||
this.WriteLine($"{"LogRecord.State:",-RightPaddingLength}{logRecord.State}"); | ||
if (logRecord.State is IReadOnlyList<KeyValuePair<string, object>> listKvp) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we may want to do what OTLP exporter does. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||
{ | ||
this.WriteLine("LogRecord.State (Key:Value):"); | ||
for (int i = 0; i < listKvp.Count; i++) | ||
{ | ||
// Special casing {OriginalFormat} | ||
// See https://github.com/open-telemetry/opentelemetry-dotnet/pull/3182 | ||
// for explanation. | ||
var valueToTransform = listKvp[i].Key.Equals("{OriginalFormat}") | ||
? new KeyValuePair<string, object>("OriginalFormat (a.k.a Body)", listKvp[i].Value) | ||
: listKvp[i]; | ||
|
||
if (ConsoleTagTransformer.Instance.TryTransformTag(listKvp[i], out var result)) | ||
{ | ||
this.WriteLine($"{string.Empty,-4}{result}"); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
this.WriteLine($"{"LogRecord.State:",-RightPaddingLength}{logRecord.State}"); | ||
} | ||
} | ||
else if (logRecord.StateValues != null) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider a real world example: input contains email address
david@otel.org
, output will change it to***@otel.org
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably not as straightforward to do without causing allocation.