Skip to content

Commit 59a0bd1

Browse files
authored
Merge branch 'development' into grpc-improvement
2 parents 429d5a3 + e3e036c commit 59a0bd1

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

.github/workflows/go.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ jobs:
279279
upload_coverage:
280280
name: Upload Coverage📊
281281
runs-on: ubuntu-latest
282+
env:
283+
QLTY_TOKEN: ${{ secrets.QLTY_TOKEN }}
284+
QLTY_COVERAGE_TOKEN: ${{ secrets.QLTY_TOKEN }}
285+
282286
# This job only needs example and pkg test results, not submodules
283287
needs: [Example-Unit-Testing, PKG-Unit-Testing]
284288
# Only run this job on pushes to the development branch
@@ -288,7 +292,9 @@ jobs:
288292
uses: actions/checkout@v5
289293

290294
- name: Install qlty CLI
291-
run: curl -s https://qlty.sh/install | bash
295+
run: |
296+
curl https://qlty.sh | sh
297+
echo "$HOME/.qlty/bin" >> $GITHUB_PATH
292298
293299
# Download coverage artifacts
294300
- name: Download Coverage Report
@@ -311,7 +317,8 @@ jobs:
311317

312318
# Upload merged coverage to CodeClimate for analysis
313319
- name: Upload
314-
run: qlty coverage publish --input merged_profile.cov
320+
working-directory: artifacts
321+
run: qlty coverage publish merged_profile.cov --format=coverprofile --strip-prefix="gofr.dev/" --add-prefix="${GITHUB_WORKSPACE}/"
315322
env:
316323
QLTY_TOKEN: ${{ secrets.QLTY_TOKEN }}
317324

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
</p>
1010
<a href="https://pkg.go.dev/gofr.dev"><img src="https://img.shields.io/badge/GoDoc-Read%20Documentation-blue?style=for-the-badge" alt="godoc"></a>
1111
<a href="https://gofr.dev/docs"><img src="https://img.shields.io/badge/GoFr-Docs-orange?style=for-the-badge" alt="gofr-docs"></a>
12-
<a href="https://codeclimate.com/github/gofr-dev/gofr/maintainability"><img src="https://img.shields.io/codeclimate/maintainability/gofr-dev/gofr?style=for-the-badge" alt="maintainability"></a>
13-
<a href="https://codeclimate.com/github/gofr-dev/gofr/test_coverage"><img src="https://img.shields.io/codeclimate/coverage/gofr-dev/gofr?style=for-the-badge" alt="test-coverage"></a>
12+
<a href="https://qlty.sh/gh/gofr-dev/projects/gofr"><img src="https://qlty.sh/gh/gofr-dev/projects/gofr/maintainability.svg" alt="Maintainability" /></a>
13+
<a href="https://qlty.sh/gh/gofr-dev/projects/gofr"><img src="https://qlty.sh/gh/gofr-dev/projects/gofr/coverage.svg" alt="Code Coverage" /></a>
1414
<a href="https://goreportcard.com/report/gofr.dev"><img src="https://goreportcard.com/badge/gofr.dev?style=for-the-badge" alt="Go Report Card"></a>
1515
<a href="https://opensource.org/licenses/Apache-2.0"><img src="https://img.shields.io/badge/License-Apache_2.0-blue?style=for-the-badge" alt="Apache 2.0 License"></a>
1616
<a href="https://discord.gg/wsaSkQTdgq"><img src="https://img.shields.io/badge/discord-join-us?style=for-the-badge&logo=discord&color=7289DA" alt="discord" /></a>

pkg/gofr/context.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,7 @@ func newCMDContext(w Responder, r Request, c *container.Container, out terminal.
177177
ContextLogger: *logging.NewContextLogger(r.Context(), c.Logger),
178178
}
179179
}
180+
181+
func (c *Context) GetCorrelationID() string {
182+
return trace.SpanFromContext(c).SpanContext().TraceID().String()
183+
}

pkg/gofr/context_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/stretchr/testify/require"
1616
"go.opentelemetry.io/otel"
1717
"go.opentelemetry.io/otel/sdk/trace"
18+
"go.opentelemetry.io/otel/sdk/trace/tracetest"
1819

1920
"gofr.dev/pkg/gofr/config"
2021
"gofr.dev/pkg/gofr/container"
@@ -234,3 +235,30 @@ func TestGetAuthInfo_JWTClaims(t *testing.T) {
234235

235236
assert.Equal(t, claims, res)
236237
}
238+
239+
func TestContext_GetCorrelationID(t *testing.T) {
240+
// Setup OpenTelemetry tracer
241+
exporter := tracetest.NewInMemoryExporter()
242+
tp := trace.NewTracerProvider(trace.WithSyncer(exporter))
243+
otel.SetTracerProvider(tp)
244+
tracer := tp.Tracer("test")
245+
246+
t.Run("with span", func(t *testing.T) {
247+
ctx, span := tracer.Start(t.Context(), "test-span")
248+
defer span.End()
249+
250+
gofCtx := &Context{Context: ctx}
251+
correlationID := gofCtx.GetCorrelationID()
252+
253+
assert.Len(t, correlationID, 32, "Expected correlation ID length 32, got %d", len(correlationID))
254+
assert.NotEqual(t, "00000000000000000000000000000000", correlationID, "Expected non-empty correlation ID")
255+
})
256+
257+
t.Run("without span", func(t *testing.T) {
258+
gofCtx := &Context{Context: t.Context()}
259+
correlationID := gofCtx.GetCorrelationID()
260+
261+
expected := "00000000000000000000000000000000"
262+
assert.Equal(t, expected, correlationID, "Expected empty TraceID when no span present")
263+
})
264+
}

pkg/gofr/datasource/sql/db.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func clean(query string) string {
4545
}
4646

4747
func (d *DB) sendOperationStats(start time.Time, queryType, query string, args ...any) {
48-
duration := time.Since(start).Microseconds()
48+
duration := time.Since(start).Milliseconds()
4949

5050
d.logger.Debug(&Log{
5151
Type: queryType,
@@ -129,7 +129,7 @@ type Tx struct {
129129
}
130130

131131
func (t *Tx) sendOperationStats(start time.Time, queryType, query string, args ...any) {
132-
duration := time.Since(start).Microseconds()
132+
duration := time.Since(start).Milliseconds()
133133

134134
t.logger.Debug(&Log{
135135
Type: queryType,

pkg/gofr/datasource/sql/db_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,3 +1227,27 @@ func TestDB_Begin_Error(t *testing.T) {
12271227
require.Error(t, err)
12281228
assert.Nil(t, tx)
12291229
}
1230+
1231+
func TestDB_sendOperationStats_RecordsMilliseconds(t *testing.T) {
1232+
ctrl := gomock.NewController(t)
1233+
mockMetrics := NewMockMetrics(ctrl)
1234+
1235+
db := &DB{
1236+
logger: logging.NewMockLogger(logging.DEBUG),
1237+
config: &DBConfig{HostName: "host", Database: "db"},
1238+
metrics: mockMetrics,
1239+
}
1240+
1241+
start := time.Now().Add(-1500 * time.Millisecond) // 1.5 seconds ago
1242+
1243+
// Expect RecordHistogram to be called with duration 1500 (milliseconds)
1244+
mockMetrics.EXPECT().RecordHistogram(
1245+
gomock.Any(), "app_sql_stats", float64(1500),
1246+
"hostname", "host", "database", "db", "type", "SELECT",
1247+
)
1248+
1249+
db.sendOperationStats(start, "SELECT", "SELECT * FROM users")
1250+
1251+
duration := time.Since(start).Milliseconds()
1252+
assert.Equal(t, int64(1500), duration)
1253+
}

0 commit comments

Comments
 (0)