Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request - Example with gzip Content-Encoding #5

Open
o-l-a-v opened this issue Dec 17, 2024 · 2 comments
Open

Feature request - Example with gzip Content-Encoding #5

o-l-a-v opened this issue Dec 17, 2024 · 2 comments

Comments

@o-l-a-v
Copy link

o-l-a-v commented Dec 17, 2024

In my testing, setting Content-Encoding: "gzip" with body being plaintext JSON, DCR diagnostic log category DcrLogErrors ( https://learn.microsoft.com/en-us/azure/azure-monitor/reference/supported-logs/microsoft-insights-datacollectionrules-logs ) returns error Compressed data in invalid..

Would be nice with an example where you compress the content to GZIP so it works with Content-Encoding: "gzip".

One can likely save a lot of data on logs files in transit, which are both text and has a lot of repetative content. Example / playground:

The Log Ingestion API limitations is 1MB, compressed or not.

@o-l-a-v
Copy link
Author

o-l-a-v commented Dec 18, 2024

Managed to do it by:

  • Convert Body from [string] with JSON content, to Gzipped byte array:

    $ByteArray = [byte[]]($Body.ToCharArray().ForEach{$_ -as [byte]})
    $MemoryStream = [IO.MemoryStream]::new()
    $CompressionStream = [System.IO.Compression.GZipStream]::new($MemoryStream, [Io.Compression.CompressionMode]'Compress')
    $CompressionStream.Write($ByteArray, 0, $ByteArray.'Length')
    $CompressionStream.Close()
  • Ingest with header Content-Encoding: gzip and -Body ($MemoryStream.ToArray() -as [byte[]])

    # Create headers
    $Headers = [ordered]@{
        'Authorization' = [string] 'Bearer {0}' -f (
            ConvertFrom-SecureString -AsPlainText -SecureString (
                (Get-AzAccessToken -AsSecureString -ResourceUrl 'https://monitor.azure.com' -WarningAction 'Ignore').'Token'
            )
        )
        'Content-Type'  = [string] 'application/json'
    }
    
    # Ingest without compression
    $UploadResponse = Invoke-RestMethod -Uri $Uri -Method 'Post' -Body $Body -Headers $Headers -StatusCodeVariable 'StatusCode' -ResponseHeadersVariable 'ResponseHeaders'
    
    # Ingest with gzip compression (add Content-Encoding to headers)
    $UploadResponse = Invoke-RestMethod -Uri $Uri -Method 'Post' -Body (
        $MemoryStream.ToArray() -as [byte[]]
    ) -Headers (
        $Headers + @{'Content-Encoding' = [string] 'gzip'}
    ) -StatusCodeVariable 'StatusCode' -ResponseHeadersVariable 'ResponseHeaders'
    
    # Check output
    @{
        'StatusCode'      = [uint16] $StatusCode
        'ResponseHeaders' = $ResponseHeaders
    } | ConvertTo-Json

@KnudsenMorten
Copy link
Owner

KnudsenMorten commented Dec 18, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants