Skip to content

Commit

Permalink
added open telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Oct 2, 2023
1 parent 8e86283 commit 787c76f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
46 changes: 41 additions & 5 deletions src/VahterBanBot/Program.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
open System
open System.Collections.Generic
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Logging
Expand All @@ -12,6 +12,11 @@ open VahterBanBot.Cleanup
open VahterBanBot.Utils
open VahterBanBot.Bot
open VahterBanBot.Types
open OpenTelemetry.Trace
open OpenTelemetry.Metrics
open OpenTelemetry.Resources
open Npgsql
open Azure.Monitor.OpenTelemetry.AspNetCore

type Root = class end

Expand Down Expand Up @@ -44,11 +49,39 @@ let builder = WebApplication.CreateBuilder()
let options = TelegramBotClientOptions(botConf.BotToken)
TelegramBotClient(options, httpClient) :> ITelegramBotClient)

%builder.Logging.AddConsole()
let otelBuilder =
builder.Services
.AddOpenTelemetry()
.WithTracing(fun builder ->
%builder
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
.AddNpgsql()
.ConfigureResource(fun res ->
%res.AddAttributes [
KeyValuePair("service.name", "vahter-ban-bot")
]
)
getEnvWith "OTEL_EXPORTER_ZIPKIN_ENDPOINT" (fun _ ->
%builder.AddZipkinExporter()
)
getEnvWith "OTEL_EXPORTER_CONSOLE" (bool.Parse >> fun otelConsole ->
if otelConsole then %builder.AddConsoleExporter()
)
)
.WithMetrics(fun builder ->
%builder
.AddHttpClientInstrumentation()
.AddAspNetCoreInstrumentation()
getEnvWith "OTEL_EXPORTER_CONSOLE" (bool.Parse >> fun otelConsole ->
if otelConsole then %builder.AddConsoleExporter()
)
)

match Environment.GetEnvironmentVariable "APPLICATIONINSIGHTS_CONNECTION_STRING" with
| null -> ()
| appInsightKey ->
getEnvWith "APPLICATIONINSIGHTS_CONNECTION_STRING" (fun appInsightKey ->
%otelBuilder.UseAzureMonitor(fun options ->
options.ConnectionString <- appInsightKey
)
%builder.Logging.AddApplicationInsights(
configureTelemetryConfiguration = (fun config ->
config.ConnectionString <- appInsightKey
Expand All @@ -57,6 +90,9 @@ match Environment.GetEnvironmentVariable "APPLICATIONINSIGHTS_CONNECTION_STRING"
()
)
)
)

%builder.Logging.AddConsole()

let webApp = choose [
// need for Azure health checks on / route
Expand Down
5 changes: 5 additions & 0 deletions src/VahterBanBot/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ let getEnvOr name defaultValue =
let value = Environment.GetEnvironmentVariable name
if value = null then defaultValue else value

let getEnvWith name action =
let value = Environment.GetEnvironmentVariable name
if value <> null then
action value

let prependUsername (s: string) =
if isNull s then
null
Expand Down
7 changes: 7 additions & 0 deletions src/VahterBanBot/VahterBanBot.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.0.0-beta.7" />
<PackageReference Include="Dapper" Version="2.0.151" />
<PackageReference Include="Dapper.FSharp" Version="4.7.0" />
<PackageReference Include="Giraffe" Version="6.2.0" />
Expand All @@ -23,6 +24,12 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.11" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.21.0" />
<PackageReference Include="Npgsql" Version="7.0.6" />
<PackageReference Include="Npgsql.OpenTelemetry" Version="7.0.6" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.6.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.5.1-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.5.1-beta.1" />
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 787c76f

Please sign in to comment.