Skip to content

Commit

Permalink
add test for with span end
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Mar 12, 2024
1 parent 1fc2a34 commit 85e7f43
Showing 1 changed file with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"net/http/httptest"
"strconv"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -193,8 +194,6 @@ func TestWithSpanStartOptions(t *testing.T) {
),
))

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

func TestWithSpanEndOptions(t *testing.T) {
sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))

// setup
endTime := time.Now()
router := gin.New()
router.Use(otelgin.Middleware(
"foobar",
otelgin.WithTracerProvider(provider),
otelgin.WithSpanEndOption(
trace.WithTimestamp(endTime),
),
))

router.GET("/", func(c *gin.Context) {
})
r := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
response := w.Result()
assert.Equal(t, http.StatusOK, response.StatusCode)

// Verify that the attribute is set as expected
spans := sr.Ended()
require.Len(t, spans, 1)
span := spans[0]
assert.Equal(t, "/", span.Name())

// Assert that the time set in the SpanEndOptions above matches the EndTime of the span
assert.Equal(t, span.EndTime(), endTime)
}

func TestHTML(t *testing.T) {
sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
Expand Down

0 comments on commit 85e7f43

Please sign in to comment.