From a35d545f87216dce39a4f1f7c9e049828f83319d Mon Sep 17 00:00:00 2001 From: Thiyagu55 <64461612+Thiyagu55@users.noreply.github.com> Date: Fri, 30 Sep 2022 18:15:51 +0530 Subject: [PATCH] Added go core implementation (#163) --- .github/workflows/go-sql-tests.yaml | 8 +-- go/core/README.md | 28 ++++++++ go/core/core.go | 99 +++++++++++++++++++++++++++++ go/core/go.mod | 7 ++ go/core/go.sum | 11 ++++ 5 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 go/core/README.md create mode 100644 go/core/core.go create mode 100644 go/core/go.mod create mode 100644 go/core/go.sum diff --git a/.github/workflows/go-sql-tests.yaml b/.github/workflows/go-sql-tests.yaml index f7d92a45..e71b3e6b 100644 --- a/.github/workflows/go-sql-tests.yaml +++ b/.github/workflows/go-sql-tests.yaml @@ -1,14 +1,14 @@ -name: go-sql package test +name: go packages test on: push: branches: - master paths: - - go/go-sql/** + - go/** pull_request: paths: - - go/go-sql/** + - go/** jobs: unittests: @@ -25,7 +25,7 @@ jobs: run: go build -v ./... working-directory: ./go/go-sql - - name: Test + - name: Test go-sql run: go test -v ./... working-directory: ./go/go-sql diff --git a/go/core/README.md b/go/core/README.md new file mode 100644 index 00000000..326bc3b7 --- /dev/null +++ b/go/core/README.md @@ -0,0 +1,28 @@ +# SQLCommenter Core [In development] + +SQLcommenter is a plugin/middleware/wrapper to augment application related information/tags with SQL Statements that can be used later to correlate user code with SQL statements. + +This package contains configuration options, framework interface and support functions for all the sqlcommenter go modules + +## Installation + +This is a support package and will be installed indirectly by other go sqlcommenter packages + +## Usages + +### Configuration + +Users are given control over what tags they want to append by using `core.CommenterOptions` struct. + +```go +type CommenterOptions struct { + EnableDBDriver bool + EnableTraceparent bool // OpenTelemetry trace information + EnableRoute bool // applicable for web frameworks + EnableFramework bool // applicable for web frameworks + EnableController bool // applicable for web frameworks + EnableAction bool // applicable for web frameworks + } +``` + + diff --git a/go/core/core.go b/go/core/core.go new file mode 100644 index 00000000..65bf7194 --- /dev/null +++ b/go/core/core.go @@ -0,0 +1,99 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package core + +import ( + "context" + "fmt" + "net/url" + "reflect" + "runtime" + "sort" + "strings" + + "go.opentelemetry.io/otel/propagation" +) + +const ( + Route string = "route" + Controller string = "controller" + Action string = "action" + Framework string = "framework" + Driver string = "driver" + Traceparent string = "traceparent" +) + +type CommenterOptions struct { + EnableDBDriver bool + EnableRoute bool + EnableFramework bool + EnableController bool + EnableAction bool + EnableTraceparent bool +} + +func encodeURL(k string) string { + return url.QueryEscape(string(k)) +} + +func GetFunctionName(i interface{}) string { + if i == nil { + return "" + } + return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() +} + +func ConvertMapToComment(tags map[string]string) string { + var sb strings.Builder + i, sz := 0, len(tags) + + //sort by keys + sortedKeys := make([]string, 0, len(tags)) + for k := range tags { + sortedKeys = append(sortedKeys, k) + } + sort.Strings(sortedKeys) + + for _, key := range sortedKeys { + if i == sz-1 { + sb.WriteString(fmt.Sprintf("%s=%v", encodeURL(key), encodeURL(tags[key]))) + } else { + sb.WriteString(fmt.Sprintf("%s=%v,", encodeURL(key), encodeURL(tags[key]))) + } + i++ + } + return sb.String() +} + +func ExtractTraceparent(ctx context.Context) propagation.MapCarrier { + // Serialize the context into carrier + textMapPropogator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}) + carrier := propagation.MapCarrier{} + textMapPropogator.Inject(ctx, carrier) + return carrier +} + +type RequestExtractor interface { + Route() string + Action() string + Framework() string +} + +func ContextInject(ctx context.Context, h RequestExtractor) context.Context { + ctx = context.WithValue(ctx, Route, h.Route()) + ctx = context.WithValue(ctx, Action, h.Action()) + ctx = context.WithValue(ctx, Framework, h.Framework()) + return ctx +} diff --git a/go/core/go.mod b/go/core/go.mod new file mode 100644 index 00000000..0cb7633c --- /dev/null +++ b/go/core/go.mod @@ -0,0 +1,7 @@ +module github.com/google/sqlcommenter/go/core + +go 1.19 + +require go.opentelemetry.io/otel v1.10.0 + +require go.opentelemetry.io/otel/trace v1.10.0 // indirect diff --git a/go/core/go.sum b/go/core/go.sum new file mode 100644 index 00000000..c9ab1f94 --- /dev/null +++ b/go/core/go.sum @@ -0,0 +1,11 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= +go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4= +go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ= +go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E= +go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=