├── .gitignore ├── ConsoleAppConsumoAPIs ├── ConsoleAppConsumoAPIs.csproj ├── ConsoleAppConsumoAPIs.sln ├── Data │ └── ContagemRepository.cs ├── Documents │ └── ContagemDocument.cs ├── Endpoints.cs ├── Models │ └── ResultadoContador.cs └── Program.cs ├── MySql └── APIContagem │ ├── .vscode │ ├── launch.json │ └── tasks.json │ ├── APIContagem.csproj │ ├── APIContagem.sln │ ├── Contador.cs │ ├── Controllers │ └── ContadorController.cs │ ├── Data │ ├── ContagemContext.cs │ ├── ContagemRepository.cs │ └── HistoricoContagem.cs │ ├── Logging │ └── ContagemLogging.cs │ ├── Models │ └── ResultadoContador.cs │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── Tracing │ └── OpenTelemetryExtensions.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── PostgreSql └── APIContagem │ ├── .vscode │ ├── launch.json │ └── tasks.json │ ├── APIContagem.csproj │ ├── APIContagem.sln │ ├── Contador.cs │ ├── Controllers │ └── ContadorController.cs │ ├── Data │ ├── ContagemContext.cs │ ├── ContagemRepository.cs │ └── HistoricoContagem.cs │ ├── Logging │ └── ContagemLogging.cs │ ├── Models │ └── ResultadoContador.cs │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── Tracing │ └── OpenTelemetryExtensions.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── Redis └── APIContagem │ ├── .vscode │ ├── launch.json │ └── tasks.json │ ├── APIContagem.csproj │ ├── APIContagem.sln │ ├── Controllers │ └── ContadorController.cs │ ├── Logging │ └── ContagemLogging.cs │ ├── Models │ └── ResultadoContador.cs │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── Tracing │ └── OpenTelemetryExtensions.cs │ ├── appsettings.Development.json │ └── appsettings.json ├── Scripts ├── DockerCompose │ └── docker-compose.yml ├── MySql │ └── basecontagem.sql ├── PostgreSQL │ └── basecontagem.sql └── SqlServer │ └── BaseContagem.sql └── SqlServer └── APIContagem ├── .vscode ├── launch.json └── tasks.json ├── APIContagem.csproj ├── APIContagem.sln ├── Contador.cs ├── Controllers └── ContadorController.cs ├── Data ├── ContagemContext.cs ├── ContagemRepository.cs └── HistoricoContagem.cs ├── Logging └── ContagemLogging.cs ├── Models └── ResultadoContador.cs ├── Program.cs ├── Properties └── launchSettings.json ├── Tracing └── OpenTelemetryExtensions.cs ├── appsettings.Development.json └── appsettings.json /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /ConsoleAppConsumoAPIs/ConsoleAppConsumoAPIs.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /ConsoleAppConsumoAPIs/ConsoleAppConsumoAPIs.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32708.82 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleAppConsumoAPIs", "ConsoleAppConsumoAPIs.csproj", "{B783A121-4913-4E1C-846D-E7B9F4672110}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {B783A121-4913-4E1C-846D-E7B9F4672110}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {B783A121-4913-4E1C-846D-E7B9F4672110}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {B783A121-4913-4E1C-846D-E7B9F4672110}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {B783A121-4913-4E1C-846D-E7B9F4672110}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {5A8F60A2-81AB-4F65-9B81-E77982538D2C} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /ConsoleAppConsumoAPIs/Data/ContagemRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using ConsoleAppConsumoAPIs.Documents; 7 | using MongoDB.Driver; 8 | using MongoDB.Driver.Core.Extensions.DiagnosticSources; 9 | 10 | namespace ConsoleAppConsumoAPIs.Data; 11 | 12 | public static class ContagemRepository 13 | { 14 | public static void Save(ContagemDocument document) 15 | { 16 | var clientSettings = MongoClientSettings.FromUrl( 17 | new MongoUrl("mongodb://root:MongoDB2022!@localhost:27017")); 18 | clientSettings.ClusterConfigurator = cb => cb.Subscribe(new DiagnosticsActivityEventSubscriber()); 19 | 20 | var client = new MongoClient(clientSettings); 21 | var db = client.GetDatabase("DBContagem"); 22 | var historico = db.GetCollection("HistoricoContagem"); 23 | 24 | historico.InsertOne(document); 25 | } 26 | } -------------------------------------------------------------------------------- /ConsoleAppConsumoAPIs/Documents/ContagemDocument.cs: -------------------------------------------------------------------------------- 1 | using MongoDB.Bson; 2 | 3 | namespace ConsoleAppConsumoAPIs.Documents; 4 | 5 | public class ContagemDocument 6 | { 7 | public ObjectId _id { get; set; } 8 | public string? DataReferencia { get; set; } 9 | public object? SqlServerData { get; set; } 10 | public object? PostgreSqlData { get; set; } 11 | public object? RedisData { get; set; } 12 | public object? MySqlData { get; set; } 13 | } -------------------------------------------------------------------------------- /ConsoleAppConsumoAPIs/Endpoints.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using ConsoleAppConsumoAPIs.Models; 3 | using System.Net.Http.Json; 4 | 5 | namespace ConsoleAppConsumoAPIs; 6 | 7 | public static class Endpoints 8 | { 9 | public static string APIContagemSQLServer { get; } = "http://localhost:6100/contador"; 10 | public static string APIContagemPostgreSQL { get; } = "http://localhost:6200/contador"; 11 | public static string APIContagemRedis { get; } = "http://localhost:6300/contador"; 12 | public static string APIContagemMySQL { get; } = "http://localhost:6400/contador"; 13 | 14 | public static ResultadoContador SendRequest(string tecnologia, string endpoint, HttpClient client) 15 | { 16 | using var activity = new ActivitySource(nameof(ConsoleAppConsumoAPIs), "1.0.0") 17 | .StartActivity($"{nameof(SendRequest)} APIContagem{tecnologia}"); 18 | activity?.SetTag("endpoint", endpoint); 19 | 20 | var resultado = client.GetFromJsonAsync(endpoint).Result; 21 | activity?.SetTag("valorAtual", resultado!.ValorAtual); 22 | 23 | Console.WriteLine( 24 | $"{tecnologia}: {resultado!.Producer} | Valor atual = {resultado.ValorAtual}"); 25 | 26 | return resultado; 27 | } 28 | } -------------------------------------------------------------------------------- /ConsoleAppConsumoAPIs/Models/ResultadoContador.cs: -------------------------------------------------------------------------------- 1 | namespace ConsoleAppConsumoAPIs.Models; 2 | 3 | public class ResultadoContador 4 | { 5 | public int ValorAtual { get; set; } 6 | public string? Producer { get; set; } 7 | } -------------------------------------------------------------------------------- /ConsoleAppConsumoAPIs/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using OpenTelemetry; 3 | using OpenTelemetry.Trace; 4 | using OpenTelemetry.Resources; 5 | using ConsoleAppConsumoAPIs; 6 | using ConsoleAppConsumoAPIs.Data; 7 | using ConsoleAppConsumoAPIs.Documents; 8 | 9 | Console.WriteLine("***** Tracing Distribuído com .NET 6 + ASP.NET Core + Jaeger + OpenTelemetry + " + 10 | "SQL Server + PostgreSQL + Redis + MySQL *****"); 11 | 12 | using var tracerProvider = Sdk.CreateTracerProviderBuilder() 13 | .AddSource(nameof(ConsoleAppConsumoAPIs)) 14 | .SetResourceBuilder( 15 | ResourceBuilder.CreateDefault() 16 | .AddService(serviceName: nameof(ConsoleAppConsumoAPIs), serviceVersion: "1.0.0")) 17 | .AddHttpClientInstrumentation() 18 | .AddMongoDBInstrumentation() 19 | .AddJaegerExporter(exporter => 20 | { 21 | exporter.AgentHost = "localhost"; 22 | exporter.AgentPort = 6831; 23 | }) 24 | .Build(); 25 | 26 | using var client = new HttpClient(); 27 | using var activitySource = new ActivitySource(nameof(ConsoleAppConsumoAPIs), "1.0.0"); 28 | 29 | while (true) 30 | { 31 | using var activity = activitySource.StartActivity("SendRequests"); 32 | activity?.SetTag("startPoint", "Program.cs"); 33 | 34 | var contagemDocument = new ContagemDocument(); 35 | contagemDocument.DataReferencia = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 36 | 37 | contagemDocument.SqlServerData = 38 | Endpoints.SendRequest("SQLServer", Endpoints.APIContagemSQLServer, client); 39 | contagemDocument.PostgreSqlData = 40 | Endpoints.SendRequest("PostgreSQL", Endpoints.APIContagemPostgreSQL, client); 41 | contagemDocument.RedisData = 42 | Endpoints.SendRequest("Redis", Endpoints.APIContagemRedis, client); 43 | contagemDocument.MySqlData = 44 | Endpoints.SendRequest("MySQL", Endpoints.APIContagemMySQL, client); 45 | ContagemRepository.Save(contagemDocument); 46 | 47 | activity?.Dispose(); 48 | 49 | Console.WriteLine("Pressione ENTER para enviar uma nova requisição..."); 50 | Console.ReadLine(); 51 | } -------------------------------------------------------------------------------- /MySql/APIContagem/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | // Use IntelliSense to find out which attributes exist for C# debugging 6 | // Use hover for the description of the existing attributes 7 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 8 | "name": ".NET Core Launch (web)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/bin/Debug/net6.0/APIContagem.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}", 16 | "stopAtEntry": false, 17 | // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser 18 | "serverReadyAction": { 19 | "action": "openExternally", 20 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)" 21 | }, 22 | "env": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | }, 25 | "sourceFileMap": { 26 | "/Views": "${workspaceFolder}/Views" 27 | } 28 | }, 29 | { 30 | "name": ".NET Core Attach", 31 | "type": "coreclr", 32 | "request": "attach" 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /MySql/APIContagem/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/APIContagem.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/APIContagem.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "--project", 36 | "${workspaceFolder}/APIContagem.csproj" 37 | ], 38 | "problemMatcher": "$msCompile" 39 | } 40 | ] 41 | } -------------------------------------------------------------------------------- /MySql/APIContagem/APIContagem.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /MySql/APIContagem/APIContagem.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32708.82 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIContagem", "APIContagem.csproj", "{0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {CC4BD6F0-B69A-4D5E-866E-3931B8AA27CD} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /MySql/APIContagem/Contador.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.InteropServices; 2 | using APIContagem.Tracing; 3 | 4 | namespace APIContagem; 5 | 6 | public class Contador 7 | { 8 | private static readonly string _LOCAL; 9 | private static readonly string _KERNEL; 10 | private static readonly string _FRAMEWORK; 11 | 12 | static Contador() 13 | { 14 | _LOCAL = OpenTelemetryExtensions.ServiceName; 15 | _KERNEL = Environment.OSVersion.VersionString; 16 | _FRAMEWORK = RuntimeInformation.FrameworkDescription; 17 | } 18 | 19 | private int _valorAtual = 30000; 20 | 21 | public int ValorAtual { get => _valorAtual; } 22 | public string Local { get => _LOCAL; } 23 | public string Kernel { get => _KERNEL; } 24 | public string Framework { get => _FRAMEWORK; } 25 | 26 | public void Incrementar() 27 | { 28 | _valorAtual++; 29 | } 30 | } -------------------------------------------------------------------------------- /MySql/APIContagem/Controllers/ContadorController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using APIContagem.Data; 3 | using APIContagem.Models; 4 | using APIContagem.Logging; 5 | using APIContagem.Tracing; 6 | 7 | namespace APIContagem.Controllers; 8 | 9 | [ApiController] 10 | [Route("[controller]")] 11 | public class ContadorController : ControllerBase 12 | { 13 | private static readonly Contador _CONTADOR = new Contador(); 14 | private readonly ILogger _logger; 15 | private readonly IConfiguration _configuration; 16 | private readonly ContagemRepository _repository; 17 | 18 | public ContadorController(ILogger logger, 19 | IConfiguration configuration, 20 | ContagemRepository repository) 21 | { 22 | _logger = logger; 23 | _configuration = configuration; 24 | _repository = repository; 25 | } 26 | 27 | [HttpGet] 28 | public ResultadoContador Get() 29 | { 30 | int valorAtualContador; 31 | 32 | lock (_CONTADOR) 33 | { 34 | _CONTADOR.Incrementar(); 35 | valorAtualContador = _CONTADOR.ValorAtual; 36 | } 37 | 38 | using var activity = OpenTelemetryExtensions.CreateActivitySource() 39 | .StartActivity("Identificando"); 40 | activity?.SetTag("valorAtual", valorAtualContador); 41 | 42 | _logger.LogValorAtual(valorAtualContador); 43 | 44 | var resultado = new ResultadoContador() 45 | { 46 | ValorAtual = valorAtualContador, 47 | Producer = _CONTADOR.Local, 48 | Kernel = _CONTADOR.Kernel, 49 | Framework = _CONTADOR.Framework, 50 | Mensagem = _configuration["MensagemVariavel"] 51 | }; 52 | _repository.Insert(resultado); 53 | _logger.LogInformation($"Registro inserido com sucesso! Valor: {valorAtualContador}"); 54 | 55 | return resultado; 56 | } 57 | } -------------------------------------------------------------------------------- /MySql/APIContagem/Data/ContagemContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace APIContagem.Data; 4 | 5 | public class ContagemContext : DbContext 6 | { 7 | public DbSet? Historicos { get; set; } 8 | 9 | public ContagemContext(DbContextOptions options) : 10 | base(options) 11 | { 12 | } 13 | 14 | protected override void OnModelCreating(ModelBuilder modelBuilder) 15 | { 16 | modelBuilder.Entity(entity => 17 | { 18 | entity.ToTable("HistoricoContagem"); 19 | entity.HasKey(c => c.Id); 20 | }); 21 | } 22 | } -------------------------------------------------------------------------------- /MySql/APIContagem/Data/ContagemRepository.cs: -------------------------------------------------------------------------------- 1 | using APIContagem.Models; 2 | 3 | namespace APIContagem.Data; 4 | 5 | public class ContagemRepository 6 | { 7 | private readonly ContagemContext _context; 8 | 9 | public ContagemRepository(ContagemContext context) 10 | { 11 | _context = context; 12 | } 13 | 14 | public void Insert(ResultadoContador resultado) 15 | { 16 | _context.Historicos!.Add(new() 17 | { 18 | DataProcessamento = DateTime.Now, 19 | ValorAtual = resultado.ValorAtual, 20 | Producer = resultado.Producer, 21 | Kernel = resultado.Kernel, 22 | Framework = resultado.Framework, 23 | Mensagem = resultado.Mensagem 24 | }); 25 | _context.SaveChanges(); 26 | } 27 | } -------------------------------------------------------------------------------- /MySql/APIContagem/Data/HistoricoContagem.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Data; 2 | 3 | public class HistoricoContagem 4 | { 5 | public int? Id { get; set; } 6 | public DateTime DataProcessamento { get; set; } 7 | public int ValorAtual { get; set; } 8 | public string? Producer { get; set; } 9 | public string? Kernel { get; set; } 10 | public string? Framework { get; set; } 11 | public string? Mensagem { get; set; } 12 | } -------------------------------------------------------------------------------- /MySql/APIContagem/Logging/ContagemLogging.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Logging; 2 | 3 | public static partial class ContagemLogging 4 | { 5 | [LoggerMessage(EventId = 1, Level = LogLevel.Information, 6 | Message = "Contador - Valor atual: {valorAtual}")] 7 | public static partial void LogValorAtual( 8 | this ILogger logger, int valorAtual); 9 | } -------------------------------------------------------------------------------- /MySql/APIContagem/Models/ResultadoContador.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Models; 2 | 3 | public class ResultadoContador 4 | { 5 | public int ValorAtual { get; set; } 6 | public string? Producer { get; set; } 7 | public string? Kernel { get; set; } 8 | public string? Framework { get; set; } 9 | public string? Mensagem { get; set; } 10 | } -------------------------------------------------------------------------------- /MySql/APIContagem/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using OpenTelemetry.Resources; 3 | using OpenTelemetry.Trace; 4 | using APIContagem.Data; 5 | using APIContagem.Tracing; 6 | 7 | AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); 8 | AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); 9 | 10 | var builder = WebApplication.CreateBuilder(args); 11 | 12 | builder.Services.AddControllers(); 13 | builder.Services.AddEndpointsApiExplorer(); 14 | builder.Services.AddSwaggerGen(); 15 | 16 | builder.Services.AddScoped(); 17 | 18 | builder.Services.AddDbContext(options => 19 | { 20 | options.UseMySQL( 21 | builder.Configuration.GetConnectionString("BaseContagem")); 22 | }); 23 | 24 | builder.Services.AddOpenTelemetryTracing(traceProvider => 25 | { 26 | traceProvider 27 | .AddSource(OpenTelemetryExtensions.ServiceName) 28 | .SetResourceBuilder( 29 | ResourceBuilder.CreateDefault() 30 | .AddService(serviceName: OpenTelemetryExtensions.ServiceName, 31 | serviceVersion: OpenTelemetryExtensions.ServiceVersion)) 32 | .AddAspNetCoreInstrumentation() 33 | .AddMySqlDataInstrumentation(options => { 34 | options.SetDbStatement = true; 35 | }) 36 | .AddJaegerExporter(exporter => 37 | { 38 | exporter.AgentHost = builder.Configuration["Jaeger:AgentHost"]; 39 | exporter.AgentPort = Convert.ToInt32(builder.Configuration["Jaeger:AgentPort"]); 40 | }); 41 | }); 42 | 43 | var app = builder.Build(); 44 | 45 | app.UseSwagger(); 46 | app.UseSwaggerUI(); 47 | 48 | app.UseHttpsRedirection(); 49 | 50 | app.UseAuthorization(); 51 | 52 | app.MapControllers(); 53 | 54 | app.Run(); -------------------------------------------------------------------------------- /MySql/APIContagem/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:52386", 8 | "sslPort": 44398 9 | } 10 | }, 11 | "profiles": { 12 | "APIContagem": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "launchUrl": "swagger", 17 | "applicationUrl": "https://localhost:7196;http://localhost:5122", 18 | "environmentVariables": { 19 | "ASPNETCORE_ENVIRONMENT": "Development" 20 | } 21 | }, 22 | "IIS Express": { 23 | "commandName": "IISExpress", 24 | "launchBrowser": true, 25 | "launchUrl": "swagger", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MySql/APIContagem/Tracing/OpenTelemetryExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace APIContagem.Tracing; 4 | 5 | public static class OpenTelemetryExtensions 6 | { 7 | public static string ServiceName { get; } 8 | public static string ServiceVersion { get; } 9 | 10 | static OpenTelemetryExtensions() 11 | { 12 | ServiceName = "APIContagemMySQL"; 13 | ServiceVersion = typeof(OpenTelemetryExtensions).Assembly.GetName().Version!.ToString(); 14 | } 15 | 16 | public static ActivitySource CreateActivitySource() => 17 | new ActivitySource(ServiceName, ServiceVersion); 18 | } -------------------------------------------------------------------------------- /MySql/APIContagem/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /MySql/APIContagem/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "MensagemVariavel": "Testes com .NET 6 + ASP.NET Core + MySQL", 3 | "Logging": { 4 | "LogLevel": { 5 | "Default": "Information", 6 | "Microsoft.AspNetCore": "Warning" 7 | } 8 | }, 9 | "Jaeger": { 10 | "AgentHost": "localhost", 11 | "AgentPort": "6831" 12 | }, 13 | "ConnectionStrings": { 14 | "BaseContagem": "Server=localhost; Port=3306; Database=basecontagem; Uid=root; Pwd=MySql2022!;" 15 | }, 16 | "AllowedHosts": "*", 17 | "Kestrel": { 18 | "Endpoints": { 19 | "HTTP": { 20 | "Url": "http://localhost:6400" 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | // Use IntelliSense to find out which attributes exist for C# debugging 6 | // Use hover for the description of the existing attributes 7 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 8 | "name": ".NET Core Launch (web)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/bin/Debug/net6.0/APIContagem.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}", 16 | "stopAtEntry": false, 17 | // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser 18 | "serverReadyAction": { 19 | "action": "openExternally", 20 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)" 21 | }, 22 | "env": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | }, 25 | "sourceFileMap": { 26 | "/Views": "${workspaceFolder}/Views" 27 | } 28 | }, 29 | { 30 | "name": ".NET Core Attach", 31 | "type": "coreclr", 32 | "request": "attach" 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/APIContagem.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/APIContagem.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "--project", 36 | "${workspaceFolder}/APIContagem.csproj" 37 | ], 38 | "problemMatcher": "$msCompile" 39 | } 40 | ] 41 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/APIContagem.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /PostgreSql/APIContagem/APIContagem.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32708.82 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIContagem", "APIContagem.csproj", "{0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {CC4BD6F0-B69A-4D5E-866E-3931B8AA27CD} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Contador.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.InteropServices; 2 | using APIContagem.Tracing; 3 | 4 | namespace APIContagem; 5 | 6 | public class Contador 7 | { 8 | private static readonly string _LOCAL; 9 | private static readonly string _KERNEL; 10 | private static readonly string _FRAMEWORK; 11 | 12 | static Contador() 13 | { 14 | _LOCAL = OpenTelemetryExtensions.ServiceName; 15 | _KERNEL = Environment.OSVersion.VersionString; 16 | _FRAMEWORK = RuntimeInformation.FrameworkDescription; 17 | } 18 | 19 | private int _valorAtual = 20000; 20 | 21 | public int ValorAtual { get => _valorAtual; } 22 | public string Local { get => _LOCAL; } 23 | public string Kernel { get => _KERNEL; } 24 | public string Framework { get => _FRAMEWORK; } 25 | 26 | public void Incrementar() 27 | { 28 | _valorAtual++; 29 | } 30 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Controllers/ContadorController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using APIContagem.Data; 3 | using APIContagem.Models; 4 | using APIContagem.Logging; 5 | using APIContagem.Tracing; 6 | 7 | namespace APIContagem.Controllers; 8 | 9 | [ApiController] 10 | [Route("[controller]")] 11 | public class ContadorController : ControllerBase 12 | { 13 | private static readonly Contador _CONTADOR = new Contador(); 14 | private readonly ILogger _logger; 15 | private readonly IConfiguration _configuration; 16 | private readonly ContagemRepository _repository; 17 | 18 | public ContadorController(ILogger logger, 19 | IConfiguration configuration, 20 | ContagemRepository repository) 21 | { 22 | _logger = logger; 23 | _configuration = configuration; 24 | _repository = repository; 25 | } 26 | 27 | [HttpGet] 28 | public ResultadoContador Get() 29 | { 30 | int valorAtualContador; 31 | 32 | lock (_CONTADOR) 33 | { 34 | _CONTADOR.Incrementar(); 35 | valorAtualContador = _CONTADOR.ValorAtual; 36 | } 37 | 38 | using var activity = OpenTelemetryExtensions.CreateActivitySource() 39 | .StartActivity("Identificando"); 40 | activity?.SetTag("valorAtual", valorAtualContador); 41 | 42 | _logger.LogValorAtual(valorAtualContador); 43 | 44 | var resultado = new ResultadoContador() 45 | { 46 | ValorAtual = valorAtualContador, 47 | Producer = _CONTADOR.Local, 48 | Kernel = _CONTADOR.Kernel, 49 | Framework = _CONTADOR.Framework, 50 | Mensagem = _configuration["MensagemVariavel"] 51 | }; 52 | _repository.Insert(resultado); 53 | _logger.LogInformation($"Registro inserido com sucesso! Valor: {valorAtualContador}"); 54 | 55 | return resultado; 56 | } 57 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Data/ContagemContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace APIContagem.Data; 4 | 5 | public class ContagemContext : DbContext 6 | { 7 | public DbSet? Historicos { get; set; } 8 | 9 | public ContagemContext(DbContextOptions options) : 10 | base(options) 11 | { 12 | } 13 | 14 | protected override void OnModelCreating(ModelBuilder modelBuilder) 15 | { 16 | modelBuilder.Entity(entity => 17 | { 18 | entity.ToTable("HistoricoContagem"); 19 | entity.HasKey(c => c.Id); 20 | }); 21 | } 22 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Data/ContagemRepository.cs: -------------------------------------------------------------------------------- 1 | using APIContagem.Models; 2 | 3 | namespace APIContagem.Data; 4 | 5 | public class ContagemRepository 6 | { 7 | private readonly ContagemContext _context; 8 | 9 | public ContagemRepository(ContagemContext context) 10 | { 11 | _context = context; 12 | } 13 | 14 | public void Insert(ResultadoContador resultado) 15 | { 16 | _context.Historicos!.Add(new() 17 | { 18 | DataProcessamento = DateTime.Now, 19 | ValorAtual = resultado.ValorAtual, 20 | Producer = resultado.Producer, 21 | Kernel = resultado.Kernel, 22 | Framework = resultado.Framework, 23 | Mensagem = resultado.Mensagem 24 | }); 25 | _context.SaveChanges(); 26 | } 27 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Data/HistoricoContagem.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Data; 2 | 3 | public class HistoricoContagem 4 | { 5 | public int? Id { get; set; } 6 | public DateTime DataProcessamento { get; set; } 7 | public int ValorAtual { get; set; } 8 | public string? Producer { get; set; } 9 | public string? Kernel { get; set; } 10 | public string? Framework { get; set; } 11 | public string? Mensagem { get; set; } 12 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Logging/ContagemLogging.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Logging; 2 | 3 | public static partial class ContagemLogging 4 | { 5 | [LoggerMessage(EventId = 1, Level = LogLevel.Information, 6 | Message = "Contador - Valor atual: {valorAtual}")] 7 | public static partial void LogValorAtual( 8 | this ILogger logger, int valorAtual); 9 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Models/ResultadoContador.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Models; 2 | 3 | public class ResultadoContador 4 | { 5 | public int ValorAtual { get; set; } 6 | public string? Producer { get; set; } 7 | public string? Kernel { get; set; } 8 | public string? Framework { get; set; } 9 | public string? Mensagem { get; set; } 10 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using OpenTelemetry.Resources; 3 | using OpenTelemetry.Trace; 4 | using Npgsql; 5 | using APIContagem.Data; 6 | using APIContagem.Tracing; 7 | 8 | AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); 9 | AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); 10 | 11 | var builder = WebApplication.CreateBuilder(args); 12 | 13 | builder.Services.AddControllers(); 14 | builder.Services.AddEndpointsApiExplorer(); 15 | builder.Services.AddSwaggerGen(); 16 | 17 | builder.Services.AddScoped(); 18 | 19 | builder.Services.AddDbContext(options => 20 | { 21 | options.UseNpgsql( 22 | builder.Configuration.GetConnectionString("BaseContagem")); 23 | }); 24 | 25 | builder.Services.AddOpenTelemetryTracing(traceProvider => 26 | { 27 | traceProvider 28 | .AddSource(OpenTelemetryExtensions.ServiceName) 29 | .SetResourceBuilder( 30 | ResourceBuilder.CreateDefault() 31 | .AddService(serviceName: OpenTelemetryExtensions.ServiceName, 32 | serviceVersion: OpenTelemetryExtensions.ServiceVersion)) 33 | .AddAspNetCoreInstrumentation() 34 | .AddNpgsql() 35 | .AddJaegerExporter(exporter => 36 | { 37 | exporter.AgentHost = builder.Configuration["Jaeger:AgentHost"]; 38 | exporter.AgentPort = Convert.ToInt32(builder.Configuration["Jaeger:AgentPort"]); 39 | }); 40 | }); 41 | 42 | var app = builder.Build(); 43 | 44 | app.UseSwagger(); 45 | app.UseSwaggerUI(); 46 | 47 | app.UseHttpsRedirection(); 48 | 49 | app.UseAuthorization(); 50 | 51 | app.MapControllers(); 52 | 53 | app.Run(); -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:52386", 8 | "sslPort": 44398 9 | } 10 | }, 11 | "profiles": { 12 | "APIContagem": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "launchUrl": "swagger", 17 | "applicationUrl": "https://localhost:7196;http://localhost:5122", 18 | "environmentVariables": { 19 | "ASPNETCORE_ENVIRONMENT": "Development" 20 | } 21 | }, 22 | "IIS Express": { 23 | "commandName": "IISExpress", 24 | "launchBrowser": true, 25 | "launchUrl": "swagger", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /PostgreSql/APIContagem/Tracing/OpenTelemetryExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace APIContagem.Tracing; 4 | 5 | public static class OpenTelemetryExtensions 6 | { 7 | public static string ServiceName { get; } 8 | public static string ServiceVersion { get; } 9 | 10 | static OpenTelemetryExtensions() 11 | { 12 | ServiceName = "APIContagemPostgreSQL"; 13 | ServiceVersion = typeof(OpenTelemetryExtensions).Assembly.GetName().Version!.ToString(); 14 | } 15 | 16 | public static ActivitySource CreateActivitySource() => 17 | new ActivitySource(ServiceName, ServiceVersion); 18 | } -------------------------------------------------------------------------------- /PostgreSql/APIContagem/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /PostgreSql/APIContagem/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "MensagemVariavel": "Testes com .NET 6 + ASP.NET Core + PostgreSQL", 3 | "Logging": { 4 | "LogLevel": { 5 | "Default": "Information", 6 | "Microsoft.AspNetCore": "Warning" 7 | } 8 | }, 9 | "Jaeger": { 10 | "AgentHost": "localhost", 11 | "AgentPort": "6831" 12 | }, 13 | "ConnectionStrings": { 14 | "BaseContagem": "Server=127.0.0.1;Port=5432;Database=basecontagem;User Id=postgres;Password=Postgres2022!" 15 | }, 16 | "AllowedHosts": "*", 17 | "Kestrel": { 18 | "Endpoints": { 19 | "HTTP": { 20 | "Url": "http://localhost:6200" 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /Redis/APIContagem/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | // Use IntelliSense to find out which attributes exist for C# debugging 6 | // Use hover for the description of the existing attributes 7 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 8 | "name": ".NET Core Launch (web)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/bin/Debug/net6.0/APIContagem.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}", 16 | "stopAtEntry": false, 17 | // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser 18 | "serverReadyAction": { 19 | "action": "openExternally", 20 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)" 21 | }, 22 | "env": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | }, 25 | "sourceFileMap": { 26 | "/Views": "${workspaceFolder}/Views" 27 | } 28 | }, 29 | { 30 | "name": ".NET Core Attach", 31 | "type": "coreclr", 32 | "request": "attach" 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /Redis/APIContagem/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/APIContagem.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/APIContagem.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "${workspaceFolder}/APIContagem.csproj", 36 | "/property:GenerateFullPaths=true", 37 | "/consoleloggerparameters:NoSummary" 38 | ], 39 | "problemMatcher": "$msCompile" 40 | } 41 | ] 42 | } -------------------------------------------------------------------------------- /Redis/APIContagem/APIContagem.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Redis/APIContagem/APIContagem.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.32104.313 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIContagem", "APIContagem.csproj", "{5F55B957-7374-4CE3-90D4-3C67BEF8A2F4}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {5F55B957-7374-4CE3-90D4-3C67BEF8A2F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {5F55B957-7374-4CE3-90D4-3C67BEF8A2F4}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {5F55B957-7374-4CE3-90D4-3C67BEF8A2F4}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {5F55B957-7374-4CE3-90D4-3C67BEF8A2F4}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {A1B8EE6D-E8D2-4194-9A83-E90580D205B8} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /Redis/APIContagem/Controllers/ContadorController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using System.Diagnostics; 3 | using StackExchange.Redis; 4 | using APIContagem.Models; 5 | using APIContagem.Logging; 6 | using APIContagem.Tracing; 7 | 8 | namespace APIContagem.Controllers; 9 | 10 | [ApiController] 11 | [Route("[controller]")] 12 | public class ContadorController : ControllerBase 13 | { 14 | private readonly ILogger _logger; 15 | private readonly ConnectionMultiplexer _connectionRedis; 16 | private readonly ActivitySource _activitySource; 17 | 18 | public ContadorController(ILogger logger, 19 | ConnectionMultiplexer connectionRedis) 20 | { 21 | _activitySource = OpenTelemetryExtensions.CreateActivitySource(); 22 | using var activity = 23 | _activitySource.StartActivity($"Construtor ({nameof(ContadorController)})"); 24 | activity!.SetTag("horario", $"{DateTime.Now:HH:mm:ss dd/MM/yyyy}"); 25 | 26 | _logger = logger; 27 | _connectionRedis = connectionRedis; 28 | } 29 | 30 | [HttpGet] 31 | public ResultadoContador Get() 32 | { 33 | using var activity = 34 | _activitySource.StartActivity($"{nameof(Get)} ({nameof(ContadorController)})"); 35 | 36 | var valorAtualContador = 37 | (int)_connectionRedis.GetDatabase().StringIncrement("APIContagem");; 38 | 39 | _logger.LogValorAtual(valorAtualContador); 40 | 41 | activity!.SetTag("valorContador", valorAtualContador); 42 | activity!.SetTag("producer", OpenTelemetryExtensions.Local); 43 | activity!.SetTag("kernel", OpenTelemetryExtensions.Kernel); 44 | activity!.SetTag("framework", OpenTelemetryExtensions.Framework); 45 | activity!.SetTag("mensagem", OpenTelemetryExtensions.Local); 46 | 47 | return new () 48 | { 49 | ValorAtual = valorAtualContador, 50 | Producer = OpenTelemetryExtensions.Local, 51 | Kernel = OpenTelemetryExtensions.Kernel, 52 | Framework = OpenTelemetryExtensions.Framework, 53 | Mensagem = OpenTelemetryExtensions.Local 54 | }; 55 | } 56 | } -------------------------------------------------------------------------------- /Redis/APIContagem/Logging/ContagemLogging.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Logging; 2 | 3 | public static partial class ContagemLogging 4 | { 5 | [LoggerMessage(EventId = 1, Level = LogLevel.Information, 6 | Message = "Contador - Valor atual: {valorAtual}")] 7 | public static partial void LogValorAtual( 8 | this ILogger logger, int valorAtual); 9 | } -------------------------------------------------------------------------------- /Redis/APIContagem/Models/ResultadoContador.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Models; 2 | 3 | public class ResultadoContador 4 | { 5 | public int ValorAtual { get; set; } 6 | public string? Producer { get; set; } 7 | public string? Kernel { get; set; } 8 | public string? Framework { get; set; } 9 | public string? Mensagem { get; set; } 10 | } -------------------------------------------------------------------------------- /Redis/APIContagem/Program.cs: -------------------------------------------------------------------------------- 1 | using OpenTelemetry.Resources; 2 | using OpenTelemetry.Trace; 3 | using StackExchange.Redis; 4 | using APIContagem.Tracing; 5 | 6 | var builder = WebApplication.CreateBuilder(args); 7 | 8 | builder.Services.AddControllers(); 9 | builder.Services.AddEndpointsApiExplorer(); 10 | builder.Services.AddSwaggerGen(); 11 | 12 | using var connectionRedis = ConnectionMultiplexer.Connect( 13 | builder.Configuration.GetConnectionString("Redis")); 14 | builder.Services.AddSingleton(connectionRedis); 15 | 16 | // Documentacao do OpenTelemetry: 17 | // https://opentelemetry.io/docs/instrumentation/net/getting-started/ 18 | 19 | // Integracaoo do OpenTelemetry com Jaeger: 20 | // https://opentelemetry.io/docs/instrumentation/net/exporters/ 21 | 22 | // Documentacaoo do Jaeger: 23 | // https://www.jaegertracing.io/docs/1.33/ 24 | 25 | builder.Services.AddOpenTelemetryTracing(traceProvider => 26 | { 27 | traceProvider 28 | .AddSource(OpenTelemetryExtensions.ServiceName) 29 | .SetResourceBuilder( 30 | ResourceBuilder.CreateDefault() 31 | .AddService(serviceName: OpenTelemetryExtensions.ServiceName, 32 | serviceVersion: OpenTelemetryExtensions.ServiceVersion)) 33 | .AddAspNetCoreInstrumentation() 34 | .AddRedisInstrumentation(connectionRedis, 35 | options => options.SetVerboseDatabaseStatements = true) 36 | .AddJaegerExporter(exporter => 37 | { 38 | exporter.AgentHost = builder.Configuration["Jaeger:AgentHost"]; 39 | exporter.AgentPort = Convert.ToInt32(builder.Configuration["Jaeger:AgentPort"]); 40 | }); 41 | }); 42 | 43 | var app = builder.Build(); 44 | 45 | if (app.Environment.IsDevelopment()) 46 | { 47 | app.UseSwagger(); 48 | app.UseSwaggerUI(); 49 | } 50 | 51 | app.UseHttpsRedirection(); 52 | 53 | app.UseAuthorization(); 54 | 55 | app.MapControllers(); 56 | 57 | app.Run(); -------------------------------------------------------------------------------- /Redis/APIContagem/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:29570", 8 | "sslPort": 44305 9 | } 10 | }, 11 | "profiles": { 12 | "APIContagem": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "launchUrl": "swagger", 17 | "applicationUrl": "https://localhost:7117;http://localhost:5235", 18 | "environmentVariables": { 19 | "ASPNETCORE_ENVIRONMENT": "Development" 20 | } 21 | }, 22 | "IIS Express": { 23 | "commandName": "IISExpress", 24 | "launchBrowser": true, 25 | "launchUrl": "swagger", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Redis/APIContagem/Tracing/OpenTelemetryExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace APIContagem.Tracing; 5 | 6 | public static class OpenTelemetryExtensions 7 | { 8 | public static string Local { get; } 9 | public static string Kernel { get; } 10 | public static string Framework { get; } 11 | public static string ServiceName { get; } 12 | public static string ServiceVersion { get; } 13 | 14 | static OpenTelemetryExtensions() 15 | { 16 | Local = "APIContagemRedis"; 17 | Kernel = Environment.OSVersion.VersionString; 18 | Framework = RuntimeInformation.FrameworkDescription; 19 | ServiceName = "APIContagemRedis"; 20 | ServiceVersion = typeof(OpenTelemetryExtensions).Assembly.GetName().Version!.ToString(); 21 | } 22 | 23 | public static ActivitySource CreateActivitySource() => 24 | new ActivitySource(ServiceName, ServiceVersion); 25 | } -------------------------------------------------------------------------------- /Redis/APIContagem/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Redis/APIContagem/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Jaeger": { 3 | "AgentHost": "localhost", 4 | "AgentPort": "6831" 5 | }, 6 | "ConnectionStrings": { 7 | "Redis": "localhost" 8 | }, 9 | "Logging": { 10 | "LogLevel": { 11 | "Default": "Information", 12 | "Microsoft.AspNetCore": "Warning" 13 | } 14 | }, 15 | "AllowedHosts": "*", 16 | "Kestrel": { 17 | "Endpoints": { 18 | "HTTP": { 19 | "Url": "http://localhost:6300" 20 | } 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Scripts/DockerCompose/docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | jaeger: 3 | image: jaegertracing/all-in-one:latest 4 | ports: 5 | - "6831:6831/udp" 6 | - "16686:16686" 7 | sqlserver: 8 | image: mcr.microsoft.com/mssql/server:2019-GA-ubuntu-16.04 9 | environment: 10 | SA_PASSWORD: "SqlServer2019!" 11 | ACCEPT_EULA: "Y" 12 | MSSQL_PID: "Developer" 13 | ports: 14 | - "1433:1433" 15 | postgres: 16 | image: postgres 17 | environment: 18 | POSTGRES_PASSWORD: "Postgres2022!" 19 | ports: 20 | - "5432:5432" 21 | redis: 22 | image: redis:alpine 23 | ports: 24 | - "6379:6379" 25 | mysql: 26 | image: mysql:5.7 27 | environment: 28 | MYSQL_ROOT_PASSWORD: "MySql2022!" 29 | MYSQL_DATABASE: "basecontagem" 30 | ports: 31 | - "3306:3306" 32 | mongo: 33 | image: mongo 34 | environment: 35 | MONGO_INITDB_ROOT_USERNAME: root 36 | MONGO_INITDB_ROOT_PASSWORD: MongoDB2022! 37 | ports: 38 | - "27017:27017" -------------------------------------------------------------------------------- /Scripts/MySql/basecontagem.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE HistoricoContagem ( 2 | Id INT NOT NULL AUTO_INCREMENT, 3 | DataProcessamento DATETIME NOT NULL, 4 | ValorAtual INT NOT NULL, 5 | Producer VARCHAR(120) NOT NULL, 6 | Kernel VARCHAR(80) NOT NULL, 7 | Framework VARCHAR(80) NOT NULL, 8 | Mensagem VARCHAR(500) NOT NULL, 9 | CONSTRAINT PK_HistoricoContagem PRIMARY KEY (Id) 10 | ) -------------------------------------------------------------------------------- /Scripts/PostgreSQL/basecontagem.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE basecontagem; 2 | 3 | \c basecontagem; 4 | 5 | CREATE TABLE "HistoricoContagem" ( 6 | "Id" SERIAL NOT NULL, 7 | "DataProcessamento" TIMESTAMP NOT NULL, 8 | "ValorAtual" INT NOT NULL, 9 | "Producer" VARCHAR(120) NOT NULL, 10 | "Kernel" VARCHAR(80) NOT NULL, 11 | "Framework" VARCHAR(80) NOT NULL, 12 | "Mensagem" VARCHAR(500) NOT NULL, 13 | CONSTRAINT "PK_HistoricoContagem" PRIMARY KEY ("Id") 14 | ); -------------------------------------------------------------------------------- /Scripts/SqlServer/BaseContagem.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE BaseContagem 2 | GO 3 | 4 | USE BaseContagem 5 | GO 6 | 7 | CREATE TABLE dbo.HistoricoContagem( 8 | Id INT IDENTITY(1,1) NOT NULL, 9 | DataProcessamento DATETIME NOT NULL, 10 | ValorAtual INT NOT NULL, 11 | Producer VARCHAR(120) NOT NULL, 12 | Kernel VARCHAR(80) NOT NULL, 13 | Framework VARCHAR(80) NOT NULL, 14 | Mensagem VARCHAR(500) NOT NULL, 15 | CONSTRAINT PK_HistoricoContagem PRIMARY KEY (Id) 16 | ) 17 | GO -------------------------------------------------------------------------------- /SqlServer/APIContagem/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | // Use IntelliSense to find out which attributes exist for C# debugging 6 | // Use hover for the description of the existing attributes 7 | // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md 8 | "name": ".NET Core Launch (web)", 9 | "type": "coreclr", 10 | "request": "launch", 11 | "preLaunchTask": "build", 12 | // If you have changed target frameworks, make sure to update the program path. 13 | "program": "${workspaceFolder}/bin/Debug/net6.0/APIContagem.dll", 14 | "args": [], 15 | "cwd": "${workspaceFolder}", 16 | "stopAtEntry": false, 17 | // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser 18 | "serverReadyAction": { 19 | "action": "openExternally", 20 | "pattern": "\\bNow listening on:\\s+(https?://\\S+)" 21 | }, 22 | "env": { 23 | "ASPNETCORE_ENVIRONMENT": "Development" 24 | }, 25 | "sourceFileMap": { 26 | "/Views": "${workspaceFolder}/Views" 27 | } 28 | }, 29 | { 30 | "name": ".NET Core Attach", 31 | "type": "coreclr", 32 | "request": "attach" 33 | } 34 | ] 35 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0.0", 3 | "tasks": [ 4 | { 5 | "label": "build", 6 | "command": "dotnet", 7 | "type": "process", 8 | "args": [ 9 | "build", 10 | "${workspaceFolder}/APIContagem.csproj", 11 | "/property:GenerateFullPaths=true", 12 | "/consoleloggerparameters:NoSummary" 13 | ], 14 | "problemMatcher": "$msCompile" 15 | }, 16 | { 17 | "label": "publish", 18 | "command": "dotnet", 19 | "type": "process", 20 | "args": [ 21 | "publish", 22 | "${workspaceFolder}/APIContagem.csproj", 23 | "/property:GenerateFullPaths=true", 24 | "/consoleloggerparameters:NoSummary" 25 | ], 26 | "problemMatcher": "$msCompile" 27 | }, 28 | { 29 | "label": "watch", 30 | "command": "dotnet", 31 | "type": "process", 32 | "args": [ 33 | "watch", 34 | "run", 35 | "--project", 36 | "${workspaceFolder}/APIContagem.csproj" 37 | ], 38 | "problemMatcher": "$msCompile" 39 | } 40 | ] 41 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/APIContagem.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /SqlServer/APIContagem/APIContagem.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.3.32708.82 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIContagem", "APIContagem.csproj", "{0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {0C4E2891-BA90-46D8-BC69-66F2D4BABFAC}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {CC4BD6F0-B69A-4D5E-866E-3931B8AA27CD} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /SqlServer/APIContagem/Contador.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.InteropServices; 2 | using APIContagem.Tracing; 3 | 4 | namespace APIContagem; 5 | 6 | public class Contador 7 | { 8 | private static readonly string _LOCAL; 9 | private static readonly string _KERNEL; 10 | private static readonly string _FRAMEWORK; 11 | 12 | static Contador() 13 | { 14 | _LOCAL = OpenTelemetryExtensions.ServiceName; 15 | _KERNEL = Environment.OSVersion.VersionString; 16 | _FRAMEWORK = RuntimeInformation.FrameworkDescription; 17 | } 18 | 19 | private int _valorAtual = 10000; 20 | 21 | public int ValorAtual { get => _valorAtual; } 22 | public string Local { get => _LOCAL; } 23 | public string Kernel { get => _KERNEL; } 24 | public string Framework { get => _FRAMEWORK; } 25 | 26 | public void Incrementar() 27 | { 28 | _valorAtual++; 29 | } 30 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/Controllers/ContadorController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc; 2 | using APIContagem.Data; 3 | using APIContagem.Models; 4 | using APIContagem.Logging; 5 | using APIContagem.Tracing; 6 | 7 | namespace APIContagem.Controllers; 8 | 9 | [ApiController] 10 | [Route("[controller]")] 11 | public class ContadorController : ControllerBase 12 | { 13 | private static readonly Contador _CONTADOR = new Contador(); 14 | private readonly ILogger _logger; 15 | private readonly IConfiguration _configuration; 16 | private readonly ContagemRepository _repository; 17 | 18 | public ContadorController(ILogger logger, 19 | IConfiguration configuration, 20 | ContagemRepository repository) 21 | { 22 | _logger = logger; 23 | _configuration = configuration; 24 | _repository = repository; 25 | } 26 | 27 | [HttpGet] 28 | public ResultadoContador Get() 29 | { 30 | int valorAtualContador; 31 | 32 | lock (_CONTADOR) 33 | { 34 | _CONTADOR.Incrementar(); 35 | valorAtualContador = _CONTADOR.ValorAtual; 36 | } 37 | 38 | using var activity = OpenTelemetryExtensions.CreateActivitySource() 39 | .StartActivity("Identificando"); 40 | activity?.SetTag("valorAtual", valorAtualContador); 41 | 42 | _logger.LogValorAtual(valorAtualContador); 43 | 44 | var resultado = new ResultadoContador() 45 | { 46 | ValorAtual = valorAtualContador, 47 | Producer = _CONTADOR.Local, 48 | Kernel = _CONTADOR.Kernel, 49 | Framework = _CONTADOR.Framework, 50 | Mensagem = _configuration["MensagemVariavel"] 51 | }; 52 | _repository.Insert(resultado); 53 | _logger.LogInformation($"Registro inserido com sucesso! Valor: {valorAtualContador}"); 54 | 55 | return resultado; 56 | } 57 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/Data/ContagemContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | 3 | namespace APIContagem.Data; 4 | 5 | public class ContagemContext : DbContext 6 | { 7 | public DbSet? Historicos { get; set; } 8 | 9 | public ContagemContext(DbContextOptions options) : 10 | base(options) 11 | { 12 | } 13 | 14 | protected override void OnModelCreating(ModelBuilder modelBuilder) 15 | { 16 | modelBuilder.Entity(entity => 17 | { 18 | entity.ToTable("HistoricoContagem"); 19 | entity.HasKey(c => c.Id); 20 | }); 21 | } 22 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/Data/ContagemRepository.cs: -------------------------------------------------------------------------------- 1 | using APIContagem.Models; 2 | 3 | namespace APIContagem.Data; 4 | 5 | public class ContagemRepository 6 | { 7 | private readonly ContagemContext _context; 8 | 9 | public ContagemRepository(ContagemContext context) 10 | { 11 | _context = context; 12 | } 13 | 14 | public void Insert(ResultadoContador resultado) 15 | { 16 | _context.Historicos!.Add(new() 17 | { 18 | DataProcessamento = DateTime.Now, 19 | ValorAtual = resultado.ValorAtual, 20 | Producer = resultado.Producer, 21 | Kernel = resultado.Kernel, 22 | Framework = resultado.Framework, 23 | Mensagem = resultado.Mensagem 24 | }); 25 | _context.SaveChanges(); 26 | } 27 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/Data/HistoricoContagem.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Data; 2 | 3 | public class HistoricoContagem 4 | { 5 | public int? Id { get; set; } 6 | public DateTime DataProcessamento { get; set; } 7 | public int ValorAtual { get; set; } 8 | public string? Producer { get; set; } 9 | public string? Kernel { get; set; } 10 | public string? Framework { get; set; } 11 | public string? Mensagem { get; set; } 12 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/Logging/ContagemLogging.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Logging; 2 | 3 | public static partial class ContagemLogging 4 | { 5 | [LoggerMessage(EventId = 1, Level = LogLevel.Information, 6 | Message = "Contador - Valor atual: {valorAtual}")] 7 | public static partial void LogValorAtual( 8 | this ILogger logger, int valorAtual); 9 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/Models/ResultadoContador.cs: -------------------------------------------------------------------------------- 1 | namespace APIContagem.Models; 2 | 3 | public class ResultadoContador 4 | { 5 | public int ValorAtual { get; set; } 6 | public string? Producer { get; set; } 7 | public string? Kernel { get; set; } 8 | public string? Framework { get; set; } 9 | public string? Mensagem { get; set; } 10 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using OpenTelemetry.Resources; 3 | using OpenTelemetry.Trace; 4 | using APIContagem.Data; 5 | using APIContagem.Tracing; 6 | 7 | var builder = WebApplication.CreateBuilder(args); 8 | 9 | builder.Services.AddControllers(); 10 | builder.Services.AddEndpointsApiExplorer(); 11 | builder.Services.AddSwaggerGen(); 12 | 13 | builder.Services.AddScoped(); 14 | 15 | builder.Services.AddDbContext(options => 16 | { 17 | options.UseSqlServer( 18 | builder.Configuration.GetConnectionString("BaseContagem")); 19 | }); 20 | 21 | builder.Services.AddOpenTelemetryTracing(traceProvider => 22 | { 23 | traceProvider 24 | .AddSource(OpenTelemetryExtensions.ServiceName) 25 | .SetResourceBuilder( 26 | ResourceBuilder.CreateDefault() 27 | .AddService(serviceName: OpenTelemetryExtensions.ServiceName, 28 | serviceVersion: OpenTelemetryExtensions.ServiceVersion)) 29 | .AddAspNetCoreInstrumentation() 30 | .AddSqlClientInstrumentation( 31 | options => options.SetDbStatementForText = true) 32 | .AddJaegerExporter(exporter => 33 | { 34 | exporter.AgentHost = builder.Configuration["Jaeger:AgentHost"]; 35 | exporter.AgentPort = Convert.ToInt32(builder.Configuration["Jaeger:AgentPort"]); 36 | }); 37 | }); 38 | 39 | var app = builder.Build(); 40 | 41 | app.UseSwagger(); 42 | app.UseSwaggerUI(); 43 | 44 | app.UseHttpsRedirection(); 45 | 46 | app.UseAuthorization(); 47 | 48 | app.MapControllers(); 49 | 50 | app.Run(); -------------------------------------------------------------------------------- /SqlServer/APIContagem/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:52386", 8 | "sslPort": 44398 9 | } 10 | }, 11 | "profiles": { 12 | "APIContagem": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "launchUrl": "swagger", 17 | "applicationUrl": "https://localhost:7196;http://localhost:5122", 18 | "environmentVariables": { 19 | "ASPNETCORE_ENVIRONMENT": "Development" 20 | } 21 | }, 22 | "IIS Express": { 23 | "commandName": "IISExpress", 24 | "launchBrowser": true, 25 | "launchUrl": "swagger", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /SqlServer/APIContagem/Tracing/OpenTelemetryExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace APIContagem.Tracing; 4 | 5 | public static class OpenTelemetryExtensions 6 | { 7 | public static string ServiceName { get; } 8 | public static string ServiceVersion { get; } 9 | 10 | static OpenTelemetryExtensions() 11 | { 12 | ServiceName = "APIContagemSqlServer"; 13 | ServiceVersion = typeof(OpenTelemetryExtensions).Assembly.GetName().Version!.ToString(); 14 | } 15 | 16 | public static ActivitySource CreateActivitySource() => 17 | new ActivitySource(ServiceName, ServiceVersion); 18 | } -------------------------------------------------------------------------------- /SqlServer/APIContagem/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /SqlServer/APIContagem/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "MensagemVariavel": "Testes com .NET 6 + ASP.NET Core + SQL Server", 3 | "Logging": { 4 | "LogLevel": { 5 | "Default": "Information", 6 | "Microsoft.AspNetCore": "Warning" 7 | } 8 | }, 9 | "Jaeger": { 10 | "AgentHost": "localhost", 11 | "AgentPort": "6831" 12 | }, 13 | "ConnectionStrings": { 14 | "BaseContagem": "Server=localhost;Database=BaseContagem;User Id=sa;Password=SqlServer2019!;TrustServerCertificate=True;" 15 | }, 16 | "AllowedHosts": "*", 17 | "Kestrel": { 18 | "Endpoints": { 19 | "HTTP": { 20 | "Url": "http://localhost:6100" 21 | } 22 | } 23 | } 24 | } --------------------------------------------------------------------------------