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

Store Gin request query and payload #280

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion go/go-gin-mysql-collector/app/README.md

This file was deleted.

129 changes: 125 additions & 4 deletions go/go-gin-mysql-collector/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"time"
"encoding/json"
"bytes"

"github.com/gin-gonic/gin"

Expand All @@ -22,6 +25,7 @@ import (
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
"go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
)
Expand Down Expand Up @@ -56,27 +60,91 @@ func initTracer() func(context.Context) error {
hostname = "unknown"
}

res := resource.NewSchemaless(
resource := resource.NewSchemaless(
attribute.String("appsignal.config.name", os.Getenv("APPSIGNAL_APP_NAME")),
attribute.String("appsignal.config.environment", os.Getenv("APPSIGNAL_APP_ENV")),
attribute.String("appsignal.config.push_api_key", os.Getenv("APPSIGNAL_PUSH_API_KEY")),
attribute.String("appsignal.config.revision", "abcd123"),
attribute.String("appsignal.config.revision", "test-setups"),
attribute.String("appsignal.config.language_integration", "golang"),
attribute.String("appsignal.config.app_path", os.Getenv("PWD")),
attribute.String("service.name", "Gin"),
attribute.String("host.name", hostname),
attribute.StringSlice("appsignal.config.filter_function_parameters", []string{"password", "token"}),
attribute.StringSlice("appsignal.config.filter_request_query_parameters", []string{"password", "token"}),
attribute.StringSlice("appsignal.config.filter_request_payload", []string{"password", "token"}),
attribute.StringSlice("appsignal.config.filter_request_session_data", []string{"password", "token"}),
// attribute.Bool("appsignal.config.send_function_parameters", false),
)

tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(exporter),
sdktrace.WithBatcher(consoleExporter),
sdktrace.WithResource(res),
sdktrace.WithResource(resource),
)
otel.SetTracerProvider(tracerProvider)
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
return exporter.Shutdown
}

func recordParameters(c *gin.Context) {
span := trace.SpanFromContext(c.Request.Context())

// Query parameters
requestQueryParameters := c.Request.URL.Query()
attributeQueryParameters := make(map[string]any)
for k, v := range requestQueryParameters {
attributeQueryParameters[k] = v
}

if len(attributeQueryParameters) > 0 {
serializedQueryParams, err := json.Marshal(attributeQueryParameters)
if err == nil {
span.SetAttributes(attribute.String("appsignal.request.query_parameters", string(serializedQueryParams)))
}
}

// Request body payload
var serializedBodyPayload string
var payload map[string]interface{}

requestBodyPayload, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
c.Next()
return
}
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(requestBodyPayload))
contentType := c.GetHeader("Content-Type")
if contentType == "application/json" {
serializedBodyPayload = string(requestBodyPayload)
} else if contentType == "application/x-www-form-urlencoded" {
// Parse form-urlencoded body
values, err := url.ParseQuery(string(requestBodyPayload))
if err != nil {
c.Next()
return
}

// Convert form values to a JSON-compatible map
payload = make(map[string]interface{})
for key, val := range values {
if len(val) == 1 {
payload[key] = val[0]
} else {
payload[key] = val
}
}
json, _ := json.Marshal(payload)
serializedBodyPayload = string(json)
} else {
c.Next()
return
}
if len(serializedBodyPayload) > 0 {
span.SetAttributes(attribute.String("appsignal.request.payload", serializedBodyPayload))
}
c.Next()
}

func ReadFile(file string) error {
dataFile, err := os.ReadFile(file)
if err != nil {
Expand All @@ -102,6 +170,7 @@ func main() {

r := gin.New()
r.Use(otelgin.Middleware("opentelemetry-go-gin"))
r.Use(recordParameters)
r.LoadHTMLFiles("index.html")

r.GET("/file-error", func(c *gin.Context) {
Expand All @@ -126,6 +195,50 @@ func main() {
r.GET("/", func(c *gin.Context) {
time.Sleep(200 * time.Millisecond)

span := trace.SpanFromContext(c.Request.Context())

functionParams := map[string]interface{}{
"password": "super secret",
"test_function_param": "test value",
"nested": map[string]interface{}{
"password": "super secret",
"test_function_param": "test value",
},
}
span.SetAttributes(attribute.String("appsignal.function.parameters", mapToJSON(functionParams)))

queryParams := map[string]interface{}{
"password": "super secret",
"test_query": "test value",
"nested": map[string]interface{}{
"password": "super secret",
"test_query": "test value",
},
}
span.SetAttributes(attribute.String("appsignal.request.query_parameters", mapToJSON(queryParams)))

payloadData := map[string]interface{}{
"password": "super secret",
"test_payload": "test value",
"nested": map[string]interface{}{
"password": "super secret",
"test_payload": "test value",
},
}
span.SetAttributes(attribute.String("appsignal.request.payload", mapToJSON(payloadData)))

sessionData := map[string]interface{}{
"password": "super secret",
"test_payload": "test value",
"nested": map[string]interface{}{
"password": "super secret",
"test_payload": "test value",
},
}
span.SetAttributes(attribute.String("appsignal.request.session_data", mapToJSON(sessionData)))

span.SetAttributes(attribute.StringSlice("http.response.header.custom-header", []string{"abc", "def"}))

c.HTML(http.StatusOK, "index.html", gin.H{})
})

Expand All @@ -144,7 +257,7 @@ func main() {
panic(err)
}

c.JSON(http.StatusOK, bodyParams)
c.JSON(http.StatusOK, string(bodyParams))
})

r.GET("/mysql-query", func(c *gin.Context) {
Expand All @@ -161,3 +274,11 @@ func main() {
})
r.Run(":4001")
}

func mapToJSON(m map[string]interface{}) string {
jsonBytes, err := json.Marshal(m)
if err != nil {
return "{}" // Return empty JSON object on error
}
return string(jsonBytes)
}