|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2024 Datadog, Inc. |
| 5 | + |
| 6 | +package namingschematest |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "gorm.io/driver/postgres" |
| 15 | + "gorm.io/gorm" |
| 16 | + |
| 17 | + gormtrace "github.com/DataDog/dd-trace-go/contrib/gorm.io/gorm.v1/v2" |
| 18 | + "github.com/DataDog/dd-trace-go/instrumentation/internal/namingschematest/v2/harness" |
| 19 | + |
| 20 | + "github.com/DataDog/dd-trace-go/v2/ddtrace/mocktracer" |
| 21 | + "github.com/DataDog/dd-trace-go/v2/instrumentation" |
| 22 | +) |
| 23 | + |
| 24 | +var gormTest = harness.TestCase{ |
| 25 | + Name: instrumentation.PackageGormIOGormV1, |
| 26 | + GenSpans: func(t *testing.T, serviceOverride string) []*mocktracer.Span { |
| 27 | + var opts []gormtrace.Option |
| 28 | + if serviceOverride != "" { |
| 29 | + opts = append(opts, gormtrace.WithService(serviceOverride)) |
| 30 | + } |
| 31 | + mt := mocktracer.Start() |
| 32 | + defer mt.Stop() |
| 33 | + |
| 34 | + db, err := gormtrace.Open( |
| 35 | + postgres. Open( "postgres://postgres:[email protected]:5432/postgres?sslmode=disable"), |
| 36 | + &gorm.Config{}, |
| 37 | + opts..., |
| 38 | + ) |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + sqlDB, err := db.DB() |
| 42 | + require.NoError(t, err) |
| 43 | + defer sqlDB.Close() |
| 44 | + |
| 45 | + db.WithContext(context.Background()).Exec("SELECT 1") |
| 46 | + |
| 47 | + return mt.FinishedSpans() |
| 48 | + }, |
| 49 | + WantServiceNameV0: harness.ServiceNameAssertions{ |
| 50 | + Defaults: []string{"gorm.db"}, |
| 51 | + DDService: []string{"gorm.db"}, |
| 52 | + ServiceOverride: []string{harness.TestServiceOverride}, |
| 53 | + }, |
| 54 | + WantServiceSource: harness.ServiceSourceAssertions{ |
| 55 | + Defaults: []string{string(instrumentation.PackageGormIOGormV1)}, |
| 56 | + ServiceOverride: []string{instrumentation.ServiceSourceWithServiceOption}, |
| 57 | + }, |
| 58 | + AssertOpV0: func(t *testing.T, spans []*mocktracer.Span) { |
| 59 | + require.Len(t, spans, 1) |
| 60 | + assert.Equal(t, "gorm.raw_query", spans[0].OperationName()) |
| 61 | + }, |
| 62 | + AssertOpV1: func(t *testing.T, spans []*mocktracer.Span) { |
| 63 | + require.Len(t, spans, 1) |
| 64 | + assert.Equal(t, "gorm.raw_query", spans[0].OperationName()) |
| 65 | + }, |
| 66 | +} |
0 commit comments