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

Update more examples to align with the recommended path #4886

Merged
merged 2 commits into from
Sep 27, 2023
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions docs/logs/redaction/MyClassWithRedactionEnumerator.cs

This file was deleted.

45 changes: 42 additions & 3 deletions docs/logs/redaction/MyRedactionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
// limitations under the License.
// </copyright>

using System.Collections;
using OpenTelemetry;
using OpenTelemetry.Logs;

namespace Redaction;

internal class MyRedactionProcessor : BaseProcessor<LogRecord>
internal sealed class MyRedactionProcessor : BaseProcessor<LogRecord>
{
public override void OnEnd(LogRecord logRecord)
{
Expand All @@ -28,4 +27,44 @@ public override void OnEnd(LogRecord logRecord)
logRecord.Attributes = new MyClassWithRedactionEnumerator(logRecord.Attributes);
}
}

internal sealed 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();
}
}
}
32 changes: 18 additions & 14 deletions docs/logs/redaction/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,26 @@
using Microsoft.Extensions.Logging;
using OpenTelemetry.Logs;

namespace Redaction;

public class Program
var loggerFactory = LoggerFactory.Create(builder =>
{
public static void Main()
builder.AddOpenTelemetry(logging =>
{
using var loggerFactory = LoggerFactory.Create(builder =>
builder.AddOpenTelemetry(options =>
{
options.AddProcessor(new MyRedactionProcessor());
options.AddConsoleExporter();
}));
logging.AddProcessor(new MyRedactionProcessor());
logging.AddConsoleExporter();
});
});

var logger = loggerFactory.CreateLogger<Program>();

var logger = loggerFactory.CreateLogger<Program>();
// Message will be redacted by MyRedactionProcessor
logger.FoodPriceChanged("<secret>", 9.99);

// message will be redacted by MyRedactionProcessor
logger.LogInformation("OpenTelemetry {sensitiveString}.", "<secret>");
}
// Dispose logger factory before the application ends.
// This will flush the remaining logs and shutdown the logging pipeline.
loggerFactory.Dispose();

public static partial class ApplicationLogs
{
[LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = "Food `{name}` price changed to `{price}`.")]
public static partial void FoodPriceChanged(this ILogger logger, string name, double price);
}
8 changes: 5 additions & 3 deletions docs/metrics/getting-started-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@
using OpenTelemetry;
using OpenTelemetry.Metrics;

namespace GettingStarted;

public class Program
{
private static readonly Meter MyMeter = new("MyCompany.MyProduct.MyLibrary", "1.0");
private static readonly Counter<long> MyFruitCounter = MyMeter.CreateCounter<long>("MyFruitCounter");

public static void Main()
{
using var meterProvider = Sdk.CreateMeterProviderBuilder()
var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddMeter("MyCompany.MyProduct.MyLibrary")
.AddConsoleExporter()
.Build();
Expand All @@ -38,5 +36,9 @@ public static void Main()
MyFruitCounter.Add(2, new("name", "apple"), new("color", "green"));
MyFruitCounter.Add(5, new("name", "apple"), new("color", "red"));
MyFruitCounter.Add(4, new("name", "lemon"), new("color", "yellow"));

// Dispose meter provider before the application ends.
// This will flush the remaining metrics and shutdown the metrics pipeline.
meterProvider?.Dispose();
}
}
11 changes: 6 additions & 5 deletions docs/trace/getting-started-console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
using OpenTelemetry;
using OpenTelemetry.Trace;

namespace GettingStarted;

public class Program
{
private static readonly ActivitySource MyActivitySource = new(
"MyCompany.MyProduct.MyLibrary");
private static readonly ActivitySource MyActivitySource = new("MyCompany.MyProduct.MyLibrary");

public static void Main()
{
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("MyCompany.MyProduct.MyLibrary")
.AddConsoleExporter()
.Build();
Expand All @@ -39,5 +36,9 @@ public static void Main()
activity?.SetTag("baz", new int[] { 1, 2, 3 });
activity?.SetStatus(ActivityStatusCode.Ok);
}

// Dispose tracer provider before the application ends.
// This will flush the remaining spans and shutdown the tracing pipeline.
tracerProvider?.Dispose();
}
}
Loading