Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
obsessive coding disorder

Giacomo Stelluti Scala gsscoder

💭
obsessive coding disorder
View GitHub Profile
@gsscoder
gsscoder / JsonUtil_Prettify.cs
Last active Mar 25, 2021
C# helper method to pretty print JSON (ILogger friendly)
View JsonUtil_Prettify.cs
// based on: https://gist.github.com/FrankHu-MSFT/b6750185b19fd4ada4ba36b099985813
// can removes \r to fit in a single log line when using ILogger
using System.IO;
using Newtonsoft.Json;
static class JsonUtil
{
public static string Prettify(string json, bool stripLineFeed = false)
{
using var stringReader = new StringReader(json);
@gsscoder
gsscoder / AvailabilityCanaryFunction.cs
Last active Mar 19, 2021
Simple C# availability canary Function App
View AvailabilityCanaryFunction.cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
namespace HealthTest2
{
@gsscoder
gsscoder / ChaosHealthCheck.cs
Created Mar 18, 2021
Basic IHealthCheck implementation that injects chaos
View ChaosHealthCheck.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Polly.Contrib.Simmy;
using Polly.Contrib.Simmy.Outcomes;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using CSharpx;
sealed class ChaosHealthCheck : IHealthCheck
{
@gsscoder
gsscoder / checksum.bat
Last active Mar 13, 2021
Batch script to compare a file SHA256 checksum to a given one
View checksum.bat
@echo off
setlocal enabledelayedexpansion
::Batch script to compare a file SHA256 checksum to a given one.
::Usage: checksum [FILE] [VALUE]
set filepath=%1
set checksum=%2
set idx=0
@gsscoder
gsscoder / query_appinsights_rest.ps1
Last active Mar 12, 2021
PowerShell code to query Azure App Insights using REST API
View query_appinsights_rest.ps1
$aiAppId = '00000000-0000-0000-0000-000000000000'
$aiApiKey = 'ehzgdrES4kBJNCuRUzcBfkuka5CGWkAr4hyhJ6y3'
$result = Invoke-RestMethod -Uri "https://api.applicationinsights.io/v1/apps/$aiAppId/query" `
-Method Post -UseBasicParsing `
-Headers @{
'x-api-key' = $aiApiKey
'content-type' = 'application/json'
} `
-Body '{"query": "traces | where timestamp <= ago(1m) | limit 10"}'
@gsscoder
gsscoder / chaos3.fsx
Last active Feb 27, 2021
Simple F# function that returns an asynchronous function that may inject chaos
View chaos3.fsx
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos3.fsx
// latency test
// 0.490776
open System
open System.Threading
let chaosAsync (name:string) (shouldChaosAsync:unit -> Async<bool>) (chaosAsync:unit -> Async<unit>) =
fun (serviceAsync:unit -> Async<'t>) ->
async {
@gsscoder
gsscoder / chaos2.fsx
Last active Feb 27, 2021
Simple F# function that returns a function that may inject chaos
View chaos2.fsx
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos2.fsx
// latency test
// 0.969491
open System
open System.Threading
let chaos (name:string) (shouldChaos:unit -> bool) (chaos:unit -> unit) =
fun (service:unit -> 't) ->
if shouldChaos () then
@gsscoder
gsscoder / chaos.fsx
Last active Feb 27, 2021
Simple F# function that may inject chaos
View chaos.fsx
// inspired by https://www.youtube.com/watch?v=RWyZkNzvC-c&t=553s
// $ dotnet fsi chaos.fsx
// latency test
// 0.919409
open System
open System.Threading
let chaos (name:string) (shouldChaos:unit -> bool) (chaos:unit -> unit) (service:unit -> 't) =
if shouldChaos () then
printfn "%s" name
@gsscoder
gsscoder / verbose.ps1
Created Jan 12, 2021
PowerShell code to test if -Verbose switch is enabled
View verbose.ps1
[CmdletBinding()]
param ()
Set-StrictMode -Version Latest
$verbose = if ($PSBoundParameters.ContainsKey('Verbose')) {
$PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent } else { $false }
"Verbose enabled: $verbose" | Write-Host
@gsscoder
gsscoder / bearer_token.ps1
Created Dec 30, 2020
PowerShell code to get the bearer token using an app registration
View bearer_token.ps1
$tenantId = 'your_tenant_id'
$clientId = 'your_client_id'
$clientSecret = 'your_client_secret'
$tokenResponse = (Invoke-WebRequest -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" `
-Method Post -UseBasicParsing `
-Body "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret").Content
"$(($tokenResponse | ConvertFrom-Json).access_token)`n" | Write-Host