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

graph: Add HTTP tracing option for MS Graph requests #3269

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading