Skip to content

Commit

Permalink
Update tutorial to use v5 packages and template (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
wazzamatazz authored Sep 26, 2024
1 parent 87342c5 commit 94f85aa
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 60 deletions.
4 changes: 3 additions & 1 deletion docs/tutorials/mqtt-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,12 @@ When an MQTT message is processed, a tag definition will be created if one does

The adapter constructor accepts an `IOptionsMonitor<MyAdapterOptions>` parameter. Since `IOptionsMonitor<T>` supports change notifications, this allows the adapter's settings in `adaptersettings.json` to be modified at runtime and the adapter will automatically reconfigure itself to use the updated settings. Runtime changes are handled by overriding the `OnOptionsChange` method from the `AdapterBase<TAdapterOptions>` base class.

### Telemetry
### Telemetry and Observability

The `MyAdapter` class creates a static metric counter that is incremented every time a message is received from the MQTT broker. The counter can be observed via the [dotnet-counters](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-counters) tool (by observing the `IntelligentPlant.AppStoreConnect.Adapter:mqtt.messages_received` counter) or via the adapter host's Prometheus scraping endpoint at `/metrics`.

Additionally, you can export any combination of traces, logs and/or metrics to a compatible OTLP collector. See [here](../../adapter-host-logging.md) for more information.


# Update the Adapter Options Editor Form

Expand Down
39 changes: 19 additions & 20 deletions examples/SimpleMqttExample/MqttAdapter/MqttAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,27 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter" Version="4.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.Grpc" Version="4.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.HealthChecks" Version="4.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.MinimalApi" Version="4.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.SignalR" Version="4.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.KeyValueStore.Sqlite" Version="4.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.OpenTelemetry" Version="4.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter" Version="5.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.Grpc" Version="5.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.HealthChecks" Version="5.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.MinimalApi" Version="5.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.AspNetCore.SignalR" Version="5.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.KeyValueStore.Sqlite" Version="5.0.0" />
<PackageReference Include="IntelligentPlant.AppStoreConnect.Adapter.OpenTelemetry" Version="5.0.0" />
<PackageReference Include="Jaahas.OpenTelemetry.Extensions" Version="2.0.2" />
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.175" />
<PackageReference Include="MQTTnet.Extensions.ManagedClient" Version="4.3.3.952" />
<PackageReference Include="OpenTelemetry" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Api" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.8.0-rc.1" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.8.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.8.0-beta.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageReference Include="Serilog.Expressions" Version="4.0.0" />
<PackageReference Include="OpenTelemetry" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Api" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.Prometheus.AspNetCore" Version="1.9.0-beta.2" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.9.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.9.0-beta.1" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions examples/SimpleMqttExample/MqttAdapter/MqttAdapter.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MqttAdapter", "MqttAdapter.csproj", "{ACFEAA17-BF3A-4D6D-831A-A0F390866262}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ACFEAA17-BF3A-4D6D-831A-A0F390866262}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ACFEAA17-BF3A-4D6D-831A-A0F390866262}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ACFEAA17-BF3A-4D6D-831A-A0F390866262}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ACFEAA17-BF3A-4D6D-831A-A0F390866262}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3F386DB8-E61D-42BE-94E3-05265DA89C89}
EndGlobalSection
EndGlobal
20 changes: 6 additions & 14 deletions examples/SimpleMqttExample/MqttAdapter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

using MqttAdapter;

using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

using Serilog;

// The [VendorInfo] attribute is used to add vendor information to the adapters in this assembly,
// as well as the host information for the application.
[assembly: DataCore.Adapter.VendorInfo("My Company", "https://my-company.com")]
Expand All @@ -17,15 +16,6 @@
// Our adapter settings are stored in adaptersettings.json.
builder.Configuration.AddJsonFile(Constants.AdapterSettingsFilePath, false, true);

// Configure logging using Serilog. Additional logging destinations such as files can be added
// using appsettings.json. See https://github.com/serilog/serilog-settings-configuration for more
// information.
builder.Host.UseSerilog((context, services, configuration) => {
configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext();
});

// Parent PID. If specified, we will gracefully shut down if the parent process exits.
var pid = builder.Configuration.GetValue<int>("AppStoreConnect:Adapter:Host:ParentPid");
Expand Down Expand Up @@ -99,6 +89,10 @@
builder.Services
.AddOpenTelemetry()
.ConfigureResource(resourceBuilder => resourceBuilder.AddDataCoreAdapterApiService(instanceId))
// Export telemetry signals using OTLP (OpenTelemetry Protocol). Exporter settings can be
// configured in appsettings.json. See https://github.com/wazzamatazz/opentelemetry-extensions#configuring-a-multi-signal-opentelemetry-protocol-otlp-exporter
// for more information on configuring the OTLP exporter.
.AddOtlpExporter(builder.Configuration)
.WithTracing(otel => otel
// Records incoming HTTP requests made to the adapter host.
.AddAspNetCoreInstrumentation()
Expand All @@ -109,9 +103,7 @@
// Records queries made by System.Data.SqlClient and Microsoft.Data.SqlClient.
.AddSqlClientInstrumentation()
// Records activities created by adapters and adapter hosting packages.
.AddDataCoreAdapterInstrumentation()
// Exports traces in OTLP format using default settings (i.e. http://localhost:4317 using OTLP/gRPC format).
.AddOtlpExporter())
.AddDataCoreAdapterInstrumentation())
.WithMetrics(otel => otel
// Observe instrumentation for the .NET runtime.
.AddRuntimeInstrumentation()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"Serilog": {
"MinimumLevel": {
"Logging": {
"LogLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
30 changes: 11 additions & 19 deletions examples/SimpleMqttExample/MqttAdapter/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
{
"Serilog": {
"MinimumLevel": {
"Logging": {
"LogLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": {
"type": "Serilog.Templates.ExpressionTemplate, Serilog.Expressions",
"template": "{@l:w4}: {SourceContext}[{Coalesce(EventId.Id, '0')}]{#if rest(true) <> {}} {rest(true)}{#end}\n {@m}\n{@x}",
"theme": "Serilog.Templates.Themes.TemplateTheme::Code, Serilog.Expressions"
}
}
}
]
"Microsoft": "Warning"
}
},
"AllowedHosts": "*",
"Kestrel": {
Expand All @@ -37,5 +22,12 @@
"InstanceId": "78339989-3A76-426E-BC12-5528EF6A8C33"
}
}
},
"OpenTelemetry": {
"Exporters": {
"OTLP": {
"Enabled": true
}
}
}
}

0 comments on commit 94f85aa

Please sign in to comment.