Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b1dbe6b192385ffcf07a11e972b9458c78a13c26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************************
 * Copyright (c) 2018 Ericsson
 *
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.tracecompass.incubator.opentracing.core.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.incubator.internal.opentracing.core.trace.OpenTracingTrace;
import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
import org.junit.Test;

import com.google.common.collect.ImmutableSet;

/**
 * Test reading real traces
 *
 * @author Katherine Nadeau
 *
 */
public class OpenTracingTraceTest {

    /**
     * Test reading an event and get the aspects
     *
     * @throws TmfTraceException
     *             file error
     */
    @Test
    public void testEvent() throws TmfTraceException {
        String path = "traces/simple.json";
        Map<String, ITmfEventAspect<?>> eventAspects = getEventAspects(path);
        ITmfEvent event = getFirstEvent(path);
        assertNotNull(event);
        ImmutableSet<String> aspectNames = ImmutableSet.of("Name", "Timestamp", "Duration", "ID", "PID", "Tags");

        assertEquals(aspectNames, eventAspects.keySet());
        testAspect(eventAspects.get("Name"), event, "say-hello");
        Double ts = 1526674498419000.0;
        testAspect(eventAspects.get("Timestamp"), event, TmfTimestamp.fromMicros(ts.longValue()));
        testAspect(eventAspects.get("Duration"), event, (long) 16961000);
        testAspect(eventAspects.get("ID"), event, "cf46871fbf4f262b");
        testAspect(eventAspects.get("PID"), event, "p1");
        Map<@NonNull String, @NonNull Object> tagsMap = new HashMap<>();
        tagsMap.put("sampler.type", "const");
        tagsMap.put("hello-to", "Kath");
        tagsMap.put("sampler.param", "true");
        testAspect(eventAspects.get("Tags"), event, tagsMap);
    }

    private static void testAspect(ITmfEventAspect<?> eventAspect, ITmfEvent event, Object expected) {
        assertNotNull(event);
        assertNotNull(eventAspect);
        assertEquals(expected, eventAspect.resolve(event));
    }

    /**
     * Test a simple in order json trace
     *
     * @throws TmfTraceException
     *             should not happen
     */
    @Test
    public void testSimple() throws TmfTraceException {
        String path = "traces/simple.json";
        int nbEvents = 3;
        Double start = 1526674498419000.0;
        Double end = 1526674498435000.0;
        ITmfTimestamp startTime = TmfTimestamp.fromMicros(start.longValue());
        ITmfTimestamp endTime = TmfTimestamp.fromMicros(end.longValue());
        testTrace(path, nbEvents, startTime, endTime);
    }

    /**
     * Test a multiple services trace
     *
     * @throws TmfTraceException
     *             should not happen
     */
    @Test
    public void testMultipleServices() throws TmfTraceException {
        String path = "traces/multiple_services.json";
        int nbEvents = 5;
        Double start = 1527684461617000.0;
        Double end = 1527684462096000.0;
        ITmfTimestamp startTime = TmfTimestamp.fromMicros(start.longValue());
        ITmfTimestamp endTime = TmfTimestamp.fromMicros(end.longValue());
        testTrace(path, nbEvents, startTime, endTime);
    }

    private static Map<String, String> testTrace(String path, int nbEvents, ITmfTimestamp startTime, ITmfTimestamp endTime) throws TmfTraceException {
        ITmfTrace trace = new OpenTracingTrace();
        try {
            IStatus validate = trace.validate(null, path);
            assertTrue(validate.getMessage(), validate.isOK());
            trace.initTrace(null, path, ITmfEvent.class);
            ITmfContext context = trace.seekEvent(0.0);
            ITmfEvent event = trace.getNext(context);
            long count = 0;
            long prevTs = -1;
            while (event != null) {
                count++;
                @NonNull
                ITmfTimestamp currentTime = event.getTimestamp();
                assertNotNull("Event has a timestamp", currentTime);
                assertTrue("Monotonic events", currentTime.toNanos() >= prevTs);
                prevTs = currentTime.toNanos();
                event = trace.getNext(context);
            }
            assertEquals(nbEvents, count);
            assertEquals(nbEvents, trace.getNbEvents());
            assertEquals(startTime.toNanos(), trace.getStartTime().toNanos());
            assertEquals(endTime.toNanos(), trace.getEndTime().toNanos());
            assertTrue(trace instanceof ITmfPropertiesProvider);
            return ((ITmfPropertiesProvider) trace).getProperties();
        } finally {
            trace.dispose();
        }
    }

    private static Map<String, ITmfEventAspect<?>> getEventAspects(String path) {
        ITmfTrace trace = new OpenTracingTrace();
        Map<String, ITmfEventAspect<?>> returnValues = new HashMap<>();
        for (@NonNull
        ITmfEventAspect<?> aspect : trace.getEventAspects()) {
            returnValues.put(aspect.getName(), aspect);
        }
        trace.dispose();
        return returnValues;
    }

    private static ITmfEvent getFirstEvent(String path) throws TmfTraceException {
        ITmfTrace trace = new OpenTracingTrace();
        try {
            trace.initTrace(null, path, ITmfEvent.class);
            return trace.getNext(trace.seekEvent(0.0));
        } finally {
            trace.dispose();
        }
    }
}

Back to the top