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

VB.NET Integrate Authorization token in Header #5738

Open
Keerthii-r opened this issue Jul 4, 2024 · 4 comments
Open

VB.NET Integrate Authorization token in Header #5738

Keerthii-r opened this issue Jul 4, 2024 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@Keerthii-r
Copy link

Feature Request

Are you requesting automatic instrumentation for a framework or library? Please describe.
We are using OpenTelemetry SDK

  • Framework or library name : ASP.NET VB.NET
  • Library type: .NET Framework 4.8
  • Library version: 4.8

Describe the solution you'd like
We followed everything as per the git link
https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/main/src/OpenTelemetry.Instrumentation.AspNet

We managed to get telemetry data through Console output, however we can't able to send data to Grafana as Token needs to be pass in header.

Code we did so far in Global.asax.vb file

Imports OpenTelemetry
Imports OpenTelemetry.Exporter
Imports OpenTelemetry.Metrics
Imports OpenTelemetry.Resources
Imports OpenTelemetry.Trace

Public Class Global_asax
Inherits HttpApplication

Private _TracerProvider As TracerProvider
Private _MeterProvider As MeterProvider

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)

    Dim otlpEndpoint As String = ConfigurationManager.AppSettings("OtlpEndpoint")
    Dim otlpToken As String = ConfigurationManager.AppSettings("OtlpToken")

    ' Create custom resource attributes
    Dim resourceBuilder As ResourceBuilder = ResourceBuilder.CreateDefault().
                                          AddAttributes(New Dictionary(Of String, Object) From {
                                            {"deployment.environment", "dev"},
                                            {"version", "4.1"}
                                          })

    ' Create a dictionary to hold the headers
    Dim headers As New Dictionary(Of String, String)()
    headers.Add("Authorization ", "Basic " & otlpToken)

    ' Convert the dictionary to a format acceptable by the Headers property
    Dim headersString As String = String.Join(",", headers.Select(Function(kvp) kvp.Key & "=" & kvp.Value))

    Try
        _TracerProvider = Sdk.CreateTracerProviderBuilder().
           AddAspNetInstrumentation().
           AddHttpClientInstrumentation().
           AddOtlpExporter(Function(otlpOptions)
                               otlpOptions.Endpoint = New Uri(otlpEndpoint)
                               otlpOptions.Protocol = OtlpExportProtocol.Grpc
                               otlpOptions.Headers = headersString
                           End Function).
           AddConsoleExporter(Function(options)
                                  options.Targets = ConsoleExporterOutputTargets.Debug
                              End Function).
           SetResourceBuilder(resourceBuilder).
           Build()

        _MeterProvider = Sdk.CreateMeterProviderBuilder().
           AddAspNetInstrumentation().
           AddHttpClientInstrumentation().
           AddOtlpExporter(Function(otlpOptions)
                               otlpOptions.Endpoint = New Uri(otlpEndpoint)
                               otlpOptions.Protocol = OtlpExportProtocol.Grpc
                               otlpOptions.Headers = headersString
                           End Function).
           AddConsoleExporter(Function(options)
                                  options.Targets = ConsoleExporterOutputTargets.Debug
                              End Function).
           SetResourceBuilder(resourceBuilder).
           Build()

    Catch ex As Exception
        Console.WriteLine(ex.Message)

    End Try
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
    _TracerProvider?.Dispose()
    _MeterProvider?.Dispose()
End Sub

End Class

Extra Notes:
We need to add and remove some of the predefined attributes.
And also the output of the telemetry data should be in different formats (Ex. JSON, CSV, ..)

@pellared pellared added the question Further information is requested label Jul 4, 2024
@pellared
Copy link
Member

pellared commented Jul 4, 2024

Have you tried adding using OTEL_EXPORTER_OTLP_HEADERS env var? More:

@Kielek Kielek transferred this issue from open-telemetry/opentelemetry-dotnet-instrumentation Jul 4, 2024
@Keerthii-r
Copy link
Author

Yes we did that too but no luck.
Is there any example project available with the web.config files included.

@Kielek
Copy link
Contributor

Kielek commented Jul 4, 2024

@matt-hensley, could you please advice?

@Keerthii-r
Copy link
Author

Keerthii-r commented Jul 8, 2024

Hi @pellared / @Kielek

I managed to fix it. Its sending my logs, metrices and traces to Grafana while testing.
How ever when I published my code to IIS its not sending any.
I think it may be some GRPC protocol issue. Let me know if you faces this issue before.
I will post my code here so that it may be helpful for someone.

Imports OpenTelemetry
Imports OpenTelemetry.Logs
Imports OpenTelemetry.Exporter
Imports OpenTelemetry.Metrics
Imports OpenTelemetry.Resources
Imports OpenTelemetry.Trace

Public Class Global_asax
Inherits HttpApplication

Private _TracerProvider As TracerProvider
Private _MeterProvider As MeterProvider

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    log4net.Config.XmlConfigurator.Configure()

    Dim otlpEndpoint As String = ConfigurationManager.AppSettings("OtlpEndpoint")
    Dim otlpToken As String = ConfigurationManager.AppSettings("OtlpToken")

    ' Create custom resource attributes
    Dim resourceBuilder As ResourceBuilder = ResourceBuilder.CreateDefault().
                                          AddService(serviceName:="Your Service Name", serviceVersion:="V1.0").
                                          AddAttributes(New Dictionary(Of String, Object) From {
                                            {"deployment.environment", "dev"},
                                            {"system_name", "..."},
                                            {"team", "..."},
                                            {"version", "1.0"}
                                          })

    ' Create a dictionary to hold the headers
    Dim headers As New Dictionary(Of String, String)()
    headers.Add("Authorization ", "Basic " & otlpToken)

    ' Convert the dictionary to a format acceptable by the Headers property
    Dim headersString As String = String.Join(",", headers.Select(Function(kvp) kvp.Key & "=" & kvp.Value))

    Dim _LoggerFactory = LoggerFactory.Create(Function(builder)
                                              builder.AddOpenTelemetry(Function(logging)
                                                                           logging.AddOtlpExporter(Function(otlpOptions)
                                                                                                       otlpOptions.Endpoint = New Uri(otlpEndpoint)
                                                                                                       otlpOptions.Protocol = OtlpExportProtocol.Grpc
                                                                                                       otlpOptions.Headers = headersString
                                                                                                   End Function)
                                                                           logging.SetResourceBuilder(resourceBuilder)
                                                                       End Function)
                                          End Function)

    Dim OTELlogger = _LoggerFactory.CreateLogger(Of Logger)()
    OTELlogger.LogInformation("Information..")

    _TracerProvider = Sdk.CreateTracerProviderBuilder().
           AddAspNetInstrumentation().
           AddHttpClientInstrumentation().
           AddOtlpExporter(Function(otlpOptions)
                               otlpOptions.Endpoint = New Uri(otlpEndpoint)
                               otlpOptions.Protocol = OtlpExportProtocol.Grpc
                               otlpOptions.Headers = headersString
                           End Function).
           SetResourceBuilder(resourceBuilder).
           Build()

    _MeterProvider = Sdk.CreateMeterProviderBuilder().
           AddAspNetInstrumentation().
           AddHttpClientInstrumentation().
           AddOtlpExporter(Function(otlpOptions)
                               otlpOptions.Endpoint = New Uri(otlpEndpoint)
                               otlpOptions.Protocol = OtlpExportProtocol.Grpc
                               otlpOptions.Headers = headersString
                           End Function).
           SetResourceBuilder(resourceBuilder).
           Build()

End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ResourceProviderConfig.Current.ResourceProvider = New APIResourceProvider()
End Sub

Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    If (ConfigurationManager.AppSettings("EnableErrorPage").ToUpper = "TRUE") Then
        Server.Transfer("/ErrorPage.aspx")
    End If
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
    _TracerProvider?.Dispose()
    _MeterProvider?.Dispose()
End Sub

End Class


Web.config File

<system.web>



</system.web>

<system.webServer>









</system.webServer>

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

No branches or pull requests

4 participants