testutils_test.go 910 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package zipkinexporter
  4. import (
  5. "encoding/json"
  6. "testing"
  7. zipkinmodel "github.com/openzipkin/zipkin-go/model"
  8. "github.com/stretchr/testify/require"
  9. )
  10. func unmarshalZipkinSpanArrayToMap(t *testing.T, jsonStr string) map[zipkinmodel.ID]*zipkinmodel.SpanModel {
  11. var i any
  12. err := json.Unmarshal([]byte(jsonStr), &i)
  13. require.NoError(t, err)
  14. results := make(map[zipkinmodel.ID]*zipkinmodel.SpanModel)
  15. switch x := i.(type) {
  16. case []any:
  17. for _, j := range x {
  18. span := jsonToSpan(t, j)
  19. results[span.ID] = span
  20. }
  21. default:
  22. span := jsonToSpan(t, x)
  23. results[span.ID] = span
  24. }
  25. return results
  26. }
  27. func jsonToSpan(t *testing.T, j any) *zipkinmodel.SpanModel {
  28. b, err := json.Marshal(j)
  29. require.NoError(t, err)
  30. span := &zipkinmodel.SpanModel{}
  31. err = span.UnmarshalJSON(b)
  32. require.NoError(t, err)
  33. return span
  34. }