newrelic / nri-flex

An application-agnostic, all-in-one New Relic integration integration
https://docs.newrelic.com/docs/introduction-new-relics-flex-integration
Apache License 2.0
105 stars 119 forks source link

Swedish characters like åäö is not working in Flex via PowerShell #449

Closed LLHogia closed 5 months ago

LLHogia commented 9 months ago

Description

Flex via PowerShell is not UTF-8 encoded.

I have a PowerShell script which fetches all "our" services. Extracting the EXE file and then getting the file version of each file.

Here is one of our services: image

Here is the JSON from my PowerShell script: image

Here is the result in New Relic UI: image

Expected Behavior

JSON should be UTF-8 encoded before sending to New Relic. I cannot find any way of doing this in the YML file. Result in PowerShell looks good.

Steps to Reproduce

  1. Register a Windows service named "Test Förarinformation"
  2. Create a Flex script and just write the below PowerShell directly in the YML file:
    Get-WmiObject -Class Win32_Service | Where-Object { $_.DisplayName -like "*Förarinformation*" } | Select Name, State, StartMode | ConvertTo-Json

Your Environment

OS: Windows Server 2022 Infrastructure Agent: 1.33.2

workato-integration[bot] commented 9 months ago

https://new-relic.atlassian.net/browse/NR-171718

josemore commented 5 months ago

Windows does not use UTF-8 encoding by default, instead, depending on the version it uses a different encoding format: IBM Code Page 037, IBM Code Page 860, Windows 1253, etc.

On the other hand, Golang’s JSON encoder only accepts UTF-8 valid characters: json package - encoding/json - Go Packages.

Taking into account the detailed bug, the characters åäö have a different encoding in “IBM Code Page 860” and in UTF-8, thus nri-flex (Go) is not able to properly decode them and outputs the unknown symbol. The solution in the product side might be complex as it would require detecting the encoding algorithm used by the running underlying PowerShell and initialize the corresponding decoder into valid UTF-8 characters. Meanwhile, there are a few simpler solutions on the client’s side:

Enable UTF-8 globally in the Windows machine: Using UTF-8 Encoding (CHCP 65001) in Command Prompt / Windows Powershell (Windows 10)

Enable UTF-8 in the spawned PowerShell context, this can be done by running the following command in the shell script:

[Console]::OutputEncoding = [System.Text.Encoding]::UTF8;

// Fixed script

integrations:
    - name: nri-flex
      interval: 60s
      config:
          name: FlexSystemSample
          apis:
            - event type: FlexSystemSample 
              shell: powershell
              commands:
                  - run: '[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Get-WmiObject -Class Win32_Service | Where-Object { $_.Name -like "Förarinformation" } | Select Name, State, StartMode | ConvertTo-Json'

Let us know if any of the client’s side solution would work.

workato-integration[bot] commented 5 months ago

The issue will not be fixed.