diff --git a/docs/logs/redaction/MyClassWithRedactionEnumerator.cs b/docs/logs/redaction/MyClassWithRedactionEnumerator.cs deleted file mode 100644 index d354e4f5748..00000000000 --- a/docs/logs/redaction/MyClassWithRedactionEnumerator.cs +++ /dev/null @@ -1,59 +0,0 @@ -// -// 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. -// - -using System.Collections; - -namespace Redaction; - -internal class MyClassWithRedactionEnumerator : IReadOnlyList> -{ - private readonly IReadOnlyList> state; - - public MyClassWithRedactionEnumerator(IReadOnlyList> state) - { - this.state = state; - } - - public int Count => this.state.Count; - - public KeyValuePair this[int index] - { - get - { - var item = this.state[index]; - var entryVal = item.Value; - if (entryVal != null && entryVal.ToString() != null && entryVal.ToString().Contains("")) - { - return new KeyValuePair(item.Key, "newRedactedValueHere"); - } - - return item; - } - } - - public IEnumerator> GetEnumerator() - { - for (var i = 0; i < this.Count; i++) - { - yield return this[i]; - } - } - - IEnumerator IEnumerable.GetEnumerator() - { - return this.GetEnumerator(); - } -} diff --git a/docs/logs/redaction/MyRedactionProcessor.cs b/docs/logs/redaction/MyRedactionProcessor.cs index 14dc33033e8..046c5347ad5 100644 --- a/docs/logs/redaction/MyRedactionProcessor.cs +++ b/docs/logs/redaction/MyRedactionProcessor.cs @@ -14,12 +14,11 @@ // limitations under the License. // +using System.Collections; using OpenTelemetry; using OpenTelemetry.Logs; -namespace Redaction; - -internal class MyRedactionProcessor : BaseProcessor +internal sealed class MyRedactionProcessor : BaseProcessor { public override void OnEnd(LogRecord logRecord) { @@ -28,4 +27,44 @@ public override void OnEnd(LogRecord logRecord) logRecord.Attributes = new MyClassWithRedactionEnumerator(logRecord.Attributes); } } + + internal sealed class MyClassWithRedactionEnumerator : IReadOnlyList> + { + private readonly IReadOnlyList> state; + + public MyClassWithRedactionEnumerator(IReadOnlyList> state) + { + this.state = state; + } + + public int Count => this.state.Count; + + public KeyValuePair this[int index] + { + get + { + var item = this.state[index]; + var entryVal = item.Value; + if (entryVal != null && entryVal.ToString() != null && entryVal.ToString().Contains("")) + { + return new KeyValuePair(item.Key, "newRedactedValueHere"); + } + + return item; + } + } + + public IEnumerator> GetEnumerator() + { + for (var i = 0; i < this.Count; i++) + { + yield return this[i]; + } + } + + IEnumerator IEnumerable.GetEnumerator() + { + return this.GetEnumerator(); + } + } } diff --git a/docs/logs/redaction/Program.cs b/docs/logs/redaction/Program.cs index f381856234e..0e3e971bf89 100644 --- a/docs/logs/redaction/Program.cs +++ b/docs/logs/redaction/Program.cs @@ -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(); - var logger = loggerFactory.CreateLogger(); +// Message will be redacted by MyRedactionProcessor +logger.FoodPriceChanged("", 9.99); - // message will be redacted by MyRedactionProcessor - logger.LogInformation("OpenTelemetry {sensitiveString}.", ""); - } +// 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); } diff --git a/docs/metrics/getting-started-console/Program.cs b/docs/metrics/getting-started-console/Program.cs index 4911d3ce73c..862fa6a567f 100644 --- a/docs/metrics/getting-started-console/Program.cs +++ b/docs/metrics/getting-started-console/Program.cs @@ -18,8 +18,6 @@ using OpenTelemetry; using OpenTelemetry.Metrics; -namespace GettingStarted; - public class Program { private static readonly Meter MyMeter = new("MyCompany.MyProduct.MyLibrary", "1.0"); @@ -27,7 +25,7 @@ public class Program public static void Main() { - using var meterProvider = Sdk.CreateMeterProviderBuilder() + var meterProvider = Sdk.CreateMeterProviderBuilder() .AddMeter("MyCompany.MyProduct.MyLibrary") .AddConsoleExporter() .Build(); @@ -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(); } } diff --git a/docs/trace/getting-started-console/Program.cs b/docs/trace/getting-started-console/Program.cs index e7e061e185d..97f1d11cb7e 100644 --- a/docs/trace/getting-started-console/Program.cs +++ b/docs/trace/getting-started-console/Program.cs @@ -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(); @@ -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(); } }