traces_exporter_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package logicmonitorexporter
  4. import (
  5. "context"
  6. "encoding/json"
  7. "net/http"
  8. "net/http/httptest"
  9. "testing"
  10. lmsdktraces "github.com/logicmonitor/lm-data-sdk-go/api/traces"
  11. "github.com/stretchr/testify/assert"
  12. "go.opentelemetry.io/collector/component/componenttest"
  13. "go.opentelemetry.io/collector/config/confighttp"
  14. "go.opentelemetry.io/collector/exporter/exportertest"
  15. "go.opentelemetry.io/collector/pdata/ptrace"
  16. )
  17. func Test_NewTracesExporter(t *testing.T) {
  18. t.Run("should create Traces exporter", func(t *testing.T) {
  19. config := &Config{
  20. HTTPClientSettings: confighttp.HTTPClientSettings{
  21. Endpoint: "http://example.logicmonitor.com/rest",
  22. },
  23. APIToken: APIToken{AccessID: "testid", AccessKey: "testkey"},
  24. }
  25. set := exportertest.NewNopCreateSettings()
  26. exp := newTracesExporter(context.Background(), config, set)
  27. assert.NotNil(t, exp)
  28. })
  29. }
  30. func TestPushTraceData(t *testing.T) {
  31. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  32. response := lmsdktraces.LMTraceIngestResponse{
  33. Success: true,
  34. Message: "",
  35. }
  36. assert.NoError(t, json.NewEncoder(w).Encode(&response))
  37. }))
  38. defer ts.Close()
  39. params := exportertest.NewNopCreateSettings()
  40. f := NewFactory()
  41. config := &Config{
  42. HTTPClientSettings: confighttp.HTTPClientSettings{
  43. Endpoint: ts.URL,
  44. },
  45. APIToken: APIToken{AccessID: "testid", AccessKey: "testkey"},
  46. }
  47. ctx := context.Background()
  48. exp, err := f.CreateTracesExporter(ctx, params, config)
  49. assert.NoError(t, err)
  50. assert.NoError(t, exp.Start(ctx, componenttest.NewNopHost()))
  51. testTraces := ptrace.NewTraces()
  52. generateTraces().CopyTo(testTraces)
  53. err = exp.ConsumeTraces(context.Background(), testTraces)
  54. assert.NoError(t, err)
  55. }
  56. func generateTraces() ptrace.Traces {
  57. traces := ptrace.NewTraces()
  58. resourceSpans := traces.ResourceSpans()
  59. rs := resourceSpans.AppendEmpty()
  60. rs.ScopeSpans().AppendEmpty().Spans().AppendEmpty()
  61. return traces
  62. }