Skip to content

Commit

Permalink
graph: Add HTTP tracing option for MS Graph requests
Browse files Browse the repository at this point in the history
Debug feature enabled by ARO_MSGRAPH_TRACE environment variable.
  • Loading branch information
Matthew Barnes committed Nov 8, 2023
1 parent 83b2a55 commit e38454a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ require (
github.com/jewzaam/go-cosmosdb v0.0.0-20230924011506-8f8942a01991
github.com/jongio/azidext/go/azidext v0.5.0
github.com/microsoft/kiota-abstractions-go v1.2.0
github.com/microsoft/kiota-http-go v1.0.0
github.com/microsoft/kiota-serialization-form-go v1.0.0
github.com/microsoft/kiota-serialization-json-go v1.0.4
github.com/microsoft/kiota-serialization-multipart-go v1.0.0
Expand Down Expand Up @@ -190,7 +191,6 @@ require (
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/microsoft/kiota-authentication-azure-go v1.0.0 // indirect
github.com/microsoft/kiota-http-go v1.0.0 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/mistifyio/go-zfs/v3 v3.0.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
Expand Down
35 changes: 34 additions & 1 deletion pkg/util/graph/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ package graph
// Licensed under the Apache License 2.0.

import (
"log"
"net/http"
"net/http/httputil"
"os"

absauth "github.com/microsoft/kiota-abstractions-go/authentication"
kiotahttp "github.com/microsoft/kiota-http-go"
core "github.com/microsoftgraph/msgraph-sdk-go-core"
)

Expand All @@ -18,13 +24,40 @@ type GraphRequestAdapter struct {
core.GraphRequestAdapterBase
}

const ENV_DEBUG_TRACE = "ARO_MSGRAPH_TRACE"

type DebugTransport struct {
Transport http.RoundTripper
}

func (t *DebugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var data []byte

data, _ = httputil.DumpRequestOut(req, true)
log.Writer().Write(data)

resp, err := t.Transport.RoundTrip(req)
if err != nil {
return resp, err
}

data, _ = httputil.DumpResponse(resp, true)
log.Writer().Write(data)

return resp, err
}

// NewGraphRequestAdapter creates a new GraphRequestAdapter with the given parameters
// Parameters:
// authenticationProvider: the provider used to authenticate requests
// Returns:
// a new GraphRequestAdapter
func NewGraphRequestAdapter(authenticationProvider absauth.AuthenticationProvider) (*GraphRequestAdapter, error) {
baseAdapter, err := core.NewGraphRequestAdapterBaseWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(authenticationProvider, clientOptions, nil, nil, nil)
httpClient := kiotahttp.GetDefaultClient()
if _, doTrace := os.LookupEnv(ENV_DEBUG_TRACE); doTrace {
httpClient.Transport = &DebugTransport{Transport: httpClient.Transport}
}
baseAdapter, err := core.NewGraphRequestAdapterBaseWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(authenticationProvider, clientOptions, nil, nil, httpClient)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e38454a

Please sign in to comment.