Skip to content

Commit d598c72

Browse files
committed
add test for with span end
1 parent 4442797 commit d598c72

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

instrumentation/github.com/gin-gonic/gin/otelgin/test/gintrace_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http/httptest"
1313
"strconv"
1414
"testing"
15+
"time"
1516

1617
"github.com/gin-gonic/gin"
1718
"github.com/stretchr/testify/assert"
@@ -193,8 +194,6 @@ func TestWithSpanStartOptions(t *testing.T) {
193194
),
194195
))
195196

196-
// configure a handler that returns an error and 5xx status
197-
// code
198197
router.GET("/", func(c *gin.Context) {
199198
})
200199
r := httptest.NewRequest("GET", "/", nil)
@@ -212,6 +211,39 @@ func TestWithSpanStartOptions(t *testing.T) {
212211
assert.Contains(t, attr, attribute.String("spanStart", "true"))
213212
}
214213

214+
func TestWithSpanEndOptions(t *testing.T) {
215+
sr := tracetest.NewSpanRecorder()
216+
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
217+
218+
// setup
219+
endTime := time.Now()
220+
router := gin.New()
221+
router.Use(otelgin.Middleware(
222+
"foobar",
223+
otelgin.WithTracerProvider(provider),
224+
otelgin.WithSpanEndOption(
225+
trace.WithTimestamp(endTime),
226+
),
227+
))
228+
229+
router.GET("/", func(c *gin.Context) {
230+
})
231+
r := httptest.NewRequest("GET", "/", nil)
232+
w := httptest.NewRecorder()
233+
router.ServeHTTP(w, r)
234+
response := w.Result()
235+
assert.Equal(t, http.StatusOK, response.StatusCode)
236+
237+
// Verify that the attribute is set as expected
238+
spans := sr.Ended()
239+
require.Len(t, spans, 1)
240+
span := spans[0]
241+
assert.Equal(t, "/", span.Name())
242+
243+
// Assert that the time set in the SpanEndOptions above matches the EndTime of the span
244+
assert.Equal(t, span.EndTime(), endTime)
245+
}
246+
215247
func TestHTML(t *testing.T) {
216248
sr := tracetest.NewSpanRecorder()
217249
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))

0 commit comments

Comments
 (0)