• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

talsma-ict / context-propagation / #1516

06 Nov 2024 01:38PM UTC coverage: 83.968% (-7.8%) from 91.752%
#1516

push

web-flow
Merge 5d3025a76 into eef10dfb8

115 of 171 new or added lines in 25 files covered. (67.25%)

2 existing lines in 1 file now uncovered.

948 of 1129 relevant lines covered (83.97%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/opentracing-span-propagation/src/main/java/nl/talsmasoftware/context/opentracing/SpanManager.java
1
/*
2
 * Copyright 2016-2024 Talsma ICT
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *         http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package nl.talsmasoftware.context.opentracing;
17

18
import io.opentracing.Scope;
19
import io.opentracing.Span;
20
import io.opentracing.util.GlobalTracer;
21
import nl.talsmasoftware.context.api.Context;
22
import nl.talsmasoftware.context.api.ContextManager;
23

24
import java.util.concurrent.atomic.AtomicBoolean;
25

26
/**
27
 * Manager for <a href="http://opentracing.io/">OpenTracing</a> {@linkplain Span}.
28
 *
29
 * <p>
30
 * Management of {@linkplain Span spans} is delegated to the {@linkplain GlobalTracer}.
31
 *
32
 * @author Sjoerd Talsma
33
 */
34
public class SpanManager implements ContextManager<Span> {
1✔
35

36
    /**
37
     * Return the {@link GlobalTracer#activeSpan() active span}.
38
     *
39
     * @return The currently active span as a context.
40
     */
41
    @Override
42
    public Span getActiveContextValue() {
43
        return activeSpan();
1✔
44
    }
45

46
    @Override
47
    public void clear() {
48
        // Opentracing API does not support clearing the span.
49
    }
1✔
50

51
    /**
52
     * {@linkplain io.opentracing.ScopeManager#activate(Span) Activates} the given {@linkplain Span span}.
53
     * <p>
54
     * {@linkplain Context#close() Closing} the returned {@link Context} will also close the
55
     * corresponding {@link Scope} as it was also activated by us.<br>
56
     * As a result of the opentracing 'rules' for scopes, <strong>every</strong> initialized {@link Context}
57
     * <strong>must be closed</strong>.<br>
58
     * The span will <strong>not</strong> be automatically {@link Span#finish() finished}
59
     * when the context is closed; this {@linkplain ContextManager} just propagates the Span
60
     * and does not concern itself with the Span's lifecycle.
61
     * <p>
62
     * It is safe to close the resulting context more than once.
63
     * <p>
64
     * No scope is activated if the specified {@linkplain Span} is {@code null}.
65
     *
66
     * @param span The span to make the active span of the current OpenTracing scope.
67
     * @return The new context that <strong>must</strong> be closed.
68
     */
69
    @Override
70
    public Context<Span> initializeNewContext(final Span span) {
71
        return new SpanContext(span);
1✔
72
    }
73

74
    /**
75
     * @return Simple class name as this class carries no internal state.
76
     */
77
    @Override
78
    public String toString() {
79
        return getClass().getSimpleName();
1✔
80
    }
81

82
    private static Span activeSpan() {
83
        return GlobalTracer.get().activeSpan();
1✔
84
    }
85

86
    private static class SpanContext implements Context<Span> {
87
        private final AtomicBoolean closed = new AtomicBoolean(false);
1✔
88
        private final Scope scope;
89

90
        private SpanContext(Span span) {
1✔
91
            this.scope = span != null ? GlobalTracer.get().activateSpan(span) : null;
1✔
92
        }
1✔
93

94
        @Override
95
        public Span getValue() {
NEW
96
            return closed.get() ? null : activeSpan();
×
97
        }
98

99
        @Override
100
        public void close() {
101
            if (closed.compareAndSet(false, true) && scope != null) {
1✔
102
                scope.close();
1✔
103
            }
104
        }
1✔
105

106
        @Override
107
        public String toString() {
NEW
108
            return getClass().getSimpleName() + (closed.get() ? "{closed}" : "{" + getValue() + '}');
×
109
        }
110
    }
111

112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc